18700: Merge branch 'main'
[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, print current API host and available Arvados configurations.
19
20 arvsave <name>
21   Save current 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, print current arvbox and available arvboxes.
30
31 arvopen <uuid>
32   Open an Arvados uuid in web browser (http://arvadosapi.com)
33
34 arvissue <issue number>
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 "Current host: ${ARVADOS_API_HOST}"
65         echo "Usage: arvswitch <name>"
66         echo "Available confs:" $((cd $HOME/.config/arvados && ls --indicator-style=none *.conf) | rev | cut -c6- | rev)
67     fi
68 }
69
70 arvsave() {
71     if [[ -n "$1" ]] ; then
72         touch $HOME/.config/arvados/$1.conf
73         chmod 0600 $HOME/.config/arvados/$1.conf
74         env | grep ARVADOS_ > $HOME/.config/arvados/$1.conf
75     else
76         echo "Save current Arvados environment variables to conf file"
77         echo "Usage: arvsave <name>"
78     fi
79 }
80
81 arvrm() {
82     if [[ -n "$1" ]] ; then
83         if [[ -f $HOME/.config/arvados/$1.conf ]] ; then
84             rm $HOME/.config/arvados/$1.conf
85         else
86             echo "$1 unknown"
87         fi
88     else
89         echo "Delete Arvados environment conf"
90         echo "Usage: arvrm <name>"
91     fi
92 }
93
94 arvboxswitch() {
95     if [[ -n "$1" ]] ; then
96         export ARVBOX_CONTAINER=$1
97         if [[ -d $HOME/.arvbox/$1 ]] ; then
98             echo "Arvbox switched to $1"
99         else
100             echo "Warning: $1 doesn't exist, will be created."
101         fi
102     else
103         if test -z "$ARVBOX_CONTAINER" ; then
104             ARVBOX_CONTAINER=arvbox
105         fi
106         echo "Switch Arvbox environment conf"
107         echo "Your current container is: $ARVBOX_CONTAINER"
108         echo "Usage: arvboxswitch <name>"
109         echo "Available confs:" $(cd $HOME/.arvbox && ls --indicator-style=none)
110     fi
111 }
112
113 arvopen() {
114     if [[ -n "$1" ]] ; then
115         xdg-open https://arvadosapi.com/$1
116     else
117         echo "Open Arvados uuid in browser"
118         echo "Usage: arvopen <uuid>"
119     fi
120 }
121
122 arvissue() {
123     if [[ -n "$1" ]] ; then
124         xdg-open https://dev.arvados.org/issues/$1
125     else
126         echo "Open Arvados issue in browser"
127         echo "Usage: arvissue <issue number>"
128     fi
129 }