feat: add helper functions for file and db ops and integrate them in

commands
This commit is contained in:
2025-09-24 11:52:25 +05:30
parent 452fded733
commit a7b55fc080
4 changed files with 118 additions and 84 deletions

View File

@@ -1,20 +1,14 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd 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
@@ -32,34 +26,22 @@ var createCmd = &cobra.Command{
// file exists, create environment // file exists, create environment
if err == nil { if err == nil {
envyFile, err := os.ReadFile(path.Join(cwd, ".envy")) envy, err := utils.GetEnvy()
if err != nil { if err != nil {
utils.ErrPrint("Error occured while reading .envy file:", err.Error()) utils.ErrPrint(err.Error())
return return
} }
tree, err := toml.Load(string(envyFile)) project := envy.Get("envy.project").(string)
if err != nil {
utils.ErrPrint("Failed to parse .envy file:", err.Error())
return
}
project := tree.Get("envy.project").(string)
if project == "" { if project == "" {
utils.ErrPrint("Project name is empty. .envy file might have been modified.") utils.ErrPrint("Project name is empty. .envy file might have been modified.")
return return
} }
utils.InitDb() dbProject, err := utils.GetProject(project)
if err != nil {
var dbProject utils.Project utils.ErrPrint(err.Error())
result := utils.DB.Where("name = ?", project).First(&dbProject) return
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 := "" environment := ""

View File

@@ -1,6 +1,3 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd package cmd
import ( import (

View File

@@ -1,19 +1,11 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd package cmd
import ( import (
"envy/cmd/utils" "envy/cmd/utils"
"errors"
"fmt" "fmt"
"os"
"path"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/pelletier/go-toml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gorm.io/gorm"
) )
// listCmd represents the list command // listCmd represents the list command
@@ -21,62 +13,43 @@ var listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List all the environments in the current project", Short: "List all the environments in the current project",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd() envy, err := utils.GetEnvy()
if err != nil { if err != nil {
utils.ErrPrint("Not able to fetch working directory:", err.Error()) utils.ErrPrint(err.Error())
return return
} }
_, err = os.Stat(path.Join(cwd, ".envy")) project := envy.Get("envy.project").(string)
pinned := envy.Get("envy.env").(string)
// file exists if project == "" {
if err == nil { utils.ErrPrint("Project name is empty. .envy file might have been modified.")
tree, err := toml.LoadFile(path.Join(cwd, ".envy")) return
if err != nil { }
utils.ErrPrint("Failed to parse .envy file:", err.Error())
return
}
project := tree.Get("envy.project").(string) if pinned == "" {
pinned := tree.Get("envy.env").(string) utils.ErrPrint("Pinned environment is empty. .envy file might have been modified.")
return
}
if project == "" { dbProject, err := utils.GetProject(project)
utils.ErrPrint("Project name is empty. .envy file might have been modified.") if err != nil {
return utils.ErrPrint(err.Error())
} return
}
if pinned == "" { envs, err := utils.GetEnvironments(dbProject.ID)
utils.ErrPrint("Pinned environment is empty. .envy file might have been modified.") if err != nil {
return utils.ErrPrint(err.Error())
} return
}
utils.InitDb() pinStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#0FF"))
for _, en := range envs {
var dbProject utils.Project if en.Name == pinned {
result := utils.DB.Where("name = ?", project).First(&dbProject) fmt.Println(" * " + en.Name + pinStyle.Render(" [pinned]"))
} else {
if result.Error != nil { fmt.Println(" * " + en.Name)
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)
}
} }
} }
}, },

82
cmd/utils/other.go Normal file
View File

@@ -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
}