Skip to content

Commit 9b34d26

Browse files
authored
feat(server): add default-ssh-keys option (#759)
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.
1 parent eacb7dd commit 9b34d26

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
┌───────────────┬──────────────────────┬──────────┬───────────────┬──────────────────────┬─────────────────┐
2-
│ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │
3-
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
4-
│ debug │ Enable debug output │ boolean │ debug │ HCLOUD_DEBUG │ --debug │
5-
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
6-
│ debug-file │ File to write debug │ string │ debug_file │ HCLOUD_DEBUG_FILE │ --debug-file │
7-
│ │ output to │ │ │ │ │
8-
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
9-
│ endpoint │ Hetzner Cloud API │ string │ endpoint │ HCLOUD_ENDPOINT │ --endpoint │
10-
│ │ endpoint │ │ │ │ │
11-
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
12-
│ poll-interval │ Interval at which to │ duration │ poll_interval │ HCLOUD_POLL_INTERVAL │ --poll-interval │
13-
│ │ poll information, │ │ │ │ │
14-
│ │ for example action │ │ │ │ │
15-
│ │ progress │ │ │ │ │
16-
├───────────────┼──────────────────────┼──────────┼───────────────┼──────────────────────┼─────────────────┤
17-
│ quiet │ If true, only print │ boolean │ quiet │ HCLOUD_QUIET │ --quiet │
18-
│ │ error messages │ │ │ │ │
19-
└───────────────┴──────────────────────┴──────────┴───────────────┴──────────────────────┴─────────────────┘
1+
┌──────────────────┬──────────────────────┬─────────────┬──────────────────┬─────────────────────────┬─────────────────┐
2+
│ OPTION │ DESCRIPTION │ TYPE │ CONFIG KEY │ ENVIRONMENT VARIABLE │ FLAG │
3+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
4+
│ debug │ Enable debug output │ boolean │ debug │ HCLOUD_DEBUG │ --debug │
5+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
6+
│ debug-file │ File to write debug │ string │ debug_file │ HCLOUD_DEBUG_FILE │ --debug-file │
7+
│ │ output to │ │ │ │ │
8+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
9+
│ default-ssh-keys │ Default SSH keys for │ string list │ default_ssh_keys │ HCLOUD_DEFAULT_SSH_KEYS │ │
10+
│ │ new servers │ │ │ │ │
11+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
12+
│ endpoint │ Hetzner Cloud API │ string │ endpoint │ HCLOUD_ENDPOINT │ --endpoint │
13+
│ │ endpoint │ │ │ │ │
14+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
15+
│ poll-interval │ Interval at which to │ duration │ poll_interval │ HCLOUD_POLL_INTERVAL │ --poll-interval │
16+
│ │ poll information, │ │ │ │ │
17+
│ │ for example action │ │ │ │ │
18+
│ │ progress │ │ │ │ │
19+
├──────────────────┼──────────────────────┼─────────────┼──────────────────┼─────────────────────────┼─────────────────┤
20+
│ quiet │ If true, only print │ boolean │ quiet │ HCLOUD_QUIET │ --quiet │
21+
│ │ error messages │ │ │ │ │
22+
└──────────────────┴──────────────────────┴─────────────┴──────────────────┴─────────────────────────┴─────────────────┘

internal/cmd/server/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hetznercloud/cli/internal/cmd/cmpl"
1919
"github.com/hetznercloud/cli/internal/hcapi2"
2020
"github.com/hetznercloud/cli/internal/state"
21+
"github.com/hetznercloud/cli/internal/state/config"
2122
"github.com/hetznercloud/hcloud-go/v2/hcloud"
2223
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/actionutils"
2324
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
@@ -354,6 +355,10 @@ func createOptsFromFlags(
354355
}
355356
}
356357

358+
if !flags.Changed("ssh-key") && config.OptionDefaultSSHKeys.Changed(s.Config()) {
359+
sshKeys = config.OptionDefaultSSHKeys.Get(s.Config())
360+
}
361+
357362
for _, sshKeyIDOrName := range sshKeys {
358363
var sshKey *hcloud.SSHKey
359364
sshKey, _, err = s.Client().SSHKey().Get(s, sshKeyIDOrName)

internal/state/config/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ var (
134134
DefaultPreferenceFlags,
135135
nil,
136136
)
137+
138+
OptionDefaultSSHKeys = newOpt(
139+
"default-ssh-keys",
140+
"Default SSH keys for new servers",
141+
[]string{},
142+
(DefaultPreferenceFlags&^OptionFlagPFlag)|OptionFlagSlice,
143+
nil,
144+
)
137145
)
138146

139147
type Option[T any] struct {

0 commit comments

Comments
 (0)