Initial Commit
This commit is contained in:
22
cmd/break.go
Normal file
22
cmd/break.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// breakCmd represents the break command
|
||||
var breakCmd = &cobra.Command{
|
||||
Use: "break",
|
||||
Short: "Start a break",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(breakCmd)
|
||||
}
|
79
cmd/config.go
Normal file
79
cmd/config.go
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// configCmd represents the config command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Edit breaktime config using your default editor",
|
||||
RunE: opConfig,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
}
|
||||
|
||||
func opConfig(cmd *cobra.Command, args []string) error {
|
||||
updConfig := make(map[string]string)
|
||||
if len(args) == 1 && args[0] == "edit" {
|
||||
file := viper.ConfigFileUsed()
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
return fmt.Errorf("No EDITOR set")
|
||||
}
|
||||
fmt.Println("Starting", editor, file)
|
||||
c := exec.Command(editor, file)
|
||||
c.Stdin = os.Stdin
|
||||
c.Stdout = os.Stdout
|
||||
return c.Run()
|
||||
}
|
||||
if len(args) > 0 {
|
||||
// We're setting arguments
|
||||
for _, a := range args {
|
||||
pts := strings.Split(a, "=")
|
||||
if len(pts) == 2 {
|
||||
updConfig[pts[0]] = pts[1]
|
||||
} else {
|
||||
return fmt.Errorf("Unable to parse config values.")
|
||||
}
|
||||
}
|
||||
}
|
||||
var settings []string
|
||||
for k, v := range viper.AllSettings() {
|
||||
switch v.(type) {
|
||||
case bool:
|
||||
if nv, ok := updConfig[k]; ok {
|
||||
v = nv == "true"
|
||||
viper.Set(k, v)
|
||||
viper.WriteConfig()
|
||||
}
|
||||
settings = append(settings, fmt.Sprintf("%s: %t", k, v))
|
||||
default:
|
||||
if nv, ok := updConfig[k]; ok {
|
||||
v = nv
|
||||
viper.Set(k, v)
|
||||
viper.WriteConfig()
|
||||
}
|
||||
settings = append(settings, fmt.Sprintf("%s: %s", k, v))
|
||||
}
|
||||
}
|
||||
sort.Strings(settings)
|
||||
fmt.Println("Configuration File:", viper.ConfigFileUsed())
|
||||
for _, v := range settings {
|
||||
fmt.Println(v)
|
||||
}
|
||||
return nil
|
||||
}
|
37
cmd/i3.go
Normal file
37
cmd/i3.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// i3Cmd represents the i3 command
|
||||
var i3Cmd = &cobra.Command{
|
||||
Use: "i3",
|
||||
Short: "Print state to stdout in i3 bar format",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
lastBreak := viper.GetTime("lastbreak")
|
||||
pomo := viper.GetDuration("pomodoro")
|
||||
warnPct := viper.GetFloat64("warnpct") //0.75
|
||||
warn := pomo * time.Duration(warnPct)
|
||||
elapsed := time.Since(lastBreak)
|
||||
if elapsed > pomo {
|
||||
fmt.Print("{\"icon\":\"tomato\",\"state\":\"Critical\", \"text\": \"BREAK!\"}")
|
||||
} else if elapsed > warn {
|
||||
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Warning\", \"text\": \"%s\"}", elapsed)
|
||||
} else {
|
||||
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Good\", \"text\": \"%s\"}", elapsed)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(i3Cmd)
|
||||
}
|
105
cmd/root.go
Normal file
105
cmd/root.go
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
gap "github.com/muesli/go-app-paths"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "breaktime",
|
||||
Short: "A pomodoro-style timer app",
|
||||
RunE: runRootCmd,
|
||||
}
|
||||
|
||||
// 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() {
|
||||
viper.SetDefault("lastbreak", time.Now())
|
||||
p, _ := time.ParseDuration("25m")
|
||||
viper.SetDefault("pomodoro", p)
|
||||
viper.SetDefault("warnpct", 0.75)
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.breaktime.yaml)")
|
||||
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
var firstDir string // in case we have to create directories
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
scope := gap.NewScope(gap.User, "breaktime")
|
||||
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)
|
||||
}
|
||||
cobra.CheckErr(err)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("breaktime")
|
||||
}
|
||||
var createConfig bool
|
||||
v2Path := fmt.Sprintf("%s%s%s", firstDir, string(os.PathSeparator), "breaktime.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)
|
||||
}
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
func runRootCmd(cmd *cobra.Command, args []string) error {
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user