Skip to content

Commit

Permalink
feat: allow user configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
deadpyxel committed Nov 8, 2023
1 parent 87352c9 commit 5b0821f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion cmd/end.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// endCmd represents the end command
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// noteCmd represents the note command
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// reportCmd represents the report command
Expand All @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// startCmd represents the start command
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion internal/ioutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"encoding/json"
"errors"
"os"

"github.com/spf13/viper"
)

func SaveEntries(jounalEntries []JournalEntry) error {
data, err := json.Marshal(jounalEntries)
if err != nil {
return err
}
filename := "journal.json"
filename := viper.GetString("journalPath")
file, err := os.Create(filename)
if err != nil {
return err
Expand Down

0 comments on commit 5b0821f

Please sign in to comment.