Skip to content

Instantly share code, notes, and snippets.

@yamamoto-febc
Last active November 1, 2017 02:27
Show Gist options
  • Save yamamoto-febc/ecbd1828df11f0f88bc3cd254f9ca473 to your computer and use it in GitHub Desktop.
Save yamamoto-febc/ecbd1828df11f0f88bc3cd254f9ca473 to your computer and use it in GitHub Desktop.
usacloudで様々な出力形式を使ってみる(Go言語のtext/templateなど) ref: http://qiita.com/yamamoto-febc/items/68154c2f6a18073e628b
# CSV
$ usacloud server ls --out csv --col ID --col Name
ID,Name
123456789001,GoDev01
123456789002,GoDev02
123456789003,GoDev03
# TSV
$ usacloud server ls --out csv
ID Name
123456789001 GoDev01
123456789002 GoDev02
123456789003 GoDev03
# CSV
$ usacloud server ls --out csv
RowNumber,ID,Availability,CreatedAt,Description,[...省略...]
1,123456789001,available,2017-01-23T01:23:45+09:00,desc1,[...省略...]
2,123456789002,available,2017-01-23T01:23:45+09:00,desc2,[...省略...]
3,123456789003,available,2017-01-23T01:23:45+09:00,desc3,[...省略...]
# TSV
$ usacloud server ls --out csv
RowNumber ID Availability CreatedAt Description [...省略...]
1 123456789001 available 2017-01-23T01:23:45+09:00 desc1 [...省略...]
2 123456789002 available 2017-01-23T01:23:45+09:00 desc2 [...省略...]
3 123456789003 available 2017-01-23T01:23:45+09:00 desc3 [...省略...]
# IDのみ出力
$ usacloud server ls -q
123456789001
123456789002
123456789003
# CSV
$ usacloud server ls --out json | jq '.[] | {"id":.ID, "name":.Name, "ipaddress":.Interfaces[0].IPAddress}'
{
"id": 123456789001,
"name": "GoDev01",
"ipaddress": "192.2.100.101"
}
{
"id": 123456789002,
"name": "GoDev02",
"ipaddress": "192.2.100.102"
}
{
"id": 123456789003,
"name": "GoDev03",
"ipaddress": "192.2.100.103"
}
# JSON
$ usacloud server ls --out json
[
{
"ID": "123456789001",
"Name": "GoDev01",
"Description": "desc1",
(省略)
},
{
"ID": "123456789002",
"Name": "GoDev02",
"Description": "desc2",
(省略)
},
{
"ID": "123456789003",
"Name": "GoDev03",
"Description": "desc3",
(省略)
}
]
#デフォルト(table形式)
$ usacloud server ls
+--------------+-------------+-----+--------+------------------+--------+
| ID | Name | CPU | Memory | IPAddress | Status |
+--------------+-------------+-----+--------+------------------+--------+
| 123456789001 | GoDev01 | 2 | 4096MB | 192.2.100.101/24 | up |
| 123456789002 | GoDev02 | 2 | 4096MB | 192.2.100.102/24 | down |
| 123456789003 | GoDev03 | 2 | 4096MB | 192.2.100.103/24 | down |
+--------------+-------------+-----+--------+------------------+--------+
# --formatオプションでコマンドラインからテンプレートを指定
$ usacloud server ls --format "ID is {{.ID}}, Name is {{.Name}}"
ID is 123456789001, Name is GoDev01
ID is 123456789002, Name is GoDev02
ID is 123456789003, Name is GoDev03
# --format-fileオプションでファイルからテンプレートを指定
$ echo -n "ID is {{.ID}}, Name is {{.Name}}" > custom.tpl
$ usacloud server ls --format-file custom.tpl
ID is 123456789001, Name is GoDev01
ID is 123456789002, Name is GoDev02
ID is 123456789003, Name is GoDev03
# テンプレートの定義
$ cat > custom.tpl << EOL
#!/bin/sh
{{ with index .Interfaces 0 -}}
ip addr add {{.IPAddress}}/{{.Switch.UserSubnet.NetworkMaskLen}} dev eth0
ip route add default via {{.Switch.UserSubnet.DefaultRoute}} dev eth0
{{ end }}
{{ range .Zone.Region.NameServers -}}
echo "nameserver {{.}}" >> /etc/resolv.conf
{{ end -}}
echo "search localdomain" >> /etc/resolv.conf
EOL
# テンプレート指定でコマンド実行
$ usacloud server read --format-file custom.tpl GoDev01
#!/bin/sh
ip addr add 192.2.100.101/24 dev eth0
ip route add default via 192.2.100.1 dev eth0
echo "nameserver 133.242.0.3" >> /etc/resolv.conf
echo "nameserver 133.242.0.4" >> /etc/resolv.conf
echo "search localdomain" >> /etc/resolv.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment