Skip to content

Commit

Permalink
Do not require a context when HCLOUD_TOKEN is given (#93)
Browse files Browse the repository at this point in the history
Closes #89.
  • Loading branch information
thcyron authored and thetechnick committed Mar 2, 2018
1 parent b25e703 commit 2e73eed
Show file tree
Hide file tree
Showing 46 changed files with 68 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion datacenter_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion datacenter_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floatingip_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion floatingip_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
2 changes: 1 addition & 1 deletion floatingip_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floatingip_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floatingip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floatingip_unassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion floatingip_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion image_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion iso_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion iso_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion location_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion location_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_attach_iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion server_change_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server_create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_detach_iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion server_disable_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_disable_rescue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_enable_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion server_enable_rescue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_poweroff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_poweron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_reboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion server_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
2 changes: 1 addition & 1 deletion servertypes_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion servertypes_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 2e73eed

Please sign in to comment.