🐛 Ensure config directory exists
This commit is contained in:
parent
bc6c3d0f36
commit
0687671c7d
1 changed files with 11 additions and 4 deletions
|
|
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue