mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: implement pin command
This commit is contained in:
89
cmd/pin.go
Normal file
89
cmd/pin.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"envy/cmd/utils"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/huh"
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// pinCmd represents the pin command
|
||||||
|
var pinCmd = &cobra.Command{
|
||||||
|
Use: "pin",
|
||||||
|
Short: "Pin an environment to the .envy file",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
var env = ""
|
||||||
|
if len(args) > 0 {
|
||||||
|
env = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
envy, err := utils.GetEnvy()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
project := envy.Get("envy.project").(string)
|
||||||
|
|
||||||
|
dbProject, err := utils.GetProject(project)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
if env != "" {
|
||||||
|
_, err := utils.GetEnvironment(dbProject.ID, env)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
} else {
|
||||||
|
envs, err := utils.GetEnvironments(dbProject.ID)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
form := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewSelect[string]().
|
||||||
|
Title("Select environment").
|
||||||
|
Description("Select an environment to pin").
|
||||||
|
OptionsFunc(func () []huh.Option[string] {
|
||||||
|
var options []huh.Option[string]
|
||||||
|
for _, en := range envs {
|
||||||
|
options = append(options, huh.NewOption(en.Name, en.Name))
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}, &env).
|
||||||
|
Value(&env),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
err = form.Run()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
if env == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
envy.Set("envy.env", env)
|
||||||
|
data, err := toml.Marshal(envy)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
os.WriteFile(path.Join(cwd, ".envy"), data, 0744)
|
||||||
|
|
||||||
|
fmt.Println("Pinned", env, "to the project.")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(pinCmd)
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// pinCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// pinCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
||||||
@@ -9,6 +9,13 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func StopIfErr(err error) {
|
||||||
|
if err != nil {
|
||||||
|
ErrPrint(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetEnvy() (*toml.Tree, error) {
|
func GetEnvy() (*toml.Tree, error) {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user