From 4f63215fba987a1f7d07217dae66914abf2d99b1 Mon Sep 17 00:00:00 2001 From: Suraj B M Date: Tue, 23 Sep 2025 16:26:31 +0530 Subject: [PATCH] feat: implement create command for environments --- cmd/create.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++--- go.mod | 1 + go.sum | 2 ++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 668e503..11da968 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -5,13 +5,16 @@ package cmd import ( "envy/cmd/utils" + "errors" "fmt" "log" "os" "path" "github.com/charmbracelet/huh" + "github.com/pelletier/go-toml" "github.com/spf13/cobra" + "gorm.io/gorm" ) // createCmd represents the create command @@ -27,9 +30,70 @@ var createCmd = &cobra.Command{ _, err = os.Stat(path.Join(cwd, ".envy")) - if os.IsExist(err) { - return - } else { + // file exists, create environment + if err == nil { + envyFile, err := os.ReadFile(path.Join(cwd, ".envy")) + if err != nil { + utils.ErrPrint("Error occured while reading .envy file:", err.Error()) + return + } + + tree, err := toml.Load(string(envyFile)) + if err != nil { + utils.ErrPrint("Failed to parse .envy file:", err.Error()) + return + } + + project := tree.Get("envy.project").(string) + if project == "" { + utils.ErrPrint("Project name is empty. .envy file might have been modified.") + return + } + + utils.InitDb() + + var dbProject utils.Project + result := utils.DB.Where("name = ?", project).First(&dbProject) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + utils.ErrPrint("Project with the name", project, "not found in the database.") + 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, Active: false, Data: string(envFile)} + utils.DB.Create(&env) + + 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) { @@ -61,6 +125,10 @@ var createCmd = &cobra.Command{ 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()) diff --git a/go.mod b/go.mod index 9490a96..f34493c 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/termenv v0.16.0 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.9 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index 358dc7a..780ad02 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,8 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=