haunt/cmd/init.go

55 lines
1.2 KiB
Go
Raw Normal View History

2023-03-04 22:22:03 +00:00
package cmd
import (
"fmt"
"log"
"os"
"strings"
"github.com/abs3ntdev/haunt/src/haunt"
"github.com/spf13/cobra"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
2023-03-08 21:46:44 +00:00
Use: "init",
Aliases: []string{"i"},
Short: "Generates a haunt config file using sane defaults",
Long: "Generates a haunt config file using sane defaults, haunt will look for a main.go file and any directories inside the relative path 'cmd' and add them all as projects",
RunE: defaultConfig,
2023-03-04 22:22:03 +00:00
}
func init() {
rootCmd.AddCommand(initCmd)
}
func defaultConfig(cmd *cobra.Command, args []string) error {
2023-03-06 07:39:01 +00:00
h := haunt.NewHaunt()
2023-03-04 22:22:03 +00:00
write := true
2023-03-06 07:39:01 +00:00
if _, err := os.Stat(haunt.HFile); err == nil {
fmt.Print(h.Prefix("Config file exists. Overwire? " + haunt.Magenta.Bold("[y/n] ") + haunt.Green.Bold("(n) ")))
2023-03-04 22:22:03 +00:00
var overwrite string
fmt.Scanf("%s", &overwrite)
write = false
switch strings.ToLower(overwrite) {
case "y", "ye", "yes":
write = true
}
}
if write {
2023-03-06 07:39:01 +00:00
h.SetDefaults()
err := h.Settings.Write(h)
2023-03-04 22:22:03 +00:00
if err != nil {
return err
}
2023-03-06 07:39:01 +00:00
log.Println(h.Prefix(
2023-03-04 22:22:03 +00:00
"Config file has successfully been saved at .haunt.yaml",
))
2023-03-06 07:39:01 +00:00
log.Println(h.Prefix(
2023-03-04 22:22:03 +00:00
"Run haunt add --help to see how to add more projects",
))
return nil
}
return nil
}