From ad9f720b35a791647e96a6e9a042453db85ab544 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sat, 18 Dec 2021 02:29:55 +0300 Subject: [PATCH] flick --- cmd/add.go | 11 +++++++++++ cmd/ls.go | 9 +++++++++ cmd/root.go | 15 +-------------- cmd/start.go | 12 ++++++++++++ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index b8979b0..6367f10 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,7 +3,10 @@ package cmd import ( "strings" + "github.com/spf13/afero" "github.com/spf13/cobra" + + "github.com/neonxp/track/internal/tracker" ) // addCmd represents the add command @@ -34,6 +37,14 @@ var addCmd = &cobra.Command{ titles = append(titles, s) } title := strings.Join(titles, " ") + + fs := afero.NewOsFs() + tr, err := tracker.New(fs) + if err != nil { + cmd.PrintErr(err) + return + } + activityID, err := tr.Add(title, tags, contexts) if err != nil { cmd.PrintErr(err) diff --git a/cmd/ls.go b/cmd/ls.go index eaf58e1..846efba 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -4,6 +4,7 @@ import ( "strings" "time" + "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/neonxp/track/internal/tracker" @@ -25,7 +26,15 @@ var lsCmd = &cobra.Command{ cmd.PrintErr(err) return } + + fs := afero.NewOsFs() + tr, err := tracker.New(fs) + if err != nil { + cmd.PrintErr(err) + return + } activities := tr.List(all) + if len(activities) == 0 { cmd.Printf("There is no activities.\n") return diff --git a/cmd/root.go b/cmd/root.go index ad372c1..6126a3e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,12 +4,9 @@ import ( "fmt" "os" - "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/spf13/viper" - - "github.com/neonxp/track/internal/tracker" ) var cfgFile string @@ -18,21 +15,12 @@ var cfgFile string var rootCmd = &cobra.Command{ Use: "track", Short: "Track your work or personal activities", - Long: `Track time spent for any work or personal activities`, + Long: `Track time spent for any work or personal activities`, } -var tr *tracker.Tracker - // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - fs := afero.NewOsFs() - var err error - tr, err = tracker.New(fs) - if err != nil { - panic(err) - return - } cobra.CheckErr(rootCmd.Execute()) } @@ -65,4 +53,3 @@ func initConfig() { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } } - diff --git a/cmd/start.go b/cmd/start.go index a0ae4f1..8ec2a11 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -4,7 +4,10 @@ import ( "strconv" "strings" + "github.com/spf13/afero" "github.com/spf13/cobra" + + "github.com/neonxp/track/internal/tracker" ) // startCmd represents the start command @@ -23,11 +26,20 @@ var startCmd = &cobra.Command{ return } comment := strings.Join(args[1:], " ") + + fs := afero.NewOsFs() + tr, err := tracker.New(fs) + if err != nil { + cmd.PrintErr(err) + return + } if err := tr.Start(id, comment); err != nil { cmd.PrintErr(err) return } + activity := tr.Activity(id) + cmd.Printf("Started new span for activity \"%s\".\n", activity.Title) }, }