Skip to content

Commit

Permalink
Fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
S4tyendra committed Jan 1, 2025
1 parent 81f2881 commit 453c247
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 49 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func main() {
}
return
}

if len(os.Args) > 1 && os.Args[1] == "init" {
shell := detectShell()
forShell := false
Expand Down
116 changes: 67 additions & 49 deletions profile_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,51 @@ func (m InitModel) View() string {

func getEnvmanBlock(shell string) string {
currentTime := time.Now().UTC().Format("2006-01-02 15:04:05")
return fmt.Sprintf(`# START >>>>>>>>>>>> envman Managed [%s] <<<<<<<<<<<<<<<
eval "$(envman init - %s)"
# END >>>>>>>>>>>> envman Managed <<<<<<<<<<<<<<<`, currentTime, shell)
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 <profile>" >&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 <profile>" >&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)
}
}

func (m InitModel) appendToRC() error {
Expand All @@ -133,7 +175,8 @@ func (m InitModel) appendToRC() error {
}

existing, err := os.ReadFile(rcPath)
if err == nil && strings.Contains(string(existing), fmt.Sprintf(`eval "$(envman init - %s)"`, m.shell)) {
if err == nil && strings.Contains(string(existing), fmt.Sprintf(`envman() {
if [ "$1" = "load" ]; then`)) {
return fmt.Errorf("envman configuration already exists in %s", m.rcFile)
}

Expand Down Expand Up @@ -340,61 +383,36 @@ func init() {
fmt.Println("Error getting envman root directory: ", err)
os.Exit(1)
}
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
bashInitScript = fmt.Sprintf(`envman() {
if [ "$1" = "load" ]; then
if [ -z "$2" ]; then
echo "Usage: envman load <profile>" >&2
return 1
fi
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
if [ ! -f "%s/$2.env" ]; then
echo "Profile not found: $2" >&2
return 1
fi
;;
*)
command envman "$command" "$@"
;;
esac
source "%s/$2.env"
else
command envman "$@"
fi
}
`, 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
`, envmanRoot, envmanRoot)
fishInitScript = fmt.Sprintf(`function envman
if [ "$argv[1]" = "load" ]
if [ -z "$argv[2]" ]
echo "Usage: envman load <profile>" >&2
return 1
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
end
if not test -f "%s/$argv[2].env"
echo "Profile not found: $argv[2]" >&2
return 1
end
case '*'
command envman "$command" $argv
source "%s/$argv[2].env"
else
command envman $argv
end
end
`, envmanRoot)
`, envmanRoot, envmanRoot)
}
72 changes: 72 additions & 0 deletions profile_load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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
}

0 comments on commit 453c247

Please sign in to comment.