Files
envy/cmd/create.go

157 lines
3.6 KiB
Go

package cmd
import (
"envy/cmd/utils"
"fmt"
"os"
"path"
"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a project/environment in the current directory",
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
if err != nil {
utils.ErrPrint("Not able to fetch working directory:", err.Error())
return
}
_, err = os.Stat(path.Join(cwd, "envy.toml"))
// file exists, create environment
if err == nil {
envy, err := utils.GetEnvy()
if err != nil {
utils.ErrPrint(err.Error())
return
}
project, err := utils.GetKey(envy, "envy.project")
utils.StopIfErr(err)
dbProject, err := utils.GetProject(project)
if err != nil {
utils.ErrPrint(err.Error())
return
}
environment := ""
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Environment name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&environment),
),
)
err = form.Run()
if err != nil {
utils.ErrPrint("Some error occured:", err.Error())
return
}
envFile, err := os.ReadFile(path.Join(cwd, ".env"))
if err != nil {
utils.ErrPrint("Error occured while opening .env:", err.Error())
return
}
env := utils.Environment{Name: environment, ProjectID: dbProject.ID, Data: utils.ParseEnv(string(envFile))}
utils.DB.Create(&env)
envy.Set("envy.current", environment)
err = utils.WriteEnvy(envy)
utils.StopIfErr(err)
err = utils.RefactorEnv()
utils.StopIfErr(err)
fmt.Printf("Created environment %s for project %s.\n", environment, project)
}
// file doesn't exist, create project
if os.IsNotExist(err) {
_, err = os.Stat(path.Join(cwd, ".env"))
if os.IsNotExist(err) {
utils.ErrPrint(".env file does not exist in the current directory.")
return
}
project := ""
environment := ""
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Project name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&project),
huh.NewInput().
Title("Environment name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&environment),
),
)
err := form.Run()
if err != nil {
utils.ErrPrint("Some error occured:", err.Error())
}
if project == "" || environment == "" {
return
}
envFile, err := os.ReadFile(path.Join(cwd, ".env"))
if err != nil {
utils.ErrPrint("Error occured while opening .env:", err.Error())
return
}
utils.InitDb()
proj := utils.Project{Name: project}
utils.DB.Create(&proj)
env := utils.Environment{Name: environment, ProjectID: proj.ID, Data: utils.ParseEnv(string(envFile))}
utils.DB.Create(&env)
filePath := path.Join(cwd, "envy.toml")
err = os.WriteFile(filePath, []byte(fmt.Sprintf("[envy]\nproject = \"%s\"\nenv = \"%s\"\ncurrent = \"%s\"\n", project, environment, environment)), 0644)
utils.StopIfErr(err)
err = utils.RefactorEnv()
utils.StopIfErr(err)
fmt.Printf("Created environment %s inside of project %s.\n", environment, project)
}
},
}
func init() {
rootCmd.AddCommand(createCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}