-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-vault
executable file
·89 lines (80 loc) · 2.06 KB
/
aws-vault
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
# Initialize variables
found_exec=false
found_delimiter=false
profile=""
command_args=""
aws_flags=""
# Function to check if string starts with -
is_flag() {
case "$1" in
-*) return 0 ;;
*) return 1 ;;
esac
}
# Function to add to aws flags with proper spacing
add_aws_flag() {
if [ -z "$aws_flags" ]; then
aws_flags="$1"
else
aws_flags="$aws_flags $1"
fi
}
# Function to add to command args with proper spacing
add_cmd_arg() {
if [ -z "$command_args" ]; then
command_args="$1"
else
command_args="$command_args $1"
fi
}
# Process arguments
while [ $# -gt 0 ]; do
if [ "$1" = "exec" ]; then
found_exec=true
shift
continue
fi
if [ "$found_exec" = true ]; then
# If we hit the delimiter, everything after is command args
if [ "$1" = "--" ]; then
found_delimiter=true
shift
continue
fi
if [ "$found_delimiter" = true ]; then
# After delimiter, everything goes to command
add_cmd_arg "$1"
else
# If we don't have a profile yet and this isn't a flag, it's the profile
if [ -z "$profile" ] && ! is_flag "$1"; then
profile="$1"
# If it's a flag before the delimiter, it goes to aws-vault
elif is_flag "$1"; then
add_aws_flag "$1"
# If we have the profile, everything else is part of the command
else
add_cmd_arg "$1"
fi
fi
else
add_aws_flag "$1"
fi
shift
done
# Handle different cases
if [ "$found_exec" = true ]; then
# Use $SHELL as default if no command specified
if [ -z "$command_args" ]; then
command_args="$SHELL"
fi
# Execute with environment passing and preserve working directory
# shellcheck disable=SC2086
# shellcheck disable=SC2016
# shellcheck disable=SC2046
exec aws-vault.exe exec $aws_flags $profile -- powershell.exe -Command 'wsl.exe --cd "'$(pwd)'" sh -l -c "env $(gci env:AWS_* | ForEach-Object { "$($_.Name)=$($_.Value)" }) '$command_args'"'
else
# For non-exec commands, pass everything through
# shellcheck disable=SC2086
exec aws-vault.exe $aws_flags
fi