mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: implement create command for environments
This commit is contained in:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user