diff --git a/cmd/init.go b/cmd/init.go index d0d00e5..d18fbb9 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -15,8 +15,10 @@ package cmd import ( - "fmt" + "os" + log "github.com/Sirupsen/logrus" + "github.com/jjdekker/ponder/helpers" "github.com/spf13/cobra" ) @@ -31,11 +33,40 @@ var initCmd = &cobra.Command{ * If no name is provided, the current directory will be assumed; Init will not use an existing directory with contents.`, Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println("init called") + var path string + var err error + switch len(args) { + case 0: + path, err = helpers.CleanPath("") + case 1: + path, err = helpers.CleanPath(args[0]) + default: + log.Fatal("init command does not support more than 1 parameter") + } + if err != nil { + log.WithFields(log.Fields{"error": err}).Fatal("Given path is invalid") + } + + initializePath(path) }, } +func initializePath(path string) { + b, err := helpers.Exists(path) + if err != nil { + log.WithFields(log.Fields{"error": err}).Fatal("Error while checking file") + } + if !b { + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + log.WithFields(log.Fields{"error": err}).Fatal("Could not create directory") + } + } + + // createSettings() + // createGitIgnore() +} + func init() { RootCmd.AddCommand(initCmd) diff --git a/helpers/file_utils.go b/helpers/file_utils.go index 0872d67..be78b79 100644 --- a/helpers/file_utils.go +++ b/helpers/file_utils.go @@ -37,3 +37,15 @@ func CleanPath(path string) (string, error) { return "", err } } + +// Exists checks if a file or directory exists. +func Exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +}