🐛 Ensure config directory exists

This commit is contained in:
Dan Jones 2024-02-25 15:53:37 -06:00
commit 0687671c7d

View file

@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"os"
fp "path/filepath"
"codeberg.org/danjones000/my-log/config"
"github.com/spf13/cobra"
@ -30,22 +31,28 @@ var configCmd = &cobra.Command{
Short: "Save default config to file",
//Long: ``,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {
force, _ := cmd.Flags().GetBool("force")
if !force {
_, err := os.Stat(config.ConfigPath)
_, err = os.Stat(config.ConfigPath)
if !os.IsNotExist(err) {
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
}
}
dir := fp.Dir(config.ConfigPath)
err = os.MkdirAll(dir, 0755)
if err != nil {
return
}
f, err := os.Create(config.ConfigPath)
if err != nil {
return err
return
}
defer f.Close()
c := config.DefaultStr()
fmt.Fprint(f, c)
return nil
return
},
}