package cmd import ( "fmt" "strings" "git.dayanhub.com/sagi/envoid/internal/datastore" "git.dayanhub.com/sagi/envoid/internal/prompt" "git.dayanhub.com/sagi/envoid/internal/variables" "github.com/spf13/cobra" ) var initCmd = &cobra.Command{ Use: "init", Short: "creates a new project for this path. default env is set to `local`", Long: "", RunE: func(cmd *cobra.Command, args []string) error { _, err := configuration.GetProject(workingDir) if err == nil { fmt.Printf("Project already exists. please remove if you wish to override\n") return nil } ds, err := datastore.NewDataStore() if err != nil { return err } defer ds.Close() var name string var pass string var envNames []string // check if we are importing an existing file if ds.DoesFileExists() { fmt.Printf("Project was not found locally. But found %s file in path.\n", variables.DBFileName) fmt.Printf("Importing Project for %s\n", workingDir) name = prompt.StringPrompt("Project Name:") pass = prompt.PasswordPrompt("Project Password (Same password that was used originally):") envNames, err = ds.ListEnvironments() if err != nil { return err } fmt.Printf("Found %d environments. importing %v\n", len(envNames), envNames) } else { // New Project fmt.Printf("Creating new Project @ %s\n", workingDir) name = prompt.StringPrompt("Project Name:") pass = prompt.PasswordPrompt("Project Password:") envs := strings.TrimSpace(prompt.StringPrompt("Please provide environment names separated by `,`. (stage,prod):")) if len(envs) == 0 { envs = "local" } envNames = strings.Split(envs, ",") } project, err = configuration.NewProject(name, workingDir, pass) if err != nil { return err } for _, eName := range envNames { if err := project.NewEnv(eName); err != nil { return err } if err := ds.CreateEnv(eName); err != nil { return err } } configuration.Save() fmt.Println("✅ Done") return nil }, } func init() { }