diff --git a/CHANGES.md b/CHANGES.md index 5b296d1d..7273a3dc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## master + +* Document `HCLOUD_TOKEN` and make it work when there is no active context + ## v1.3.0 * Print dates in local time diff --git a/README.md b/README.md index b11790d4..67e03a9d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,24 @@ $ source <(hcloud completion bash) # bash $ source <(hcloud completion zsh)   # zsh ``` +## Using it in scripts + +When using `hcloud` in scripts, it may be cumbersome to work with contexts. +Instead of creating a context, you can set the token via the `HCLOUD_TOKEN` +environment variable: + +``` +$ hcloud image list +hcloud: no active context or token (see `hcloud context --help`) +$ export HCLOUD_TOKEN=token +$ hcloud image list +ID TYPE NAME DESCRIPTION IMAGE SIZE DISK SIZE CREATED +1 system ubuntu-16.04 Ubuntu 16.04 - 5 GB 1 month ago +2 system debian-9 Debian 9.3 - 5 GB 1 month ago +3 system centos-7 Centos 7.4 - 5 GB 1 month ago +4 system fedora-27 Fedora 27 - 5 GB 1 month ago +``` + ## Examples ### List all servers diff --git a/cli.go b/cli.go index ff2843a0..5a6992bf 100644 --- a/cli.go +++ b/cli.go @@ -153,9 +153,9 @@ func (c *CLI) ActionProgress(ctx context.Context, action *hcloud.Action) error { } } -func (c *CLI) ensureActiveContext(cmd *cobra.Command, args []string) error { - if c.Config.ActiveContext == nil { - return errors.New("no active context (see `hcloud context --help`)") +func (c *CLI) ensureToken(cmd *cobra.Command, args []string) error { + if c.Token == "" { + return errors.New("no active context or token (see `hcloud context --help`)") } return nil } diff --git a/datacenter_describe.go b/datacenter_describe.go index 3d1e20d3..0c37e663 100644 --- a/datacenter_describe.go +++ b/datacenter_describe.go @@ -14,7 +14,7 @@ func newDatacenterDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runDatacenterDescribe), } return cmd diff --git a/datacenter_list.go b/datacenter_list.go index 4eb1ea8e..3b952e0f 100644 --- a/datacenter_list.go +++ b/datacenter_list.go @@ -11,7 +11,7 @@ func newDatacenterListCommand(cli *CLI) *cobra.Command { Short: "List datacenters", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runDatacenterList), } return cmd diff --git a/floatingip_assign.go b/floatingip_assign.go index ad4f6986..c4bc55ba 100644 --- a/floatingip_assign.go +++ b/floatingip_assign.go @@ -16,7 +16,7 @@ func newFloatingIPAssignCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(2), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPAssign), } cmd.MarkFlagRequired("server") diff --git a/floatingip_create.go b/floatingip_create.go index ba6771ad..398e8af8 100644 --- a/floatingip_create.go +++ b/floatingip_create.go @@ -15,7 +15,7 @@ func newFloatingIPCreateCommand(cli *CLI) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateFloatingIPCreate, cli.ensureActiveContext), + PreRunE: chainRunE(validateFloatingIPCreate, cli.ensureToken), RunE: cli.wrap(runFloatingIPCreate), } cmd.Flags().String("type", "", "Type (ipv4 or ipv6)") diff --git a/floatingip_delete.go b/floatingip_delete.go index fb136aff..a99246b4 100644 --- a/floatingip_delete.go +++ b/floatingip_delete.go @@ -16,7 +16,7 @@ func newFloatingIPDeleteCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPDelete), } return cmd diff --git a/floatingip_describe.go b/floatingip_describe.go index 0ade40d7..e208226c 100644 --- a/floatingip_describe.go +++ b/floatingip_describe.go @@ -15,7 +15,7 @@ func newFloatingIPDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPDescribe), } return cmd diff --git a/floatingip_list.go b/floatingip_list.go index dc9a786b..0d2f0e1c 100644 --- a/floatingip_list.go +++ b/floatingip_list.go @@ -14,7 +14,7 @@ func newFloatingIPListCommand(cli *CLI) *cobra.Command { Short: "List Floating IPs", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPList), } return cmd diff --git a/floatingip_unassign.go b/floatingip_unassign.go index e5230b88..3780c941 100644 --- a/floatingip_unassign.go +++ b/floatingip_unassign.go @@ -16,7 +16,7 @@ func newFloatingIPUnassignCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPUnassign), } return cmd diff --git a/floatingip_update.go b/floatingip_update.go index bf579964..ddc4a410 100644 --- a/floatingip_update.go +++ b/floatingip_update.go @@ -16,7 +16,7 @@ func newFloatingIPUpdateCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runFloatingIPUpdate), } diff --git a/image_delete.go b/image_delete.go index cbd2bda5..02b9a679 100644 --- a/image_delete.go +++ b/image_delete.go @@ -13,7 +13,7 @@ func newImageDeleteCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runImageDelete), } return cmd diff --git a/image_describe.go b/image_describe.go index 2045bbca..2e05d82a 100644 --- a/image_describe.go +++ b/image_describe.go @@ -14,7 +14,7 @@ func newImageDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runImageDescribe), } return cmd diff --git a/image_list.go b/image_list.go index aded60ef..5669f051 100644 --- a/image_list.go +++ b/image_list.go @@ -15,7 +15,7 @@ func newImageListCommand(cli *CLI) *cobra.Command { Short: "List images", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runImageList), } return cmd diff --git a/image_update.go b/image_update.go index 2fb927f1..4c32a92d 100644 --- a/image_update.go +++ b/image_update.go @@ -14,7 +14,7 @@ func newImageUpdateCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runImageUpdate), } diff --git a/iso_describe.go b/iso_describe.go index 380ee4c0..3f93626a 100644 --- a/iso_describe.go +++ b/iso_describe.go @@ -13,7 +13,7 @@ func newISODescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runISODescribe), } return cmd diff --git a/iso_list.go b/iso_list.go index fac04e44..ab8f4f0a 100644 --- a/iso_list.go +++ b/iso_list.go @@ -11,7 +11,7 @@ func newISOListCommand(cli *CLI) *cobra.Command { Short: "List ISOs", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runISOList), } return cmd diff --git a/location_describe.go b/location_describe.go index 4dd07c83..ece800df 100644 --- a/location_describe.go +++ b/location_describe.go @@ -13,7 +13,7 @@ func newLocationDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runLocationDescribe), } return cmd diff --git a/location_list.go b/location_list.go index 463bd643..58b785d4 100644 --- a/location_list.go +++ b/location_list.go @@ -11,7 +11,7 @@ func newLocationListCommand(cli *CLI) *cobra.Command { Short: "List locations", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runLocationList), } return cmd diff --git a/server_attach_iso.go b/server_attach_iso.go index 55d8f281..0327a10b 100644 --- a/server_attach_iso.go +++ b/server_attach_iso.go @@ -13,7 +13,7 @@ func newServerAttachISOCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(2), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerAttachISO), } diff --git a/server_change_type.go b/server_change_type.go index 131eb7c2..3eab4471 100644 --- a/server_change_type.go +++ b/server_change_type.go @@ -14,7 +14,7 @@ func newServerChangeTypeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(2), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerChangeType), } diff --git a/server_create.go b/server_create.go index 316b4e85..3304d058 100644 --- a/server_create.go +++ b/server_create.go @@ -17,7 +17,7 @@ func newServerCreateCommand(cli *CLI) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerCreate), } cmd.Flags().String("name", "", "Server name") diff --git a/server_create_image.go b/server_create_image.go index 837e06a9..49e03ea5 100644 --- a/server_create_image.go +++ b/server_create_image.go @@ -14,7 +14,7 @@ func newServerCreateImageCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateServerCreateImage, cli.ensureActiveContext), + PreRunE: chainRunE(validateServerCreateImage, cli.ensureToken), RunE: cli.wrap(runServerCreateImage), } cmd.Flags().String("type", "snapshot", "Image type") diff --git a/server_delete.go b/server_delete.go index 05bc37f0..f54cb9ac 100644 --- a/server_delete.go +++ b/server_delete.go @@ -13,7 +13,7 @@ func newServerDeleteCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerDelete), } return cmd diff --git a/server_describe.go b/server_describe.go index 90f8b427..232671d2 100644 --- a/server_describe.go +++ b/server_describe.go @@ -14,7 +14,7 @@ func newServerDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerDescribe), } return cmd diff --git a/server_detach_iso.go b/server_detach_iso.go index 791b5bfc..d7c1a985 100644 --- a/server_detach_iso.go +++ b/server_detach_iso.go @@ -13,7 +13,7 @@ func newServerDetachISOCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerDetachISO), } diff --git a/server_disable_backup.go b/server_disable_backup.go index 3d762ef3..2b44ee5b 100644 --- a/server_disable_backup.go +++ b/server_disable_backup.go @@ -13,7 +13,7 @@ func newServerDisableBackupCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerDisableBackup), } return cmd diff --git a/server_disable_rescue.go b/server_disable_rescue.go index b85760b0..7cfe8b93 100644 --- a/server_disable_rescue.go +++ b/server_disable_rescue.go @@ -13,7 +13,7 @@ func newServerDisableRescueCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerDisableRescue), } return cmd diff --git a/server_enable_backup.go b/server_enable_backup.go index 13cab31f..46b13568 100644 --- a/server_enable_backup.go +++ b/server_enable_backup.go @@ -13,7 +13,7 @@ func newServerEnableBackupCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerEnableBackup), } cmd.Flags().String( diff --git a/server_enable_rescue.go b/server_enable_rescue.go index 0477e7bd..fbb11dd4 100644 --- a/server_enable_rescue.go +++ b/server_enable_rescue.go @@ -14,7 +14,7 @@ func newServerEnableRescueCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerEnableRescue), } cmd.Flags().String("type", "linux64", "Rescue type") diff --git a/server_list.go b/server_list.go index be044858..c713e194 100644 --- a/server_list.go +++ b/server_list.go @@ -11,7 +11,7 @@ func newServerListCommand(cli *CLI) *cobra.Command { Short: "List servers", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerList), } return cmd diff --git a/server_poweroff.go b/server_poweroff.go index d3d290e5..015d2418 100644 --- a/server_poweroff.go +++ b/server_poweroff.go @@ -13,7 +13,7 @@ func newServerPoweroffCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerPoweroff), } return cmd diff --git a/server_poweron.go b/server_poweron.go index de7b0c81..c426a2b4 100644 --- a/server_poweron.go +++ b/server_poweron.go @@ -13,7 +13,7 @@ func newServerPoweronCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerPoweron), } return cmd diff --git a/server_reboot.go b/server_reboot.go index b1defd4d..a4d9b3e8 100644 --- a/server_reboot.go +++ b/server_reboot.go @@ -13,7 +13,7 @@ func newServerRebootCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerReboot), } return cmd diff --git a/server_rebuild.go b/server_rebuild.go index 2ab4292e..37cb262c 100644 --- a/server_rebuild.go +++ b/server_rebuild.go @@ -14,7 +14,7 @@ func newServerRebuildCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerRebuild), } diff --git a/server_reset.go b/server_reset.go index 11029492..f2192a1a 100644 --- a/server_reset.go +++ b/server_reset.go @@ -13,7 +13,7 @@ func newServerResetCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerReset), } return cmd diff --git a/server_reset_password.go b/server_reset_password.go index ebffe907..b3ad407f 100644 --- a/server_reset_password.go +++ b/server_reset_password.go @@ -13,7 +13,7 @@ func newServerResetPasswordCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerResetPassword), } return cmd diff --git a/server_shutdown.go b/server_shutdown.go index f55ca91b..43c74512 100644 --- a/server_shutdown.go +++ b/server_shutdown.go @@ -13,7 +13,7 @@ func newServerShutdownCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerShutdown), } return cmd diff --git a/server_update.go b/server_update.go index 27f58c84..bd02ba77 100644 --- a/server_update.go +++ b/server_update.go @@ -15,7 +15,7 @@ func newServerUpdateCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerUpdate), } diff --git a/servertypes_describe.go b/servertypes_describe.go index b5b960df..24f44444 100644 --- a/servertypes_describe.go +++ b/servertypes_describe.go @@ -13,7 +13,7 @@ func newServerTypeDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerTypeDescribe), } return cmd diff --git a/servertypes_list.go b/servertypes_list.go index 13e5d356..5fe3af2f 100644 --- a/servertypes_list.go +++ b/servertypes_list.go @@ -13,7 +13,7 @@ func newServerTypeListCommand(cli *CLI) *cobra.Command { Short: "List server types", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runServerTypeList), } return cmd diff --git a/sshkey_create.go b/sshkey_create.go index 825d2768..efa7429d 100644 --- a/sshkey_create.go +++ b/sshkey_create.go @@ -17,7 +17,7 @@ func newSSHKeyCreateCommand(cli *CLI) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: chainRunE(validateSSHKeyCreate, cli.ensureActiveContext), + PreRunE: chainRunE(validateSSHKeyCreate, cli.ensureToken), RunE: cli.wrap(runSSHKeyCreate), } cmd.Flags().String("name", "", "Key name") diff --git a/sshkey_delete.go b/sshkey_delete.go index 66f03a55..2f2494bb 100644 --- a/sshkey_delete.go +++ b/sshkey_delete.go @@ -13,7 +13,7 @@ func newSSHKeyDeleteCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runSSHKeyDelete), } return cmd diff --git a/sshkey_describe.go b/sshkey_describe.go index 1902d6e9..74dab129 100644 --- a/sshkey_describe.go +++ b/sshkey_describe.go @@ -14,7 +14,7 @@ func newSSHKeyDescribeCommand(cli *CLI) *cobra.Command { Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runSSHKeyDescribe), } return cmd diff --git a/sshkey_list.go b/sshkey_list.go index b9368da5..c3c9f7c3 100644 --- a/sshkey_list.go +++ b/sshkey_list.go @@ -11,7 +11,7 @@ func newSSHKeyListCommand(cli *CLI) *cobra.Command { Short: "List SSH keys", TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: cli.ensureActiveContext, + PreRunE: cli.ensureToken, RunE: cli.wrap(runSSHKeyList), } return cmd