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