diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index 2f8e986..4472b29 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -27,6 +27,11 @@ func NewBluePrint(name, writePath, data string, values any) *BluePrint { } } +// Exists allows you to check if the file within the blueprint exists +func (b *BluePrint) Exists() (os.FileInfo, error) { + return os.Stat(b.WritePath) +} + // Write takes the BluePrint data then templates it out to the filesystem func (b *BluePrint) Write() error { tmpl := template.Must(template.New(b.Name).Parse(b.Data)) diff --git a/internal/commands/root.go b/internal/commands/root.go index d42431e..e98ef3c 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -2,6 +2,7 @@ package commands import ( _ "embed" + "errors" "fmt" "os" @@ -14,31 +15,43 @@ import ( // Execute runs the root command and handles any errors that occur during execution. func Execute() { - if err := rootCmd.Execute(); err != nil { + if err := NewRootCmd().Execute(); err != nil { fmt.Println(err) os.Exit(1) } } -// rootCmd is the entry command into freight -var rootCmd = &cobra.Command{ - Use: "init", - Short: "init", - Long: `init`, - Run: func(cmd *cobra.Command, args []string) { - if err := validate.GitDirExists(); err != nil { - panic(err) - } - if err := setupHooks(); err != nil { - panic(err) - } - if err := setupConfig(); err != nil { - panic(err) - } - if err := installBinary(); err != nil { - panic(err) - } - }, +// NewRootCmd creates and returns the root command for the CLI application. +func NewRootCmd() *cobra.Command { + rootCmd := &cobra.Command{ + Use: "init", + Short: "init", + Long: `init`, + Run: func(cmd *cobra.Command, args []string) { + if err := validate.GitDirExists(); err != nil { + panic(err) + } + if err := setupHooks(); err != nil { + panic(err) + } + + configForce, err := cmd.Flags().GetBool("config-force") + if err != nil { + panic(err) + } + + if err := setupConfig(configForce); err != nil { + panic(err) + } + if err := installBinary(); err != nil { + panic(err) + } + }, + } + + rootCmd.Flags().BoolP("config-force", "c", false, "If you wish to force write the config") + + return rootCmd } // setupHooks initializes and writes the Git hooks. @@ -62,12 +75,26 @@ func setupHooks() error { } // setupConfig creates and writes the configuration file. -func setupConfig() error { +func setupConfig(forceWrite bool) error { fmt.Println("Generating config file") fmt.Println("=========================") - if err := blueprint.NewConfig().Write(); err != nil { - return err + + config := blueprint.NewConfig() + + if !forceWrite { + if _, err := config.Exists(); err != nil { + if errors.Is(err, os.ErrNotExist) { + forceWrite = true + } + } } + + if forceWrite { + if err := blueprint.NewConfig().Write(); err != nil { + return err + } + } + return nil }