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")
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func InitDb() {
|
||||
var openErr error
|
||||
DB, openErr = gorm.Open(sqlite.Open(path.Join(EnvyPath, "envy.db")), &gorm.Config{})
|
||||
if openErr != nil {
|
||||
ErrPrint("Error while opening database:" + openErr.Error())
|
||||
ErrPrint("Error while opening database:", openErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#F00"))
|
||||
|
||||
func ErrPrint(data string) {
|
||||
fmt.Println(ErrStyle.Render(data))
|
||||
func ErrPrint(data ...string) {
|
||||
fmt.Println(ErrStyle.Render(strings.Join(data, " ")))
|
||||
}
|
||||
|
||||
var EnvyPath string
|
||||
@@ -24,3 +26,19 @@ func Init() {
|
||||
|
||||
EnvyPath = path.Join(conf, "envy")
|
||||
}
|
||||
|
||||
func NoSpace(input string) error {
|
||||
for _, r := range input {
|
||||
if !(isAlphaNum(r) || r == '-' || r == '_') {
|
||||
return errors.New("input can only contain letters, numbers, '-' and '_'")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// helper function
|
||||
func isAlphaNum(r rune) bool {
|
||||
return (r >= 'a' && r <= 'z') ||
|
||||
(r >= 'A' && r <= 'Z') ||
|
||||
(r >= '0' && r <= '9')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user