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

@@ -22,7 +22,7 @@ var listCmd = &cobra.Command{
project, err := utils.GetKey(envy, "envy.project")
utils.StopIfErr(err)
pinned, err := utils.GetKey(envy, "envy.pinned")
pinned, err := utils.GetKey(envy, "envy.env")
utils.StopIfErr(err)
current, err := utils.GetKey(envy, "envy.current")

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.

View File

@@ -141,3 +141,47 @@ func ParseEnv(env string) string {
return envs
}
type Env struct {
Enabled bool
Key string
Value string
}
func ParseEnvToStruct(env string) []Env {
re := regexp.MustCompile(`^\s*(#?)\s*([A-Z0-9_]+)\s*=\s*(?:"([^"\n]+)"|([^"\n]+))$`)
var envs []Env
for _, line := range strings.Split(string(env), "\n") {
matches := re.FindStringSubmatch(line)
var env Env
if matches != nil {
env.Enabled = matches[1] == ""
env.Key = matches[2] // the key
env.Value = matches[3] // quoted value
if env.Value == "" {
env.Value = matches[4] // unquoted value
}
envs = append(envs, env)
}
}
return envs
}
func ParseEnvStruct(envs []Env) string {
var env string
for _, en := range envs {
comment := ""
if !en.Enabled {
comment = "#"
}
env += fmt.Sprintf("%v%v=%v\n", comment, en.Key, en.Value)
}
return env
}