From a7b55fc080724d11c45e2180ab9d491a2d49fa60 Mon Sep 17 00:00:00 2001 From: Suraj B M Date: Wed, 24 Sep 2025 11:52:25 +0530 Subject: [PATCH] feat: add helper functions for file and db ops and integrate them in commands --- cmd/create.go | 32 ++++------------- cmd/init.go | 3 -- cmd/list.go | 85 ++++++++++++++++------------------------------ cmd/utils/other.go | 82 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 84 deletions(-) create mode 100644 cmd/utils/other.go diff --git a/cmd/create.go b/cmd/create.go index 679155e..a07232d 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,20 +1,14 @@ -/* -Copyright © 2025 NAME HERE -*/ 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 @@ -32,34 +26,22 @@ var createCmd = &cobra.Command{ // file exists, create environment if err == nil { - envyFile, err := os.ReadFile(path.Join(cwd, ".envy")) + envy, err := utils.GetEnvy() if err != nil { - utils.ErrPrint("Error occured while reading .envy file:", err.Error()) + utils.ErrPrint(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) + project := envy.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 - } + dbProject, err := utils.GetProject(project) + if err != nil { + utils.ErrPrint(err.Error()) + return } environment := "" diff --git a/cmd/init.go b/cmd/init.go index cae5505..68c729e 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,6 +1,3 @@ -/* -Copyright © 2025 NAME HERE -*/ package cmd import ( diff --git a/cmd/list.go b/cmd/list.go index 76d5153..bbbeec8 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,19 +1,11 @@ -/* -Copyright © 2025 NAME HERE -*/ package cmd import ( "envy/cmd/utils" - "errors" "fmt" - "os" - "path" "github.com/charmbracelet/lipgloss" - "github.com/pelletier/go-toml" "github.com/spf13/cobra" - "gorm.io/gorm" ) // listCmd represents the list command @@ -21,62 +13,43 @@ var listCmd = &cobra.Command{ Use: "list", Short: "List all the environments in the current project", Run: func(cmd *cobra.Command, args []string) { - cwd, err := os.Getwd() - if err != nil { - utils.ErrPrint("Not able to fetch working directory:", err.Error()) + envy, err := utils.GetEnvy() + if err != nil { + utils.ErrPrint(err.Error()) return } - _, err = os.Stat(path.Join(cwd, ".envy")) + project := envy.Get("envy.project").(string) + pinned := envy.Get("envy.env").(string) - // file exists - if err == nil { - tree, err := toml.LoadFile(path.Join(cwd, ".envy")) - if err != nil { - utils.ErrPrint("Failed to parse .envy file:", err.Error()) - return - } + if project == "" { + utils.ErrPrint("Project name is empty. .envy file might have been modified.") + return + } - project := tree.Get("envy.project").(string) - pinned := tree.Get("envy.env").(string) + if pinned == "" { + utils.ErrPrint("Pinned environment is empty. .envy file might have been modified.") + return + } - if project == "" { - utils.ErrPrint("Project name is empty. .envy file might have been modified.") - return - } + dbProject, err := utils.GetProject(project) + if err != nil { + utils.ErrPrint(err.Error()) + return + } - if pinned == "" { - utils.ErrPrint("Pinned environment is empty. .envy file might have been modified.") - return - } + envs, err := utils.GetEnvironments(dbProject.ID) + if err != nil { + utils.ErrPrint(err.Error()) + 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 - } - } - - var envs []utils.Environment - result = utils.DB.Where("project_id = ?", dbProject.ID).Find(&envs) - - if result.Error != nil { - utils.ErrPrint("Error occured:", result.Error.Error()) - return - } - - pinStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#0FF")) - for _, en := range envs { - if en.Name == pinned { - fmt.Println(" * " + en.Name + pinStyle.Render(" [pinned]")) - } else { - fmt.Println(" * " + en.Name) - } + pinStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#0FF")) + for _, en := range envs { + if en.Name == pinned { + fmt.Println(" * " + en.Name + pinStyle.Render(" [pinned]")) + } else { + fmt.Println(" * " + en.Name) } } }, diff --git a/cmd/utils/other.go b/cmd/utils/other.go new file mode 100644 index 0000000..b1db369 --- /dev/null +++ b/cmd/utils/other.go @@ -0,0 +1,82 @@ +package utils + +import ( + "errors" + "os" + "path" + + "github.com/pelletier/go-toml" + "gorm.io/gorm" +) + +func GetEnvy() (*toml.Tree, error) { + cwd, err := os.Getwd() + if err != nil { + return nil, errors.New("Not able to fetch working directory: " + err.Error()) + } + + _, err = os.Stat(path.Join(cwd, ".envy")) + + // file exists + if err == nil { + envyFile, err := toml.LoadFile(path.Join(cwd, ".envy")) + if err != nil { + return nil, errors.New("Error occured while reading .envy file:" + err.Error()) + } + return envyFile, nil + } + + // file does not exist + if os.IsNotExist(err) { + return nil, errors.New(".envy file doesn't exist in the current directory.") + } else { + return nil, errors.New("Some error occured:" + err.Error()) + } +} + + +func GetProject(name string) (Project, error) { + InitDb() + + var dbProject Project + result := DB.Where("name = ?", name).First(&dbProject) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return Project{}, errors.New("Project with the name " + name + " not found in the database.") + } else { + return Project{}, errors.New("Some error occured: " + result.Error.Error()) + } + } + return dbProject, nil +} + + +func GetEnvironments(project uint) ([]Environment, error) { + InitDb() + + var dbEnvs []Environment + result := DB.Where("project_id = ?", project).Find(&dbEnvs) + + if result.Error != nil { + return nil, errors.New("Some error occured: " + result.Error.Error()) + } + return dbEnvs, nil +} + + +func GetEnvironment(project uint, environment string) (Environment, error) { + InitDb() + + var dbEnv Environment + result := DB.Where("project_id = ? AND name = ?", project, environment).First(&dbEnv) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return Environment{}, errors.New("Environment with the name " + environment + " not found in the database.") + } else { + return Environment{}, errors.New("Some error occured: " + result.Error.Error()) + } + } + return dbEnv, nil +}