Skip to content

Commit

Permalink
feat(init): allow overwriting of data (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbudde authored Aug 13, 2023
1 parent 561d857 commit f3f5983
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
13 changes: 10 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"errors"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

Expand All @@ -14,12 +16,17 @@ func init() {
Long: "Initialize new myks project",
Run: func(cmd *cobra.Command, args []string) {
g := myks.New(".")

if err := g.Bootstrap(); err != nil {
force, err := cmd.Flags().GetBool("force")
if err != nil {
log.Fatal().Err(err).Msg("Failed to read flag")
}
if err := g.Bootstrap(force); errors.Is(err, myks.ErrNotClean) {
log.Error().Msg("Directory not empty. Use --force to overwrite data.")
} else if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize project")
}
},
}

cmd.Flags().BoolP("force", "f", false, "overwrite existing data")
rootCmd.AddCommand(cmd)
}
42 changes: 22 additions & 20 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var environmentsFs embed.FS

var GlobalLogFormat = "\033[1m[global]\033[0m %s"

var ErrNotClean = fmt.Errorf("target directory is not clean, aborting")

// Define the main structure
type Globe struct {
/// Globe configuration
Expand Down Expand Up @@ -184,9 +186,9 @@ func (g *Globe) SyncAndRender() error {
}

// Bootstrap creates the initial directory structure and files
func (g *Globe) Bootstrap() error {
func (g *Globe) Bootstrap(force bool) error {
log.Info().Msg("Creating base file structure")
if err := g.createBaseFileStructure(); err != nil {
if err := g.createBaseFileStructure(force); err != nil {
return err
}

Expand Down Expand Up @@ -231,7 +233,7 @@ func (g *Globe) dumpConfigAsYaml() (string, error) {
return configFileName, nil
}

func (g *Globe) createBaseFileStructure() error {
func (g *Globe) createBaseFileStructure(force bool) error {
envDir := filepath.Join(g.RootDir, g.EnvironmentBaseDir)
protoDir := filepath.Join(g.RootDir, g.PrototypesDir)
renderedDir := filepath.Join(g.RootDir, g.RenderedDir)
Expand All @@ -244,40 +246,40 @@ func (g *Globe) createBaseFileStructure() error {
log.Debug().Str("data schema file", dataSchemaFile).Msg("")
log.Debug().Str("environments .gitignore file", envsGitignoreFile).Msg("")

// TODO: interactively ask for confirmation and overwrite without checking
notCleanErr := fmt.Errorf("Target directory is not clean, aborting")

if _, err := os.Stat(envDir); err == nil {
return notCleanErr
if !force {
if _, err := os.Stat(envDir); err == nil {
return ErrNotClean
}
if _, err := os.Stat(protoDir); err == nil {
return ErrNotClean
}
if _, err := os.Stat(renderedDir); err == nil {
return ErrNotClean
}
if _, err := os.Stat(dataSchemaFile); err == nil {
return ErrNotClean
}
if _, err := os.Stat(envsGitignoreFile); err == nil {
return ErrNotClean
}
}

if err := os.MkdirAll(envDir, 0o750); err != nil {
return err
}

if _, err := os.Stat(protoDir); err == nil {
return notCleanErr
}
if err := os.MkdirAll(protoDir, 0o750); err != nil {
return err
}

if _, err := os.Stat(renderedDir); err == nil {
return notCleanErr
}
if err := os.MkdirAll(renderedDir, 0o750); err != nil {
return err
}

if _, err := os.Stat(dataSchemaFile); err == nil {
return notCleanErr
}
if err := os.WriteFile(dataSchemaFile, dataSchema, 0o600); err != nil {
return err
}

if _, err := os.Stat(envsGitignoreFile); err == nil {
return notCleanErr
}
if err := os.WriteFile(envsGitignoreFile, envsGitignore, 0o600); err != nil {
return err
}
Expand Down

0 comments on commit f3f5983

Please sign in to comment.