-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-gcloud-permissions
executable file
·87 lines (71 loc) · 2.31 KB
/
fix-gcloud-permissions
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
# vi:syntax=bash
#!/bin/bash
# As always, thank you to this stackoverflow for the options parsing template: https://stackoverflow.com/a/29754866
DEFAULT_GCLOUD_LOCATION="$HOME/.config/gcloud/"
usage () {
return_code=$1
cat <<HEREDOC
fix-gcloud-permissions
Description:
Fixes those weird, pesky gcloud sdk directory permissions errors you sometimes get.
May ask for your sudo password due to the sdk directory sometimes accidentally getting chown'd to root.
Usage:
fix-gcloud-permissions [-h] [-d <gcloud_dir>]
Options:
-d|--gcloud_dir <gcloud_dir> : gcloud sdk directory [default: $HOME/.config/gcloud]
-h|--help : show this usage text
HEREDOC
exit $return_code
}
# More safety, by turning some bugs into errors.
# Without `errexit` you don’t need ! and can replace
# PIPESTATUS with a simple $?, but I don’t do that.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo '`getopt --test` failed in this environment.'
if [[ "$OSTYPE" == "darwin"* ]]; then
echo 'try running `brew install gnu-getopt` and then putting `export PATH="/usr/local/opt/gnu-getopt/bin:$PATH"` into your shellrc file'
fi
exit 1
fi
OPTIONS=hd:
LONGOPTS=help,gcloud_dir:
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
gcloud_dir="$DEFAULT_GCLOUD_LOCATION"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-d|--gcloud_dir)
gcloud_dir="$2"
shift 2
;;
-h|--help)
usage 0
break
;;
--)
shift
break
;;
*)
echo "$1: invalid argument" >&2
echo ''
usage 2
;;
esac
done
sudo chown -R $USER $gcloud_dir