mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: add helper functions for file and db ops and integrate them in
commands
This commit is contained in:
@@ -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,35 +26,23 @@ 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)
|
|
||||||
|
|
||||||
if result.Error != nil {
|
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
||||||
utils.ErrPrint("Project with the name", project, "not found in the database.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
environment := ""
|
environment := ""
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
/*
|
|
||||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
|
||||||
*/
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
47
cmd/list.go
47
cmd/list.go
@@ -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,24 +13,14 @@ 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 err == nil {
|
|
||||||
tree, err := toml.LoadFile(path.Join(cwd, ".envy"))
|
|
||||||
if err != nil {
|
|
||||||
utils.ErrPrint("Failed to parse .envy file:", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
project := tree.Get("envy.project").(string)
|
|
||||||
pinned := tree.Get("envy.env").(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.")
|
||||||
@@ -50,23 +32,15 @@ var listCmd = &cobra.Command{
|
|||||||
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)
|
|
||||||
|
|
||||||
if result.Error != nil {
|
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
||||||
utils.ErrPrint("Project with the name", project, "not found in the database.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var envs []utils.Environment
|
envs, err := utils.GetEnvironments(dbProject.ID)
|
||||||
result = utils.DB.Where("project_id = ?", dbProject.ID).Find(&envs)
|
if err != nil {
|
||||||
|
utils.ErrPrint(err.Error())
|
||||||
if result.Error != nil {
|
|
||||||
utils.ErrPrint("Error occured:", result.Error.Error())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +52,6 @@ var listCmd = &cobra.Command{
|
|||||||
fmt.Println(" * " + en.Name)
|
fmt.Println(" * " + en.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
82
cmd/utils/other.go
Normal file
82
cmd/utils/other.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user