104 lines
2.4 KiB
Go
104 lines
2.4 KiB
Go
|
/*
|
||
|
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()
|
||
|
}
|
||
|
}
|