From 2b49d8dd5129f6e57e38855d824f2986c75c21c2 Mon Sep 17 00:00:00 2001 From: filifa Date: Fri, 9 May 2025 22:50:15 -0400 Subject: [PATCH] return errors instead of panicking --- cmd/root.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6f3e96c..f50e980 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,18 +55,18 @@ gv2adj -f --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")