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
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 := ""

View File

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

View File

@@ -1,19 +1,11 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
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)
}
}
},

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
}