|
| 1 | +# Using output options |
| 2 | + |
| 3 | +The CLI allows you to customize the output format of some commands using the `--output` flag. |
| 4 | +This guide shows you how to use this feature and use it in combination with other tools. |
| 5 | + |
| 6 | +## JSON |
| 7 | + |
| 8 | +`describe`, `list` and `create` commands support JSON output format. To use it, simply add the `--output json` flag to your command. |
| 9 | + |
| 10 | +For example, to get the details of a location in JSON format, you can use the following command: |
| 11 | + |
| 12 | +```bash |
| 13 | +$ hcloud location describe fsn1 --output json |
| 14 | +{ |
| 15 | + "id": 1, |
| 16 | + "name": "fsn1", |
| 17 | + "description": "Falkenstein DC Park 1", |
| 18 | + "country": "DE", |
| 19 | + "city": "Falkenstein", |
| 20 | + "latitude": 50.47612, |
| 21 | + "longitude": 12.370071, |
| 22 | + "network_zone": "eu-central" |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +You can combine this with other tools to process the output. For example, you can use `jq` to filter the output: |
| 27 | + |
| 28 | +```bash |
| 29 | +$ hcloud location describe fsn1 --output json | jq '.name' |
| 30 | +"fsn1" |
| 31 | +``` |
| 32 | + |
| 33 | +```bash |
| 34 | +$ hcloud location describe fsn1 --output json | jq '{id, name}' |
| 35 | +{ |
| 36 | + "id": 1, |
| 37 | + "name": "fsn1" |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +`list` commands return a list of objects in JSON format. For example, to get a list of all locations in JSON format, you can use the following command: |
| 42 | + |
| 43 | +```bash |
| 44 | +$ hcloud location list --output json |
| 45 | +[ |
| 46 | + { |
| 47 | + "id": 1, |
| 48 | + "name": "fsn1", |
| 49 | + "description": "Falkenstein DC Park 1", |
| 50 | + "country": "DE", |
| 51 | + "city": "Falkenstein", |
| 52 | + "latitude": 50.47612, |
| 53 | + "longitude": 12.370071, |
| 54 | + "network_zone": "eu-central" |
| 55 | + }, |
| 56 | + { |
| 57 | + "id": 2, |
| 58 | + "name": "nbg1", |
| 59 | + "description": "Nuremberg DC Park 1", |
| 60 | + "country": "DE", |
| 61 | + "city": "Nuremberg", |
| 62 | + "latitude": 49.452102, |
| 63 | + "longitude": 11.076665, |
| 64 | + "network_zone": "eu-central" |
| 65 | + }, |
| 66 | + ... |
| 67 | +] |
| 68 | +``` |
| 69 | + |
| 70 | +Once again, you can use `jq` to filter the output. Following example shows how to get the names of all locations of which the network zone is `eu-central`: |
| 71 | + |
| 72 | +```bash |
| 73 | +$ hcloud location list --output json | jq '[.[] | select(.network_zone == "eu-central") | .name]' |
| 74 | +[ |
| 75 | + "fsn1", |
| 76 | + "nbg1", |
| 77 | + "hel1" |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +## YAML |
| 82 | + |
| 83 | +`describe`, `list` and `create` commands support YAML output format as well. |
| 84 | + |
| 85 | +```bash |
| 86 | +$ hcloud location describe fsn1 --output yaml |
| 87 | +id: 1 |
| 88 | +name: fsn1 |
| 89 | +description: Falkenstein DC Park 1 |
| 90 | +country: DE |
| 91 | +city: Falkenstein |
| 92 | +latitude: 50.47612 |
| 93 | +longitude: 12.370071 |
| 94 | +network_zone: eu-central |
| 95 | +``` |
| 96 | + |
| 97 | +For YAML, you can use `yq` instead of `jq`. |
| 98 | + |
| 99 | + |
| 100 | +```bash |
| 101 | +$ hcloud location list --output yaml | yq '.[] | [{"id": .id, "name": .name}]' |
| 102 | +- id: 1 |
| 103 | + name: fsn1 |
| 104 | +- id: 2 |
| 105 | + name: nbg1 |
| 106 | +- id: 3 |
| 107 | + name: hel1 |
| 108 | +- id: 4 |
| 109 | + name: ash |
| 110 | +- id: 5 |
| 111 | + name: hil |
| 112 | +- id: 6 |
| 113 | + name: sin |
| 114 | +``` |
| 115 | + |
| 116 | +## Go Template Format |
| 117 | + |
| 118 | +`describe` commands support the Go string template format as well. You can read up on the syntax in the |
| 119 | +[Go documentation](https://pkg.go.dev/text/template/). The template format will be applied to structs from hcloud-go. |
| 120 | +You can find the structs in the [hcloud-go source code](https://github.com/hetznercloud/hcloud-go/tree/main/hcloud). |
| 121 | + |
| 122 | +For example, you could obtain the number of cores of a server using the following command: |
| 123 | + |
| 124 | +```bash |
| 125 | +$ hcloud server describe my-server --output format={{.ServerType.Cores}} |
| 126 | +2 |
| 127 | +``` |
| 128 | + |
| 129 | +## Table options |
| 130 | + |
| 131 | +`list` commands support table options as well. These options allow you to customize the output format of the output table, |
| 132 | +if not using JSON or YAML. |
| 133 | + |
| 134 | +### noheader |
| 135 | + |
| 136 | +This option removes the header from the output table. |
| 137 | + |
| 138 | +```bash |
| 139 | +$ hcloud location list --output noheader |
| 140 | +1 fsn1 Falkenstein DC Park 1 eu-central DE Falkenstein |
| 141 | +2 nbg1 Nuremberg DC Park 1 eu-central DE Nuremberg |
| 142 | +3 hel1 Helsinki DC Park 1 eu-central FI Helsinki |
| 143 | +4 ash Ashburn, VA us-east US Ashburn, VA |
| 144 | +5 hil Hillsboro, OR us-west US Hillsboro, OR |
| 145 | +6 sin Singapore ap-southeast SG Singapore |
| 146 | +``` |
| 147 | + |
| 148 | +### columns |
| 149 | + |
| 150 | +This option allows you to filter by columns. |
| 151 | + |
| 152 | +```bash |
| 153 | +$ hcloud location list --output columns=id,name,network_zone |
| 154 | +ID NAME NETWORK ZONE |
| 155 | +1 fsn1 eu-central |
| 156 | +2 nbg1 eu-central |
| 157 | +3 hel1 eu-central |
| 158 | +4 ash us-east |
| 159 | +5 hil us-west |
| 160 | +6 sin ap-southeast |
| 161 | +``` |
| 162 | + |
| 163 | +Using the ``--help`` flag will show you a list of all available columns for this command. Note that these might include |
| 164 | +more than the default columns. |
0 commit comments