Skip to content

Commit 665fafc

Browse files
thetechnickthcyron
authored andcommitted
Add --user-data-from-file flag to server create command (#88)
1 parent ddd1601 commit 665fafc

10 files changed

Lines changed: 70 additions & 5 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Print dates in local time
66
* Do not echo token when creating a context
7+
* Add `--user-data-from-file` flag to `hcloud server create` command
78

89
## v1.2.0
910

Gopkg.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[[constraint]]
66
name = "github.com/hetznercloud/hcloud-go"
7-
version = "1.2.0"
7+
version = "1.3.0"
88

99
[[constraint]]
1010
name = "github.com/pelletier/go-toml"

server_create.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cli
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"os"
57

68
"github.com/hetznercloud/hcloud-go/hcloud"
79
"github.com/spf13/cobra"
@@ -47,6 +49,9 @@ func newServerCreateCommand(cli *CLI) *cobra.Command {
4749
cmd.Flag("ssh-key").Annotations = map[string][]string{
4850
cobra.BashCompCustom: {"__hcloud_sshkey_names"},
4951
}
52+
53+
cmd.Flags().String("user-data-from-file", "", "Read user data from specified file (use - to read from stdin)")
54+
5055
return cmd
5156
}
5257

@@ -80,6 +85,7 @@ func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts
8085
image, _ := flags.GetString("image")
8186
location, _ := flags.GetString("location")
8287
datacenter, _ := flags.GetString("datacenter")
88+
userDataFile, _ := flags.GetString("user-data-from-file")
8389
sshKeys, _ := flags.GetStringSlice("ssh-key")
8490

8591
opts = hcloud.ServerCreateOpts{
@@ -91,6 +97,20 @@ func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts
9197
Name: image,
9298
},
9399
}
100+
101+
if userDataFile != "" {
102+
var data []byte
103+
if userDataFile == "-" {
104+
data, err = ioutil.ReadAll(os.Stdin)
105+
} else {
106+
data, err = ioutil.ReadFile(userDataFile)
107+
}
108+
if err != nil {
109+
return
110+
}
111+
opts.UserData = string(data)
112+
}
113+
94114
for _, sshKeyIDOrName := range sshKeys {
95115
var sshKey *hcloud.SSHKey
96116
sshKey, _, err = cli.Client().SSHKey.Get(cli.Context, sshKeyIDOrName)

vendor/github.com/hetznercloud/hcloud-go/CHANGES.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/client.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/hcloud.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/schema/server.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/server.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/server_test.go

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)