mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: implement create command
This commit is contained in:
103
cmd/create.go
Normal file
103
cmd/create.go
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
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"))
|
||||
|
||||
if os.IsExist(err) {
|
||||
return
|
||||
} else {
|
||||
_, 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())
|
||||
}
|
||||
|
||||
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, Active: true, 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")
|
||||
}
|
||||
Reference in New Issue
Block a user