Working on v2

This commit is contained in:
2022-01-19 12:56:31 -06:00
parent 1895739c71
commit a2924b8a61
32 changed files with 2121 additions and 20 deletions

40
cmd/archive.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// archiveCmd represents the archive command
var archiveCmd = &cobra.Command{
Use: "archive",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("archive called")
},
}
func init() {
rootCmd.AddCommand(archiveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// archiveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// archiveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/config.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("config called")
},
}
func init() {
rootCmd.AddCommand(configCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/editor.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// editorCmd represents the editor command
var editorCmd = &cobra.Command{
Use: "editor",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("editor called")
},
}
func init() {
rootCmd.AddCommand(editorCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// editorCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// editorCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/fuzzyparse.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// fuzzyparseCmd represents the fuzzyparse command
var fuzzyparseCmd = &cobra.Command{
Use: "fuzzyparse",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("fuzzyparse called")
},
}
func init() {
rootCmd.AddCommand(fuzzyparseCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// fuzzyparseCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// fuzzyparseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/i3status.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// i3statusCmd represents the i3status command
var i3statusCmd = &cobra.Command{
Use: "i3status",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("i3status called")
},
}
func init() {
rootCmd.AddCommand(i3statusCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// i3statusCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// i3statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

163
cmd/list.go Normal file
View File

@@ -0,0 +1,163 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"strings"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls", "lsa", "lsd"},
Short: "List Timers",
RunE: opListTimers,
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Local Flags
listCmd.Flags().BoolP("all", "a", false, "Include done.txt timers")
listCmd.Flags().BoolP("done", "d", false, "Only done.txt timers")
}
func opListTimers(cmd *cobra.Command, args []string) error {
var err error
var includeArchive bool
var onlyArchive bool
//onlyArchive := cmd.CalledAs() == "lsd" || listCmd.Flags().Lookup
if onlyArchive, err = cmd.Flags().GetBool("done"); err != nil {
return err
} else if includeArchive, err = cmd.Flags().GetBool("all"); err != nil {
return err
}
if cmd.CalledAs() == "lsd" {
onlyArchive = true
} else if cmd.CalledAs() == "lsa" {
includeArchive = true
}
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if !onlyArchive {
if err := p.LoadTimerList(); err != nil {
return err
}
}
if includeArchive || onlyArchive {
if err := p.LoadDoneList(); err != nil {
return err
}
}
start := time.Time{}
end := time.Now()
var contextFilters []string
var projectFilters []string
var allFilters []func(timertxt.Timer) bool
if len(args) > 0 {
contextFilters, args = util.GetContextsFromSlice(args)
projectFilters, args = util.GetProjectsFromSlice(args)
}
if len(args) > 0 {
if start, err = util.ParseFuzzyTime(args[0]); err != nil {
y, m, d := time.Now().Date()
start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())
} else {
args = args[1:]
}
if len(args) > 0 {
if end, err = util.ParseFuzzyTime(args[0]); err != nil {
y, m, d := time.Now().Date()
end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location())
} else {
args = args[1:]
}
}
}
list := timertxt.NewTimerList()
if !onlyArchive {
list = p.TimerList.GetTimersInRange(start, end)
if includeArchive {
*list = append(*list, (*p.DoneList.GetTimersInRange(start, end))...)
}
} else {
list = p.DoneList.GetTimersInRange(start, end)
}
if len(contextFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range contextFilters {
v = strings.TrimPrefix(v, "@")
if !t.HasContext(v) {
return false
}
}
return true
})
}
if len(projectFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range projectFilters {
v = strings.TrimPrefix(v, "+")
if !t.HasProject(v) {
return false
}
}
return true
})
}
doFilters := func(t *timertxt.Timer) bool {
for _, v := range allFilters {
if !v(*t) {
return false
}
}
// If we made it all the way down here, it matches
return true
}
list = list.Filter(doFilters)
dayTotals := make(map[string]time.Duration)
for _, v := range *list {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
}
dayTotals[v.StartDate.Format("2006/01/02")] += dur
}
var oldDayStr, dayStr string
for _, v := range *list {
oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr {
// TODO:
wrkDur := dayTotals[dayStr].Round(util.GetRoundToDuration())
fmtStr := dayStr + " ( %.2f )\n"
fmt.Printf(fmtStr, util.DurationToDecimal(wrkDur))
}
fmt.Println(" " + util.TimerToFriendlyString(v))
}
return nil
}

40
cmd/mod.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// modCmd represents the mod command
var modCmd = &cobra.Command{
Use: "mod",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("mod called")
},
}
func init() {
rootCmd.AddCommand(modCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// modCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// modCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/rm.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// rmCmd represents the rm command
var rmCmd = &cobra.Command{
Use: "rm",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("rm called")
},
}
func init() {
rootCmd.AddCommand(rmCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// rmCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// rmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

103
cmd/root.go Normal file
View File

@@ -0,0 +1,103 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"git.bullercodeworks.com/brian/gime/util"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
Version = "2.0"
configFile string
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "gime",
Short: "Timer.txt Client",
Long: "Gime is a timer.txt client to make all aspects of timekeeping simpler.",
Run: func(cmd *cobra.Command, args []string) {
},
}
)
// 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() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Version = Version
initConfig()
}
func initConfig() {
viper.SetDefault("roundto", "15m")
viper.SetDefault("timerfile", "timer.txt")
viper.SetDefault("donefile", "done.txt")
viper.SetDefault("reportfile", "report.txt")
var firstDir string // In case we need to make directories
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
scope := gap.NewScope(gap.User, "gime")
dirs, err := scope.ConfigDirs()
if err != nil {
fmt.Println("Can't retrieve default config. Please manually pass a config file with '--config'")
os.Exit(1)
}
firstDir = dirs[0]
for _, v := range dirs {
viper.AddConfigPath(v)
}
viper.SetConfigName("gime")
viper.SetConfigType("yaml")
}
var createConfig bool
v2Path := fmt.Sprintf("%s%s%s", firstDir, string(os.PathSeparator), "gime.yaml")
if err := viper.ReadInConfig(); err != nil {
createConfig = true
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; Check if we have a v1 config file
fmt.Println("Config file not found.")
} else {
fmt.Println("Found config file, but another error occurred.")
fmt.Println(err)
}
}
if createConfig {
_, err := os.Stat(firstDir)
if os.IsNotExist(err) {
err := os.Mkdir(firstDir, 0755)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if err = viper.WriteConfigAs(v2Path); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
dir := viper.GetString("directory")
if dir == "" {
dir = util.PromptUser("Path to timer.txt")
if dir == "" {
fmt.Println("No path to timer.txt given")
os.Exit(1)
}
viper.Set("directory", dir)
viper.WriteConfig()
}
}

40
cmd/start.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("start called")
},
}
func init() {
rootCmd.AddCommand(startCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

53
cmd/status.go Normal file
View File

@@ -0,0 +1,53 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"github.com/spf13/cobra"
)
// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Prints the status of all active timers",
RunE: opStatus,
}
func init() {
rootCmd.AddCommand(statusCmd)
}
func opStatus(cmd *cobra.Command, args []string) error {
var err error
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if err := p.LoadTimerList(); err != nil {
return err
}
if len(*p.TimerList.GetActiveTimers()) == 0 {
fmt.Println("No timers running")
return nil
}
var currDur time.Duration
for _, v := range *p.TimerList {
if v.ActiveToday() {
currDur += v.Duration()
}
}
d := currDur.Round(util.GetRoundToDuration())
fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d))
for _, v := range *p.TimerList.GetActiveTimers() {
fmt.Println(util.TimerToFriendlyString(v))
}
return nil
}

40
cmd/stop.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("stop called")
},
}
func init() {
rootCmd.AddCommand(stopCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// stopCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// stopCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/switch.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// switchCmd represents the switch command
var switchCmd = &cobra.Command{
Use: "switch",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("switch called")
},
}
func init() {
rootCmd.AddCommand(switchCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// switchCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// switchCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

55
cmd/time.go Normal file
View File

@@ -0,0 +1,55 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"github.com/spf13/cobra"
)
// timeCmd represents the time command
var timeCmd = &cobra.Command{
Use: "time",
Short: "A brief description of your command",
RunE: opShowTimers,
}
func init() {
rootCmd.AddCommand(timeCmd)
}
func opShowTimers(cmd *cobra.Command, args []string) error {
var err error
p := cli.Program{}
err = p.Initialize()
if err != nil {
return err
}
if err := p.LoadTimerList(); err != nil {
return err
}
list := p.GetFilteredTimerList(args)
var isActive bool
var total time.Duration
for _, v := range *list {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
isActive = true
}
total += dur
}
total = total.Round(util.GetRoundToDuration())
if isActive {
fmt.Printf("%.2f+\n", util.DurationToDecimal(total))
} else {
fmt.Printf("%.2f\n", util.DurationToDecimal(total))
}
return nil
}

40
cmd/toggle.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// toggleCmd represents the toggle command
var toggleCmd = &cobra.Command{
Use: "toggle",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("toggle called")
},
}
func init() {
rootCmd.AddCommand(toggleCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// toggleCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// toggleCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/ui.go Normal file
View File

@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// uiCmd represents the ui command
var uiCmd = &cobra.Command{
Use: "ui",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ui called")
},
}
func init() {
rootCmd.AddCommand(uiCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// uiCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// uiCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}