Parse flags before accessing config

Fixes #13.
This commit is contained in:
Christian Muehlhaeuser 2020-10-06 04:07:22 +02:00
parent d15fedd774
commit e57cb0710e
No known key found for this signature in database
GPG Key ID: 3CF9FA45CA1EBB7E
1 changed files with 14 additions and 12 deletions

26
main.go
View File

@ -22,6 +22,19 @@ var (
Short: "mastotool offers a collection of tools to work with your Mastodon account", Short: "mastotool offers a collection of tools to work with your Mastodon account",
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: 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() { func main() {
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "mastodon.json", "uses the specified config file") 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 { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(-1) os.Exit(1)
} }
} }