mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
build: minor fixes and build optimization
This commit is contained in:
52
cmd/root.go
52
cmd/root.go
@@ -4,8 +4,12 @@ Copyright © 2025 Suraj B M <silicoflare@gmail.com>
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"envy/cmd/utils"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -15,7 +19,53 @@ var rootCmd = &cobra.Command{
|
||||
Short: "A CLI tool to manage .env files for your projects",
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cwd, err := os.Getwd()
|
||||
utils.StopIfErr(err)
|
||||
|
||||
if _, err := os.Stat(path.Join(cwd, ".env")); os.IsNotExist(err) {
|
||||
cmd.Help()
|
||||
return
|
||||
}
|
||||
|
||||
env, err := os.ReadFile(path.Join(cwd, ".env"))
|
||||
utils.StopIfErr(err)
|
||||
|
||||
envs := utils.ParseEnvToStruct(string(env))
|
||||
|
||||
var lines []int
|
||||
|
||||
form := huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewMultiSelect[int]().
|
||||
Title("Enable or disable environment variables").
|
||||
OptionsFunc(func() []huh.Option[int] {
|
||||
var opts []huh.Option[int]
|
||||
for i, en := range envs {
|
||||
opts = append(opts,
|
||||
huh.NewOption(fmt.Sprintf("%s = %s", en.Key, en.Value), i).
|
||||
Selected(en.Enabled))
|
||||
}
|
||||
return opts
|
||||
}, &envs).
|
||||
Value(&lines),
|
||||
),
|
||||
)
|
||||
|
||||
err = form.Run()
|
||||
utils.StopIfErr(err)
|
||||
|
||||
for i := range envs {
|
||||
envs[i].Enabled = false
|
||||
}
|
||||
for _, v := range lines {
|
||||
envs[v].Enabled = true
|
||||
}
|
||||
|
||||
envStr := utils.ParseEnvStruct(envs)
|
||||
|
||||
os.WriteFile(path.Join(cwd, ".env"), []byte(envStr), 0755)
|
||||
},
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
||||
Reference in New Issue
Block a user