mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: implement export command
This commit is contained in:
103
cmd/export.go
Normal file
103
cmd/export.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"envy/cmd/utils"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/huh"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// exportCmd represents the export command
|
||||||
|
var exportCmd = &cobra.Command{
|
||||||
|
Use: "export [env-name]",
|
||||||
|
Short: "Export an environment as a .env file",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
all, err := cmd.Flags().GetBool("all")
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
env := ""
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
env = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
envy, err := utils.GetEnvy()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
project, err := utils.GetKey(envy, "envy.project")
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
dbProject, err := utils.GetProject(project)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
if all {
|
||||||
|
dbEnvs, err := utils.GetEnvironments(dbProject.ID)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
for _, en := range dbEnvs {
|
||||||
|
err = os.WriteFile(path.Join(cwd, ".env."+en.Name), []byte(en.Data), 0755)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if env != "" {
|
||||||
|
dbEnv, err := utils.GetEnvironment(dbProject.ID, env)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
err = os.WriteFile(path.Join(cwd, ".env."+env), []byte(dbEnv.Data), 0755)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
} else {
|
||||||
|
dbEnvs, err := utils.GetEnvironments(dbProject.ID)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
var selected *utils.Environment
|
||||||
|
|
||||||
|
form := huh.NewForm(
|
||||||
|
huh.NewGroup(
|
||||||
|
huh.NewSelect[*utils.Environment]().
|
||||||
|
Title("Select an environment").
|
||||||
|
OptionsFunc(func() []huh.Option[*utils.Environment] {
|
||||||
|
var options []huh.Option[*utils.Environment]
|
||||||
|
for _, en := range dbEnvs {
|
||||||
|
options = append(options, huh.NewOption(en.Name, &en))
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}, &dbEnvs).
|
||||||
|
Value(&selected),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
err = form.Run()
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
|
||||||
|
err = os.WriteFile(path.Join(cwd, ".env."+selected.Name), []byte(selected.Data), 0755)
|
||||||
|
utils.StopIfErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(exportCmd)
|
||||||
|
|
||||||
|
exportCmd.Flags().BoolP("all", "a", false, "Export all environments")
|
||||||
|
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
// and all subcommands, e.g.:
|
||||||
|
// exportCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||||
|
|
||||||
|
// Cobra supports local flags which will only run when this command
|
||||||
|
// is called directly, e.g.:
|
||||||
|
// exportCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user