Files
envy/cmd/create.go

154 lines
3.5 KiB
Go

package cmd
import (
"envy/cmd/utils"
"fmt"
"log"
"os"
"path"
"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a project/environment in the current directory",
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
if err != nil {
utils.ErrPrint("Not able to fetch working directory:", err.Error())
return
}
_, err = os.Stat(path.Join(cwd, ".envy"))
// file exists, create environment
if err == nil {
envy, err := utils.GetEnvy()
if err != nil {
utils.ErrPrint(err.Error())
return
}
project := envy.Get("envy.project").(string)
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
}
environment := ""
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Environment name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&environment),
),
)
err = form.Run()
if err != nil {
utils.ErrPrint("Some error occured:", err.Error())
return
}
envFile, err := os.ReadFile(path.Join(cwd, ".env"))
if err != nil {
utils.ErrPrint("Error occured while opening .env:", err.Error())
return
}
env := utils.Environment{Name: environment, ProjectID: dbProject.ID, Data: string(envFile)}
utils.DB.Create(&env)
fmt.Printf("Created environment %s for project %s.\n", environment, project)
}
// file doesn't exist, create project
if os.IsNotExist(err) {
_, err = os.Stat(path.Join(cwd, ".env"))
if os.IsNotExist(err) {
utils.ErrPrint(".env file does not exist in the current directory.")
return
}
project := ""
environment := ""
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Project name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&project),
huh.NewInput().
Title("Environment name").
Prompt("? ").
Validate(utils.NoSpace).
Value(&environment),
),
)
err := form.Run()
if err != nil {
utils.ErrPrint("Some error occured:", err.Error())
}
if project == "" || environment == "" {
return
}
envFile, err := os.ReadFile(path.Join(cwd, ".env"))
if err != nil {
utils.ErrPrint("Error occured while opening .env:", err.Error())
return
}
utils.InitDb()
proj := utils.Project{Name: project}
utils.DB.Create(&proj)
env := utils.Environment{Name: environment, ProjectID: proj.ID, Data: string(envFile)}
utils.DB.Create(&env)
filePath := path.Join(cwd, ".envy")
err = os.WriteFile(filePath, []byte(fmt.Sprintf("[envy]\nproject = \"%s\"\nenv = \"%s\"\n", project, environment)), 0644)
if err != nil {
log.Println("Error creating .envy file:", err)
return
}
fmt.Printf("Created environment %s inside of project %s.\n", environment, project)
}
},
}
func init() {
rootCmd.AddCommand(createCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}