Skip to content

Commit

Permalink
feat(config,cache): ensure parent folder exists (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Jun 12, 2024
1 parent d74ad74 commit 4c5f866
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"os"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -44,6 +45,10 @@ func (c *FileCache) Write(in any) error {
return err
}

if err := os.MkdirAll(filepath.Dir(c.path), 0755); err != nil {
return err
}

return os.WriteFile(c.path, marshalled, 0644)
}

Expand Down
16 changes: 8 additions & 8 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ func init() {
rootCmd.AddCommand(configCmd)

configCmd.Flags().BoolVarP(&editConfigFlag, "edit", "e", false, "Edit the config in $EDITOR")
configCmd.Flags().BoolVarP(&initConfigFlag, "init", "i", false, "Print the default config to stdout")
configCmd.Flags().BoolVarP(&initConfigFlag, "init", "i", false, "Create the default config file")
}

func runConfig(cmd *cobra.Command, args []string) error {
if initConfigFlag {
return initConfig()
if err := initConfig(); err != nil {
return err
}
}

if editConfigFlag {
Expand All @@ -50,15 +52,13 @@ func runConfig(cmd *cobra.Command, args []string) error {
}

func initConfig() error {
slog.Debug("printing config file", "path", configPathFlag)
slog.Debug("creating initial config file", "path", configPathFlag)

marshalled, err := yaml.Marshal(configPkg.Default())
if err != nil {
slog.Error("Failed to marshall config", "err", err)
if err := configPkg.Default().Save(configPathFlag); err != nil {
slog.Error("Failed to save initial config", "err", err)
return err
}

fmt.Printf("%s\n", marshalled)
fmt.Printf("Initial config saved to %s\n", configPathFlag)

return nil
}
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func setupGlobals(cmd *cobra.Command, args []string) error {
return err
}

slog.Debug("config", "config", config)

var caller api.Caller
if notificationDumpPath != "" {
caller = file.New(notificationDumpPath)
Expand Down
16 changes: 16 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"log/slog"
"os"
"path"
"path/filepath"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -87,9 +88,24 @@ func New(path string) (*Config, error) {
return nil, err
}

slog.Warn("config file found", "path", path)

if err := yaml.Unmarshal(content, config); err != nil {
return nil, err
}

return config, nil
}

func (c *Config) Save(path string) error {
marshalled, err := yaml.Marshal(c)
if err != nil {
return err
}

if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}

return os.WriteFile(path, marshalled, 0644)
}

0 comments on commit 4c5f866

Please sign in to comment.