From e57cb0710e3d21cc5e72cf927d4dbc5be20bb19d Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 6 Oct 2020 04:07:22 +0200 Subject: [PATCH] Parse flags before accessing config Fixes #13. --- main.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 18867c6..8e5674e 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,19 @@ var ( Short: "mastotool offers a collection of tools to work with your Mastodon account", SilenceErrors: true, SilenceUsage: true, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if err := initClient(); err != nil { + return err + } + + var err error + self, err = client.GetAccountCurrentUser(context.Background()) + if err != nil { + return fmt.Errorf("Can't retrieve user: %s", err) + } + + return nil + }, } ) @@ -120,19 +133,8 @@ func initClient() error { func main() { RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "mastodon.json", "uses the specified config file") - if err := initClient(); err != nil { - fmt.Println(err) - os.Exit(1) - } - var err error - self, err = client.GetAccountCurrentUser(context.Background()) - if err != nil { - fmt.Printf("Can't retrieve user: %s\n", err) - os.Exit(1) - } - if err := RootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(-1) + os.Exit(1) } }