From 8ed73184bd09ce24a65e96eb94a90c505fcfa35e Mon Sep 17 00:00:00 2001 From: s4tyendra Date: Wed, 1 Jan 2025 23:16:37 +0530 Subject: [PATCH] Fixed some bugs --- install.sh | 3 +- main.go | 1 - outputs.go | 24 ++++++++-- profile_init.go | 121 +++++++++++++++++++++--------------------------- profile_list.go | 2 +- profile_load.go | 72 ---------------------------- 6 files changed, 75 insertions(+), 148 deletions(-) delete mode 100644 profile_load.go diff --git a/install.sh b/install.sh index f01dd74..8c2961f 100644 --- a/install.sh +++ b/install.sh @@ -55,7 +55,8 @@ install_binary() { return 1 fi - echo "Successfully installed envman for architecture: $arch" + echo "Successfully installed envman for architecture: $arch \n Initialize with: envman init" + envman init return 0 } diff --git a/main.go b/main.go index 52d56a3..5c32e1d 100644 --- a/main.go +++ b/main.go @@ -105,7 +105,6 @@ func main() { } return } - if len(os.Args) > 1 && os.Args[1] == "init" { shell := detectShell() forShell := false diff --git a/outputs.go b/outputs.go index 4f1778c..09cc51e 100644 --- a/outputs.go +++ b/outputs.go @@ -5,7 +5,6 @@ import "fmt" const ( Version = "v0.1.0" ProjectName = "envman" - configDirName = ".envman" configFileName = "config" ) @@ -17,16 +16,31 @@ Usage: Available Commands: init Initialize envman in your shell + profile Manage environment profiles + load Load a profile into the current shell + +Profile Subcommands: create Create a new environment profile edit Edit an existing environment profile show Display profile contents delete Delete an environment profile list List all available profiles - load Load a profile into current shell + +Examples: + # Initialize envman + $ envman init + + # Profile Management + $ envman profile create server-test # Create new profile + $ envman profile list # List all profiles + $ envman profile show server-test # Show profile contents + $ envman profile edit server-test # Edit existing profile + $ envman profile delete server-test # Delete profile + + # Load Profile + $ envman load server-test # Load profile into current shell Flags: -h, --help Display help information -v, --version Display version information - -Use "%s [command] --help" for more information about a command. -`, ProjectName, Version, ProjectName, ProjectName) +`, ProjectName, Version, ProjectName) diff --git a/profile_init.go b/profile_init.go index 4c761e5..ade738e 100644 --- a/profile_init.go +++ b/profile_init.go @@ -78,12 +78,14 @@ func (m InitModel) View() string { if m.succeeded { if m.selected == "a" { - return fmt.Sprintf("%s%s%sSuccess:%s Configuration appended to %s\n", + return fmt.Sprintf("%s%s%sSuccess:%s Configuration appended to %s\nPlease run\n❯ eval \"$(envman init - %s)\" \nor\n❯ source %s\nor Restart your shell\n", colorGreen, colorBold, iconCheck, colorReset, m.rcFile, + m.shell, + m.rcFile, ) } if m.selected == "c" { @@ -119,51 +121,10 @@ func (m InitModel) View() string { func getEnvmanBlock(shell string) string { currentTime := time.Now().UTC().Format("2006-01-02 15:04:05") - envmanRoot, err := getEnvmanRoot() - if err != nil { - fmt.Println("Error getting envman root directory: ", err) - os.Exit(1) - } - switch shell { - case "bash", "zsh": - return fmt.Sprintf(`# START >>>>>>>>>>>> envman Managed [%s] <<<<<<<<<<<<<<< -envman() { - if [ "$1" = "load" ]; then - if [ -z "$2" ]; then - echo "Usage: envman load " >&2 - return 1 - fi - if [ ! -f "%s/$2.env" ]; then - echo "Profile not found: $2" >&2 - return 1 - fi - source "%s/$2.env" - else - command envman "$@" - fi -} -# END >>>>>>>>>>>> envman Managed <<<<<<<<<<<<<<<`, currentTime, envmanRoot, envmanRoot) - case "fish": - return fmt.Sprintf(`# START >>>>>>>>>>>> envman Managed [%s] <<<<<<<<<<<<<<< -function envman - if [ "$argv[1]" = "load" ] - if [ -z "$argv[2]" ] - echo "Usage: envman load " >&2 - return 1 - end - if not test -f "%s/$argv[2].env" - echo "Profile not found: $argv[2]" >&2 - return 1 - end - source "%s/$argv[2].env" - else - command envman $argv - end -end -# END >>>>>>>>>>>> envman Managed <<<<<<<<<<<<<<<`, currentTime, envmanRoot, envmanRoot) - default: - return fmt.Sprintf("Unsupported shell: %s\n", shell) - } + return fmt.Sprintf(` +# START >>>>>>>>>>>> envman Managed [%s] <<<<<<<<<<<<<<< +eval "$(envman init - %s)" +# END >>>>>>>>>>>> envman Managed <<<<<<<<<<<<<<<`, currentTime, shell) } func (m InitModel) appendToRC() error { @@ -175,8 +136,7 @@ func (m InitModel) appendToRC() error { } existing, err := os.ReadFile(rcPath) - if err == nil && strings.Contains(string(existing), fmt.Sprintf(`envman() { - if [ "$1" = "load" ]; then`)) { + if err == nil && strings.Contains(string(existing), fmt.Sprintf(`eval "$(envman init - %s)"`, m.shell)) { return fmt.Errorf("envman configuration already exists in %s", m.rcFile) } @@ -383,36 +343,61 @@ func init() { fmt.Println("Error getting envman root directory: ", err) os.Exit(1) } - bashInitScript = fmt.Sprintf(`envman() { - if [ "$1" = "load" ]; then - if [ -z "$2" ]; then + bashInitScript = fmt.Sprintf(`export ENVMAN_ROOT="%s" + +envman() { + local command + command="$1" + if [ "$#" -gt 0 ]; then + shift + fi + + case "$command" in + load) + if [ "$#" -eq 0 ]; then echo "Usage: envman load " >&2 return 1 fi - if [ ! -f "%s/$2.env" ]; then - echo "Profile not found: $2" >&2 + + local profile_path="$ENVMAN_ROOT/$1.env" + if [ -f "$profile_path" ]; then + source "$profile_path" + echo "Loaded profile: $1" + else + echo "Profile not found: $1" >&2 return 1 fi - source "%s/$2.env" - else - command envman "$@" - fi + ;; + *) + command envman "$command" "$@" + ;; + esac } -`, envmanRoot, envmanRoot) - fishInitScript = fmt.Sprintf(`function envman - if [ "$argv[1]" = "load" ] - if [ -z "$argv[2]" ] +`, envmanRoot) + fishInitScript = fmt.Sprintf(`set -gx ENVMAN_ROOT "%s" + +function envman + set -l command $argv[1] + set -e argv[1] + + switch "$command" + case "load" + if test (count $argv) -eq 0 echo "Usage: envman load " >&2 return 1 - end - if not test -f "%s/$argv[2].env" - echo "Profile not found: $argv[2]" >&2 + end + + set -l profile_path "$ENVMAN_ROOT/$argv[1].env" + if test -f "$profile_path" + source "$profile_path" + echo "Loaded profile: $argv[1]" + else + echo "Profile not found: $argv[1]" >&2 return 1 end - source "%s/$argv[2].env" - else - command envman $argv + case '*' + command envman "$command" $argv end end -`, envmanRoot, envmanRoot) +`, envmanRoot) } diff --git a/profile_list.go b/profile_list.go index fce91bd..0b5b7ff 100644 --- a/profile_list.go +++ b/profile_list.go @@ -83,7 +83,7 @@ func (m ListProfileModel) View() string { for _, p := range m.profiles { profileName := strings.TrimSuffix(p.name, ".env") - output.WriteString(fmt.Sprintf("%s %-20s %s%-8d%s %-19s %-19s\n", + output.WriteString(fmt.Sprintf("%s %-20s %s%-8d%s %-19s\n", colorBold, profileName, colorGreen, diff --git a/profile_load.go b/profile_load.go deleted file mode 100644 index 7489d75..0000000 --- a/profile_load.go +++ /dev/null @@ -1,72 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "os" - "os/user" - "path/filepath" - "strings" -) - -func LoadProfile(name string) error { - if name == "" { - return fmt.Errorf("profile name cannot be empty") - } - - currentUser, err := user.Current() - if err != nil { - return fmt.Errorf("failed to get current user: %v", err) - } - - configPath := filepath.Join("/home", currentUser.Username, ".config", ProjectName, configFileName) - configContent, err := os.ReadFile(configPath) - if err != nil { - return fmt.Errorf("failed to read config file: %v", err) - } - - var profileDir string - for _, line := range strings.Split(string(configContent), "\n") { - if strings.HasPrefix(line, "PROFILE_DIR=") { - profileDir = strings.TrimPrefix(line, "PROFILE_DIR=") - profileDir = strings.Split(profileDir, "#")[0] - profileDir = strings.TrimSpace(profileDir) - break - } - } - - if profileDir == "" { - return fmt.Errorf("PROFILE_DIR not found in config") - } - - profilePath := filepath.Join(profileDir, name+".env") - file, err := os.Open(profilePath) - if err != nil { - return fmt.Errorf("failed to open profile: %v", err) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := scanner.Text() - line = strings.TrimSpace(line) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - parts := strings.SplitN(line, "=", 2) - if len(parts) == 2 { - key := strings.TrimSpace(parts[0]) - value := strings.TrimSpace(parts[1]) - if key != "" { - os.Setenv(key, value) - } - } - } - - if err := scanner.Err(); err != nil { - return fmt.Errorf("error reading profile: %v", err) - } - - fmt.Printf("Loaded profile: %s\n", name) - return nil -}