/
k8s-raw-set.go
67 lines (56 loc) · 1.62 KB
/
k8s-raw-set.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
)
// K8sRawSetCommand implements the k8s-raw-set command.
type K8sRawSetCommand struct {
cmd.CommandBase
ctx Context
specFile cmd.FileVar
}
// NewK8sRawSetCommand makes a k8s-raw-set command.
func NewK8sRawSetCommand(ctx Context) (cmd.Command, error) {
return &K8sRawSetCommand{ctx: ctx}, nil
}
func (c *K8sRawSetCommand) Info() *cmd.Info {
doc := `
Sets configuration data in k8s raw format to use for k8s resources.
The spec applies to all units for the application.
`
return jujucmd.Info(&cmd.Info{
Name: "k8s-raw-set",
Args: "--file <core spec file>",
Purpose: "set k8s raw spec information",
Doc: doc,
})
}
func (c *K8sRawSetCommand) SetFlags(f *gnuflag.FlagSet) {
c.specFile.SetStdin()
c.specFile.Path = "-"
f.Var(&c.specFile, "file", "file containing k8s raw spec")
}
func (c *K8sRawSetCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *K8sRawSetCommand) Run(ctx *cmd.Context) error {
specData, err := c.handleSpecFile(ctx)
if err != nil {
return errors.Trace(err)
}
return c.ctx.SetRawK8sSpec(specData)
}
func (c *K8sRawSetCommand) handleSpecFile(ctx *cmd.Context) (string, error) {
specData, err := c.specFile.Read(ctx)
if err != nil {
return "", errors.Trace(err)
}
if len(specData) == 0 {
return "", errors.New("no k8s raw spec specified: pipe k8s raw spec to command, or specify a file with --file")
}
return string(specData), nil
}