Skip to content

Commit

Permalink
Fix Issue open-pomodoro#16 - Change openpomodoro client init
Browse files Browse the repository at this point in the history
The --directory flag does not work, all commands use the default
`~/.pomodoro` directory even when the flag is provided.
With the current code, `directoryFlag` is always an empty string when
passed to the openpomodoro.NewClient function because the flag has not
been parsed yet at this time.
Moving to the PersistentPreRun hook ensures that all commands will
initialize the client with the correct flag value.

Co-authored-by: hugobally <hugobally@users.noreply.github.com>
  • Loading branch information
Hugo Bally and hugobally committed Nov 14, 2022
1 parent 82cee17 commit 7afa99f
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,35 @@ import (
"github.com/spf13/viper"
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "pomodoro",
Short: "Pomodoro CLI",
Long: "A simple Pomodoro command-line client for the Open Pomodoro format",
}

var (
client *openpomodoro.Client
settings *openpomodoro.Settings
settings = &openpomodoro.DefaultSettings

directoryFlag string
formatFlag string
waitFlag bool
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "pomodoro",
Short: "Pomodoro CLI",
Long: "A simple Pomodoro command-line client for the Open Pomodoro format",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error

client, err = openpomodoro.NewClient(directoryFlag)
if err != nil {
log.Fatalf("Could not create client: %v", err)
}

settings, err = client.Settings()
if err != nil {
log.Fatalf("Could not retrieve settings: %v", err)
}
},
}

func init() {
cobra.OnInitialize(initConfig)

Expand All @@ -43,18 +56,6 @@ func init() {
"wait for the Pomodoro to end before exiting")

viper.AutomaticEnv()

var err error

client, err = openpomodoro.NewClient(directoryFlag)
if err != nil {
log.Fatalf("Could not create client: %v", err)
}

settings, err = client.Settings()
if err != nil {
log.Fatalf("Could not retrieve settings: %v", err)
}
}

func initConfig() {
Expand Down

0 comments on commit 7afa99f

Please sign in to comment.