Merge branch '10655-arvbash' closes #10655
[arvados.git] / tools / arvbash / arvbash.sh
1 #!/bin/bash
2 # bash functions for managing Arvados tokens and other conveniences.
3
4 read -rd "\000" helpmessage <<EOF
5 $(basename $0): bash functions for managing Arvados tokens and other shortcuts.
6
7 Syntax:
8         . $0            # activate for current shell
9         $0 --install    # install into .bashrc
10
11 arvswitch <name>
12   Set ARVADOS_API_HOST and ARVADOS_API_TOKEN in the current environment based on
13   $HOME/.config/arvados/<name>.conf
14   With no arguments, list available Arvados configurations.
15
16 arvsave <name>
17   Save values of ARVADOS_API_HOST and ARVADOS_API_TOKEN in the current environment to
18   $HOME/.config/arvados/<name>.conf
19
20 arvrm <name>
21   Delete $HOME/.config/arvados/<name>.conf
22
23 arvboxswitch <name>
24   Set ARVBOX_CONTAINER to <name>
25   With no arguments, list available arvboxes.
26
27 arvopen:
28   Open an Arvados uuid in web browser (http://curover.se)
29
30 arvissue
31   Open an Arvados ticket in web browser (http://dev.arvados.org)
32
33 EOF
34
35 if [[ "$1" = "--install" ]] ; then
36     this=$(readlink -f $0)
37     if ! grep ". $this" ~/.bashrc >/dev/null ; then
38         echo ". $this" >> ~/.bashrc
39         echo "Installed into ~/.bashrc"
40     else
41         echo "Already installed in ~/.bashrc"
42     fi
43 elif [[ $0 != "bash" ]] ; then
44     echo "$helpmessage"
45 fi
46
47 HISTIGNORE=$HISTIGNORE:'export ARVADOS_API_TOKEN=*'
48
49 arvswitch() {
50     if [[ -n "$1" ]] ; then
51         if [[ -f $HOME/.config/arvados/$1.conf ]] ; then
52             unset ARVADOS_API_HOST_INSECURE
53             for a in $(cat $HOME/.config/arvados/$1.conf) ; do export $a ; done
54             echo "Switched to $1"
55         else
56             echo "$1 unknown"
57         fi
58     else
59         echo "Switch Arvados environment conf"
60         echo "Usage: arvswitch name"
61         echo "Available confs:" $((cd $HOME/.config/arvados && ls --indicator-style=none *.conf) | rev | cut -c6- | rev)
62     fi
63 }
64
65 arvsave() {
66     if [[ -n "$1" ]] ; then
67         touch $HOME/.config/arvados/$1.conf
68         chmod 0600 $HOME/.config/arvados/$1.conf
69         env | grep ARVADOS_ > $HOME/.config/arvados/$1.conf
70     else
71         echo "Save current Arvados environment variables to conf file"
72         echo "Usage: arvsave name"
73     fi
74 }
75
76 arvrm() {
77     if [[ -n "$1" ]] ; then
78         if [[ -f $HOME/.config/arvados/$1.conf ]] ; then
79             rm $HOME/.config/arvados/$1.conf
80         else
81             echo "$1 unknown"
82         fi
83     else
84         echo "Delete Arvados environment conf"
85         echo "Usage: arvrm name"
86     fi
87 }
88
89 arvboxswitch() {
90     if [[ -n "$1" ]] ; then
91         if [[ -d $HOME/.arvbox/$1 ]] ; then
92             export ARVBOX_CONTAINER=$1
93             echo "Arvbox switched to $1"
94         else
95             echo "$1 unknown"
96         fi
97     else
98         if test -z "$ARVBOX_CONTAINER" ; then
99             ARVBOX_CONTAINER=arvbox
100         fi
101         echo "Switch Arvbox environment conf"
102         echo "Usage: arvboxswitch name"
103         echo "Your current container is: $ARVBOX_CONTAINER"
104         echo "Available confs:" $(cd $HOME/.arvbox && ls --indicator-style=none)
105     fi
106 }
107
108 arvopen() {
109     if [[ -n "$1" ]] ; then
110         xdg-open https://curover.se/$1
111     else
112         echo "Open Arvados uuid in browser"
113         echo "Usage: arvopen uuid"
114     fi
115 }
116
117 arvissue() {
118     if [[ -n "$1" ]] ; then
119         xdg-open https://dev.arvados.org/issues/$1
120     else
121         echo "Open Arvados issue in browser"
122         echo "Usage: arvissue uuid"
123     fi
124 }