c93fe26ed726069bff069d5bac25b83c61e0ec73
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / bin / cirros-query
1 #!/bin/sh
2
3 . ${CIRROS_LIB:=/lib/cirros/shlib_cirros} ||
4         { echo "failed to read ${CIRROS_LIB}" 1>&2; exit 1; }
5
6 Usage() {
7         cat <<EOF
8 Usage: ${0##*/} command [options]
9
10    commands:
11      datasource:  list the datasource name
12      dsmode:      list the datasource mode ('net', or 'local')
13      available:   list available fields for datasource
14      get field:   dump field
15 EOF
16 }
17
18 cirros_query() {
19         local short_opts="hqv"
20         local long_opts="help,quiet,verbose"
21         local getopt_out=""
22         getopt_out=$(getopt --name "${0##*/}" \
23                 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
24                 eval set -- "${getopt_out}" ||
25                 { bad_Usage; return; }
26
27         local cur="" next="" command="" VERBOSITY
28         while [ $# -ne 0 ]; do
29                 cur=${1}; next=${2};
30                 case "$cur" in
31                         -h|--help) Usage ; exit 0;;
32                         -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
33                         -q|--quiet) VERBOSITY=0;;
34                         --) shift; break;;
35                 esac
36                 shift;
37         done
38
39         [ $# -ne 0 ] || { bad_Usage "must provide arguments"; return; }
40
41         local command="$1" dsname="" dsmode="" start_d="$PWD"
42
43         assert_datasource ||
44                 { debug 1 "no datasource"; return 1; }
45         dsname="${_DATASOURCE_NAME}"
46         dsmode="${_DATASOURCE_MODE}"
47
48         if [ "$command" = "datasource" ]; then
49                 [ $# -eq 1 ] ||
50                         { error "confused by '$2' to mode $command"; return 1; }
51                 echo "$dsname"
52                 return
53         elif [ "$command" = "dsmode" ]; then
54                 [ $# -eq 1 ] ||
55                         { error "confused by '$2' to mode $command"; return 1; }
56                 echo "$dsmode"
57                 return
58         elif [ "$command" = "available" ]; then
59                 [ $# -eq 1 ] ||
60                         { error "confused by '$2' to mode $command"; return 1; }
61                 ds_list_items ||
62                         { error "failed to list items"; return 1; }
63                 echo "$_RET"
64                 return
65         elif [ "$command" = "get" ]; then
66                 [ "$#" -eq 2 ] || fail "can only query 1 item at a time"
67                 ds_cat_item "$2"
68                 return
69         else
70                 error "unknown command '$command'"
71         fi
72
73         return 1
74 }
75
76 cirros_query "$@"
77
78 # vi: ts=4 noexpandtab