From 5b0821f0b3bc118d086d78c2038f8e6de76f8570 Mon Sep 17 00:00:00 2001 From: Robson Cruz Date: Wed, 8 Nov 2023 10:49:50 -0300 Subject: [PATCH] feat: allow user configuration With this change we are finally allowing some customization of options via configuration file. This first iteration makes so the journal file path can be set, and if not, uses the user home as base BREAKING CHANGE: as we are now using safe defaults, previous users will need to move their current journal file to the desired path, or setup the configuration options in the config.yaml file. docs(readme.md): add configuration section --- README.md | 13 +++++++++++-- cmd/end.go | 3 ++- cmd/note.go | 3 ++- cmd/report.go | 3 ++- cmd/root.go | 9 +++++++-- cmd/start.go | 3 ++- internal/ioutils.go | 4 +++- 7 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c98da64..c67339f 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ A simple CLI written in go to help with my day to day activity tracking at work. - Fully CLI Based - Very small footprint (In memory, CPU and codebase) - Cross platform - +- Configurable using config files ## Installation -Install my-project with go +Install workday with go ```bash go install github.com/deadpyxel/workday@latest @@ -26,6 +26,14 @@ And the you can just start to use it: workday ``` +## Configuration + +Workday allows you to configure some options using a YAML configuration file. By default, it will search for the file under your `$HOME/.config/workday/config.yaml`, but you can pass the configuration file path with the `--config` flag. An example of a valid config file can be seen below. + +```yaml +journalPath: "/path/to/your/journal.json" +``` + ## Running Tests To run tests, run the following command @@ -70,6 +78,7 @@ Run the app - Gopher's Public Discord - [cobra-cli](https://github.com/spf13/cobra-cli) - [Cobra Docs](https://github.com/spf13/cobra) + - [Viper](https://github.com/spf13/viper) ## License diff --git a/cmd/end.go b/cmd/end.go index 87051c6..fad74d4 100644 --- a/cmd/end.go +++ b/cmd/end.go @@ -9,6 +9,7 @@ import ( journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // endCmd represents the end command @@ -35,7 +36,7 @@ func markDayAsFinished(cmd *cobra.Command, args []string) error { currentDayId := now.Format("20060102") // Load journal entries - entries, err := journal.LoadEntries("journal.json") + entries, err := journal.LoadEntries(viper.GetString("journalPath")) if err != nil { return err } diff --git a/cmd/note.go b/cmd/note.go index b4a8c29..accd33e 100644 --- a/cmd/note.go +++ b/cmd/note.go @@ -9,6 +9,7 @@ import ( journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // noteCmd represents the note command @@ -29,7 +30,7 @@ Otherwise, it will add the note to the current entry and save the updated journa // If there is no entry for the current day, it prints an error message and returns an error. // Otherwise, it adds the note to the current entry and saves the updated journal entries back to the file. func addNoteToCurrentDay(cmd *cobra.Command, args []string) error { - journalEntries, err := journal.LoadEntries("journal.json") + journalEntries, err := journal.LoadEntries(viper.GetString("journalPath")) if err != nil { return err } diff --git a/cmd/report.go b/cmd/report.go index 828209c..0834c4f 100644 --- a/cmd/report.go +++ b/cmd/report.go @@ -9,6 +9,7 @@ import ( journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // reportCmd represents the report command @@ -28,7 +29,7 @@ Otherwise, it prints out the entry.`, // If there is no entry for the current day, it returns and error. // Otherwise, it prints out the entry. func reportWorkDay(cmd *cobra.Command, args []string) error { - journalEntries, err := journal.LoadEntries("journal.json") + journalEntries, err := journal.LoadEntries(viper.GetString("journalPath")) if err != nil { return err } diff --git a/cmd/root.go b/cmd/root.go index 775cbf0..b645512 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -81,11 +81,16 @@ func initConfig() { cobra.CheckErr(err) // Search config in home directory with name ".workday" (without extension). - viper.AddConfigPath(home) + viper.AddConfigPath(home + "/.config/workday") viper.SetConfigType("yaml") - viper.SetConfigName(".workday") + viper.SetConfigName("config.yaml") } + // Set default value for journalPath. Will use your HOME if not set. + home, err := os.UserHomeDir() + cobra.CheckErr(err) + viper.SetDefault("journalPath", home+"/journal.json") + viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. diff --git a/cmd/start.go b/cmd/start.go index 768a539..4cb3bd1 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -10,6 +10,7 @@ import ( journal "github.com/deadpyxel/workday/internal" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // startCmd represents the start command @@ -33,7 +34,7 @@ After running this command, you can begin adding notes to the new workday entry. // appends the new entry to the journal entries, and saves the updated journal entries back to the file. // It then prints a message indicating that a new JournalEntry has been added for the current day. func startWorkDay(cmd *cobra.Command, args []string) error { - journalEntries, err := journal.LoadEntries("journal.json") + journalEntries, err := journal.LoadEntries(viper.GetString("journalPath")) if err != nil { return err } diff --git a/internal/ioutils.go b/internal/ioutils.go index bd3b34c..c21cda0 100644 --- a/internal/ioutils.go +++ b/internal/ioutils.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "os" + + "github.com/spf13/viper" ) func SaveEntries(jounalEntries []JournalEntry) error { @@ -11,7 +13,7 @@ func SaveEntries(jounalEntries []JournalEntry) error { if err != nil { return err } - filename := "journal.json" + filename := viper.GetString("journalPath") file, err := os.Create(filename) if err != nil { return err