-
Notifications
You must be signed in to change notification settings - Fork 69
/
resource_apply_cmd.go
71 lines (57 loc) 路 1.65 KB
/
resource_apply_cmd.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
package cmd
import (
"context"
"fmt"
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/spf13/cobra"
)
var (
applyParams = &applyParameters{}
applyCmd *cobra.Command
)
func init() {
applyCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: "apply " + resourceList(),
Short: "Apply resources",
Long: "Apply (create/update) resources to your Tracetest server",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(ctx context.Context, _ *cobra.Command, args []string) (string, error) {
resourceType := resourceParams.ResourceName
resourceClient, err := resources.Get(resourceType)
if err != nil {
return "", err
}
resultFormat, err := resourcemanager.Formats.GetWithFallback(output, "yaml")
if err != nil {
return "", err
}
inputFile, err := fileutil.Read(applyParams.DefinitionFile)
if err != nil {
return "", fmt.Errorf("cannot read file %s: %w", applyParams.DefinitionFile, err)
}
result, err := resourceClient.Apply(ctx, inputFile, resultFormat)
if err != nil {
return "", err
}
return result, nil
}, applyParams),
PostRun: teardownCommand,
}
applyCmd.Flags().StringVarP(&applyParams.DefinitionFile, "file", "f", "", "path to the definition file")
rootCmd.AddCommand(applyCmd)
}
type applyParameters struct {
DefinitionFile string
}
func (p applyParameters) Validate(cmd *cobra.Command, args []string) []error {
errors := make([]error, 0)
if p.DefinitionFile == "" {
errors = append(errors, paramError{
Parameter: "file",
Message: "Definition file must be provided",
})
}
return errors
}