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 453c247 commit 8ed7318
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 148 deletions.
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func main() {
}
return
}

if len(os.Args) > 1 && os.Args[1] == "init" {
shell := detectShell()
forShell := false
Expand Down
24 changes: 19 additions & 5 deletions outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "fmt"
const (
Version = "v0.1.0"
ProjectName = "envman"
configDirName = ".envman"
configFileName = "config"
)

Expand All @@ -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)
121 changes: 53 additions & 68 deletions profile_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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 <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)
}
return fmt.Sprintf(`
# START >>>>>>>>>>>> envman Managed [%s] <<<<<<<<<<<<<<<
eval "$(envman init - %s)"
# END >>>>>>>>>>>> envman Managed <<<<<<<<<<<<<<<`, currentTime, shell)
}

func (m InitModel) appendToRC() error {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 <profile>" >&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 <profile>" >&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)
}
2 changes: 1 addition & 1 deletion profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
72 changes: 0 additions & 72 deletions profile_load.go

This file was deleted.

0 comments on commit 8ed7318

Please sign in to comment.