Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added server {set,get}-user-data subcommand #105

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package cmd

import (
"context"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -132,6 +134,7 @@ func Server() *cobra.Command {
Long: ``,
}
ipv4Cmd.AddCommand(serverIPV4List, createIpv4, deleteIpv4)
createIpv4.Flags().StringP("reboot", "r", "no", "whether to reboot server after adding ipv4 address")
deleteIpv4.Flags().StringP("ipv4", "i", "", "ipv4 address you wish to delete")
deleteIpv4.MarkFlagRequired("ipv4")
serverCmd.AddCommand(ipv4Cmd)
Expand Down Expand Up @@ -177,9 +180,17 @@ func Server() *cobra.Command {
setIpv6.Flags().StringP("entry", "e", "", "reverse dns entry")
setIpv6.MarkFlagRequired("ip")
setIpv6.MarkFlagRequired("entry")

serverCmd.AddCommand(reverseCmd)

userdataCmd := &cobra.Command{
Use: "userdata",
Short: "commands to handle userdata on an instance",
Long: ``,
}
userdataCmd.AddCommand(setUserData, getUserData)
setUserData.Flags().StringP("userdata", "d", "/dev/stdin", "file to read userdata from")
serverCmd.AddCommand(userdataCmd)

return serverCmd
}

Expand Down Expand Up @@ -794,7 +805,8 @@ var createIpv4 = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
err := client.Server.AddIPV4(context.TODO(), id)
reboot, _ := cmd.Flags().GetString("reboot")
_, err := client.Server.AddIPV4(context.TODO(), id, reboot)

if err != nil {
fmt.Printf("error creating ipv4 : %v\n", err)
Expand Down Expand Up @@ -1119,6 +1131,55 @@ var serverCreate = &cobra.Command{
},
}

var setUserData = &cobra.Command{
Use: "set <instanceID>",
Short: "Set the user-data of a server",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide an instanceID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
userData, _ := cmd.Flags().GetString("userdata")
rawData, err := ioutil.ReadFile(userData)

if err != nil {
fmt.Printf("error reading user-data : %v\n", err)
os.Exit(1)
}

err = client.Server.SetUserData(context.TODO(), args[0], base64.StdEncoding.EncodeToString(rawData))

if err != nil {
fmt.Printf("error setting user-data : %v\n", err)
os.Exit(1)
}
fmt.Println("Set user-data for server")
},
}

var getUserData = &cobra.Command{
Use: "get <instanceID>",
Short: "Get the user-data of a server",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please provide an instanceID")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
userData, err := client.Server.GetUserData(context.TODO(), args[0])

if err != nil {
fmt.Printf("error getting user-data : %v\n", err)
os.Exit(1)
}

printer.UserData(userData)
},
}

func optionCheck(options map[string]string) (string, error) {
result := []string{}
for k, v := range options {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
github.com/vultr/govultr v0.4.2
github.com/vultr/govultr v0.5.0
golang.org/x/sys v0.0.0-20191110163157-d32e6e3b99c4 // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/hashicorp/go-retryablehttp v0.6.4 h1:BbgctKO892xEyOXnGiaAwIoSq1QZ/SS4
github.com/hashicorp/go-retryablehttp v0.6.4/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.6.7 h1:8/CAEZt/+F7kR7GevNHulKkUjLht3CPmn7egmhieNKo=
github.com/hashicorp/go-retryablehttp v0.6.7/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down Expand Up @@ -218,6 +220,8 @@ github.com/vultr/govultr v0.4.1 h1:6KjWW30t5czS9iWkYR0e3akjvRMDzYTiFCWVG+6Hcg8=
github.com/vultr/govultr v0.4.1/go.mod h1:TUuUizMOFc7z+PNMssb6iGjKjQfpw5arIaOLfocVudQ=
github.com/vultr/govultr v0.4.2 h1:9i8xKZ+xp6vwZ9raqHoBLzhB4wCnMj7nOQTj5YIRLWY=
github.com/vultr/govultr v0.4.2/go.mod h1:TUuUizMOFc7z+PNMssb6iGjKjQfpw5arIaOLfocVudQ=
github.com/vultr/govultr v0.5.0 h1:iQzYhzbokmpDARbvIkvTkoyS7WMH82zVTKAL1PZ4JOA=
github.com/vultr/govultr v0.5.0/go.mod h1:wZZXZbYbqyY1n3AldoeYNZK4Wnmmoq6dNFkvd5TV3ss=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
12 changes: 0 additions & 12 deletions vendor/github.com/hashicorp/go-retryablehttp/.travis.yml

This file was deleted.

Loading