🐛 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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
fp "path/filepath"
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/config"
|
"codeberg.org/danjones000/my-log/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -30,22 +31,28 @@ var configCmd = &cobra.Command{
|
||||||
Short: "Save default config to file",
|
Short: "Save default config to file",
|
||||||
//Long: ``,
|
//Long: ``,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||||
force, _ := cmd.Flags().GetBool("force")
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
if !force {
|
if !force {
|
||||||
_, err := os.Stat(config.ConfigPath)
|
_, err = os.Stat(config.ConfigPath)
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
|
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)
|
f, err := os.Create(config.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
c := config.DefaultStr()
|
c := config.DefaultStr()
|
||||||
fmt.Fprint(f, c)
|
fmt.Fprint(f, c)
|
||||||
return nil
|
return
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue