This commit is contained in:
Alexander Kiryukhin 2021-12-18 02:29:55 +03:00
parent 8f08151c00
commit ad9f720b35
No known key found for this signature in database
GPG key ID: 6DF7A2910D0699E9
4 changed files with 33 additions and 14 deletions

View file

@ -3,7 +3,10 @@ package cmd
import ( import (
"strings" "strings"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/neonxp/track/internal/tracker"
) )
// addCmd represents the add command // addCmd represents the add command
@ -34,6 +37,14 @@ var addCmd = &cobra.Command{
titles = append(titles, s) titles = append(titles, s)
} }
title := strings.Join(titles, " ") 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) activityID, err := tr.Add(title, tags, contexts)
if err != nil { if err != nil {
cmd.PrintErr(err) cmd.PrintErr(err)

View file

@ -4,6 +4,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/neonxp/track/internal/tracker" "github.com/neonxp/track/internal/tracker"
@ -25,7 +26,15 @@ var lsCmd = &cobra.Command{
cmd.PrintErr(err) cmd.PrintErr(err)
return return
} }
fs := afero.NewOsFs()
tr, err := tracker.New(fs)
if err != nil {
cmd.PrintErr(err)
return
}
activities := tr.List(all) activities := tr.List(all)
if len(activities) == 0 { if len(activities) == 0 {
cmd.Printf("There is no activities.\n") cmd.Printf("There is no activities.\n")
return return

View file

@ -4,12 +4,9 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/neonxp/track/internal/tracker"
) )
var cfgFile string var cfgFile string
@ -18,21 +15,12 @@ var cfgFile string
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "track", Use: "track",
Short: "Track your work or personal activities", 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. // 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. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
fs := afero.NewOsFs()
var err error
tr, err = tracker.New(fs)
if err != nil {
panic(err)
return
}
cobra.CheckErr(rootCmd.Execute()) cobra.CheckErr(rootCmd.Execute())
} }
@ -65,4 +53,3 @@ func initConfig() {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
} }
} }

View file

@ -4,7 +4,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/neonxp/track/internal/tracker"
) )
// startCmd represents the start command // startCmd represents the start command
@ -23,11 +26,20 @@ var startCmd = &cobra.Command{
return return
} }
comment := strings.Join(args[1:], " ") 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 { if err := tr.Start(id, comment); err != nil {
cmd.PrintErr(err) cmd.PrintErr(err)
return return
} }
activity := tr.Activity(id) activity := tr.Activity(id)
cmd.Printf("Started new span for activity \"%s\".\n", activity.Title) cmd.Printf("Started new span for activity \"%s\".\n", activity.Title)
}, },
} }