feat: implement create command for environments

This commit is contained in:
2025-09-23 16:26:31 +05:30
parent a582636e16
commit 4f63215fba
3 changed files with 74 additions and 3 deletions

View File

@@ -5,13 +5,16 @@ package cmd
import ( import (
"envy/cmd/utils" "envy/cmd/utils"
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
"path" "path"
"github.com/charmbracelet/huh" "github.com/charmbracelet/huh"
"github.com/pelletier/go-toml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gorm.io/gorm"
) )
// createCmd represents the create command // createCmd represents the create command
@@ -27,9 +30,70 @@ var createCmd = &cobra.Command{
_, err = os.Stat(path.Join(cwd, ".envy")) _, err = os.Stat(path.Join(cwd, ".envy"))
if os.IsExist(err) { // file exists, create environment
return if err == nil {
} else { 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")) _, err = os.Stat(path.Join(cwd, ".env"))
if os.IsNotExist(err) { if os.IsNotExist(err) {
@@ -61,6 +125,10 @@ var createCmd = &cobra.Command{
utils.ErrPrint("Some error occured:", err.Error()) utils.ErrPrint("Some error occured:", err.Error())
} }
if project == "" || environment == "" {
return
}
envFile, err := os.ReadFile(path.Join(cwd, ".env")) envFile, err := os.ReadFile(path.Join(cwd, ".env"))
if err != nil { if err != nil {
utils.ErrPrint("Error occured while opening .env:", err.Error()) utils.ErrPrint("Error occured while opening .env:", err.Error())

1
go.mod
View File

@@ -35,6 +35,7 @@ require (
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // 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/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.9 // indirect github.com/spf13/pflag v1.0.9 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect

2
go.sum
View File

@@ -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/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 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= 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.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=