-
Notifications
You must be signed in to change notification settings - Fork 151
/
custom_user_data.go
92 lines (80 loc) · 2.03 KB
/
custom_user_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package instance
import (
"context"
"fmt"
"io"
"net/http"
"sort"
"strings"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
//
// Commands
//
func userDataDeleteBuilder(c *core.Command) *core.Command {
return c
}
func userDataSetBuilder(c *core.Command) *core.Command {
*c.ArgSpecs.GetByName("content.name") = core.ArgSpec{
Name: "content",
Short: "Content of the user data",
Required: true,
CanLoadFile: true,
}
c.ArgSpecs.DeleteByName("content.content-type")
c.ArgSpecs.DeleteByName("content.content")
return c
}
func userDataGetBuilder(c *core.Command) *core.Command {
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
req := argsI.(*instance.GetServerUserDataRequest)
res, err := runner(ctx, argsI)
if err != nil {
if resErr, ok := err.(*scw.ResponseError); ok {
if resErr.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("'%s' key does not exist", req.Key)
}
}
return nil, err
}
return res, nil
})
return c
}
func userDataListBuilder(c *core.Command) *core.Command {
type userDataRow struct {
Key string `json:"key"`
Value string `json:"value"`
}
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
args := argsI.(*instance.ListServerUserDataRequest)
res, err := api.GetAllServerUserData(&instance.GetAllServerUserDataRequest{
Zone: args.Zone,
ServerID: args.ServerID,
})
if err != nil {
return nil, err
}
var r []userDataRow
for a, v := range res.UserData {
buf := new(strings.Builder)
_, err := io.Copy(buf, v)
if err != nil {
return nil, err
}
r = append(r, userDataRow{
Key: a,
Value: buf.String(),
})
}
sort.Slice(r, func(i, j int) bool {
return r[i].Key < r[j].Key
})
return r, nil
})
return c
}