build: minor fixes and build optimization

This commit is contained in:
2025-09-26 11:30:16 +05:30
parent e5a002e4b4
commit df2b446907
6 changed files with 121 additions and 20 deletions

View File

@@ -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.