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