Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/blueprint/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
73 changes: 50 additions & 23 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
_ "embed"
"errors"
"fmt"
"os"

Expand All @@ -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.
Expand All @@ -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
}

Expand Down