-
Notifications
You must be signed in to change notification settings - Fork 21
/
scm_cli.sh
52 lines (47 loc) · 1.78 KB
/
scm_cli.sh
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
function config::scm_cli() {
local token;
# Wait for gh to be installed via nix at userland_tools.sh:leveltwo_pkgs
await::until_true command::exists "${gitpod_scm_cli}";
await::for_gitpod_workspace_ready;
# Login into scm_cli (i.e. gh or glab)
declare -a scm_cli_args=("${gitpod_scm_cli}" auth login);
declare scm_host;
case "$gitpod_scm_cli" in
"gh")
scm_cli_args+=(
--with-token
)
if test -v DOTFILES_GITHUB_TOKEN; then {
log::info "Using \$DOTFILES_GITHUB_TOKEN";
} fi
token="${DOTFILES_GITHUB_TOKEN:-}";
scm_host="github.com";
;;
"glab")
scm_cli_args+=(
--stdin
)
if test -v DOTFILES_GITLAB_TOKEN; then {
log::info "Using \$DOTFILES_GITLAB_TOKEN";
} fi
token="${DOTFILES_GITLAB_TOKEN:-}";
scm_host="gitlab.com";
;;
esac
# if [[ "$(printf '%s\n' host=github.com | gp credential-helper get)" =~ password=(.*) ]]; then {
if test -n "${token:-}" || token="$(printf '%s\n' "host=${scm_host}" | gp credential-helper get | awk -F'password=' '{print $2}')"; then {
local tries=1;
until printf '%s\n' "$token" | "${scm_cli_args[@]}"; do {
if test $tries -gt 2; then {
log::error "Failed to authenticate to 'gh' CLI with 'gp' credentials after trying for $tries times with ${token:0:9}" 1 || return;
break;
} fi
((tries++));
sleep 1;
continue;
} done
} else {
log::error "Failed to get auth token for ${gitpod_scm_cli}" 1 || return;
} fi
log::info "Logged into ${scm_host^} via ${gitpod_scm_cli} CLI";
}