return errors instead of panicking

This commit is contained in:
filifa 2025-05-09 22:50:15 -04:00
parent 4cac82441a
commit 2b49d8dd51
1 changed files with 7 additions and 14 deletions

View File

@ -55,18 +55,18 @@ gv2adj -f <dot file> --weight-attr len
`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: parse,
RunE: parse,
}
func parse(cmd *cobra.Command, args []string) {
func parse(cmd *cobra.Command, args []string) error {
data, err := os.ReadFile(file)
if err != nil {
panic(err)
return err
}
ast, err := dotfmt.ParseBytes(data)
if err != nil {
panic(err)
return err
}
first := ast.Graphs[0]
@ -79,15 +79,16 @@ func parse(cmd *cobra.Command, args []string) {
err = dot.UnmarshalMulti(data, graph)
if err != nil {
panic(err)
return err
}
matrix, err := orderedAdjMatrix(graph.WeightedGraph)
if err != nil {
panic(err)
return err
}
outputMatrix(matrix)
return nil
}
func orderedAdjMatrix(g igraph.WeightedGraph) (*mat.Dense, error) {
@ -149,14 +150,6 @@ func Execute() {
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gv2adj.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().StringVarP(&file, "file", "f", "", "dot file")
rootCmd.MarkFlagRequired("file")