-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathenv.ab
164 lines (136 loc) Β· 4.6 KB
/
env.ab
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * from "std/fs"
import * from "std/text"
/// Retrieves the value of an environment variable, optionally sourcing it from a file if not already set.
pub fun get_env_var(var: Text, file: Text = ".env"): Text {
let _var = trust $ echo "\$\{!var}" $
if _var != "" {
return _var
}
if file_exist(file) {
trust $ source "{file}" $
return trust $ echo "\$\{!var}" $
}
return ""
}
/// Loads the env file in the environment, using `xargs`.
pub fun load_env_file(file: Text = ".env"): Null {
trust $ export "\$(xargs < {file})" > /dev/null $
}
/// Checks if a variable inside the shell session exists.
pub fun shell_isset(name: Text): Bool {
$ [[ ! -z \$\{!{nameof name}+z} ]] $ failed {
return false
}
return true
}
/// Sets a constant inside the shell session.
pub fun shell_constant_set(name: Text, val: Text): Null? {
$ readonly \${nameof name}="\${nameof val}" 2> /dev/null $?
}
/// Gets a constant inside the shell session.
pub fun shell_constant_get(name: Text): Text? {
return $ echo \$\{!{nameof name}} $?
}
/// Sets a constant inside the shell session.
pub fun shell_var_set(name: Text, val: Text): Null? {
$ export \${nameof name}="\${nameof val}" 2> /dev/null $?
}
/// Gets a constant inside the shell session.
pub fun shell_var_get(name: Text): Text? {
return $ echo \$\{!{nameof name}} $?
}
/// Removes a variable inside the shell session.
pub fun shell_unset(name: Text): Null? {
$ unset {name} $?
}
/// Checks if a command exists.
pub fun is_command(command: Text): Bool {
$ [ -x "\$(command -v {command})" ] $ failed {
return false
}
return true
}
/// Creates a prompt and returns the value.
pub fun input(prompt: Text): Text {
trust $ read -p "\${nameof prompt}" $
return "\$REPLY"
}
/// Creates a prompt, hides any user input and returns the value.
pub fun input_hidden(prompt: Text): Text {
trust {
$ read -s -p "\${nameof prompt}" $
$ echo "" >&2 $
}
return "\$REPLY"
}
/// Creates a confirm prompt (Yes/No), and returns true if the choice is Yes.
///
/// "No" is the default choice, set default_yes to true for "Yes" as default choice.
pub fun confirm(prompt: Text, default_yes: Bool = false): Bool {
let choice_default = default_yes then " [\x1b[1mY/\x1b[0mn]" else " [y/\x1b[1mN\x1b[0m]"
trust {
$ printf "\x1b[1m{prompt}\x1b[0m{choice_default}" $
$ read -s -n 1 $
$ printf "\n" $
}
let result = lower(trust $ echo \$REPLY $)
return result == "y" or (result == "" and default_yes)
}
/// Checks if the command has failed.
pub fun has_failed(command: Text): Bool {
trust silent $ eval {command} $
return status != 0
}
/// Checks if the script is running with a user with root permission.
pub fun is_root(): Bool {
if trust $ id -u $ == "0" {
return true
}
return false
}
/// `printf` the text following the arguments.
pub fun printf(format: Text, args: [Text] = [""]): Null {
trust $ {nameof args}=("{format}" "\$\{{nameof args}[@]}") $
trust $ printf "\$\{{nameof args}[@]}" $
}
/// Escapes the text to be used with `printf`.
pub fun printf_escape(text: Text): Text {
return trust $ echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g" $
}
/// Prepares a text with formatting options for `printf`.
pub fun text_shell(message: Text, style: Num, fg: Num, bg: Num): Text {
return "\x1b[{style};{fg};{bg}m{printf_escape(message)}\x1b[0m"
}
/// Returns a text as bold.
pub fun text_bold(message: Text): Text {
return "\x1b[1m{printf_escape(message)}\x1b[0m"
}
/// Returns a text as italic.
pub fun text_italic(message: Text): Text {
return "\x1b[3m{printf_escape(message)}\x1b[0m"
}
/// Returns a text as underlined.
pub fun text_underlined(message: Text): Text {
return "\x1b[4m{printf_escape(message)}\x1b[0m"
}
/// Prints a text with a specified color.
pub fun color_echo(message: Text, color: Num): Null {
printf("\x1b[{color as Text}m%s\x1b[0m\n", [message])
}
/// Prints a text as a info message.
pub fun echo_info(message: Text): Null {
printf("\x1b[1;3;97;44m%s\x1b[0m\n", [message])
}
/// Prints a text as a success message.
pub fun echo_success(message: Text): Null {
printf("\x1b[1;3;97;42m%s\x1b[0m\n", [message])
}
/// Prints a text as a warning message.
pub fun echo_warning(message: Text): Null {
printf("\x1b[1;3;97;43m%s\x1b[0m\n", [message])
}
/// Prints a text as a error and exits if the status code is greater than 0.
pub fun error(message: Text, exit_code: Num = 1): Null {
printf("\x1b[1;3;97;41m%s\x1b[0m\n", [message])
if exit_code > 0 : exit(exit_code)
}