Skip to content

Commit

Permalink
feat(server): add default-ssh-keys option (#759)
Browse files Browse the repository at this point in the history
This PR adds the `default-ssh-keys` option which allows you to define a
list of SSH keys to be used as defaults for the `hcloud server create`
command.
  • Loading branch information
phm07 committed Jun 6, 2024
1 parent eacb7dd commit 9b34d26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
41 changes: 22 additions & 19 deletions internal/cmd/config/helptext/preferences.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
┌───────────────┬──────────────────────┬──────────┬───────────────┬──────────────────────┬─────────────────┐
│ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
│ debug │ Enable debug output │ boolean │ debug │ HCLOUD_DEBUG │ --debug │
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
│ debug-file │ File to write debug │ string │ debug_file │ HCLOUD_DEBUG_FILE │ --debug-file │
│ │ output to │ │ │ │ │
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
│ endpoint │ Hetzner Cloud API │ string │ endpoint │ HCLOUD_ENDPOINT │ --endpoint │
│ │ endpoint │ │ │ │ │
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
│ poll-interval │ Interval at which to │ duration │ poll_interval │ HCLOUD_POLL_INTERVAL │ --poll-interval │
│ │ poll information, │ │ │ │ │
│ │ for example action │ │ │ │ │
│ │ progress │ │ │ │ │
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
│ quiet │ If true, only print │ boolean │ quiet │ HCLOUD_QUIET │ --quiet │
│ │ error messages │ │ │ │ │
└───────────────┴──────────────────────┴──────────┴───────────────┴──────────────────────┴─────────────────┘
┌──────────────────┬──────────────────────┬─────────────┬──────────────────┬─────────────────────────┬─────────────────┐
│ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ debug │ Enable debug output │ boolean │ debug │ HCLOUD_DEBUG │ --debug │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ debug-file │ File to write debug │ string │ debug_file │ HCLOUD_DEBUG_FILE │ --debug-file │
│ │ output to │ │ │ │ │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ default-ssh-keys │ Default SSH keys for │ string list │ default_ssh_keys │ HCLOUD_DEFAULT_SSH_KEYS │ │
│ │ new servers │ │ │ │ │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ endpoint │ Hetzner Cloud API │ string │ endpoint │ HCLOUD_ENDPOINT │ --endpoint │
│ │ endpoint │ │ │ │ │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ poll-interval │ Interval at which to │ duration │ poll_interval │ HCLOUD_POLL_INTERVAL │ --poll-interval │
│ │ poll information, │ │ │ │ │
│ │ for example action │ │ │ │ │
│ │ progress │ │ │ │ │
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
│ quiet │ If true, only print │ boolean │ quiet │ HCLOUD_QUIET │ --quiet │
│ │ error messages │ │ │ │ │
└──────────────────┴──────────────────────┴─────────────┴──────────────────┴─────────────────────────┴─────────────────┘
5 changes: 5 additions & 0 deletions internal/cmd/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/actionutils"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
Expand Down Expand Up @@ -354,6 +355,10 @@ func createOptsFromFlags(
}
}

if !flags.Changed("ssh-key") && config.OptionDefaultSSHKeys.Changed(s.Config()) {
sshKeys = config.OptionDefaultSSHKeys.Get(s.Config())
}

for _, sshKeyIDOrName := range sshKeys {
var sshKey *hcloud.SSHKey
sshKey, _, err = s.Client().SSHKey().Get(s, sshKeyIDOrName)
Expand Down
8 changes: 8 additions & 0 deletions internal/state/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ var (
DefaultPreferenceFlags,
nil,
)

OptionDefaultSSHKeys = newOpt(
"default-ssh-keys",
"Default SSH keys for new servers",
[]string{},
(DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice,
nil,
)
)

type Option[T any] struct {
Expand Down

0 comments on commit 9b34d26

Please sign in to comment.