This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
feature.go
103 lines (93 loc) · 2.58 KB
/
feature.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
93
94
95
96
97
98
99
100
101
102
103
package feature
import (
"fmt"
"github.com/rancher/rio/cli/cmd/apply"
"github.com/rancher/rio/cli/pkg/builder"
"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/cli/pkg/lookup"
"github.com/rancher/rio/cli/pkg/table"
"github.com/rancher/rio/cli/pkg/tables"
clitypes "github.com/rancher/rio/cli/pkg/types"
"github.com/rancher/rio/cli/pkg/up/questions"
projectv1 "github.com/rancher/rio/pkg/apis/admin.rio.cattle.io/v1"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Feature(app *cli.App) cli.Command {
ls := builder.Command(&Ls{},
"List features",
app.Name+" feature ls",
"")
return cli.Command{
Name: "features",
ShortName: "feature",
Usage: "Enable, Disable and List features",
Action: clicontext.DefaultAction(ls.Action),
Flags: table.WriterFlags(),
Category: "SUB COMMANDS",
Subcommands: []cli.Command{
ls,
builder.Command(&Enable{},
"Enable a feature",
app.Name+" feature enable $FEATURE_NAME", ""),
},
}
}
type Ls struct{}
type Data struct {
ID string
Feature projectv1.Feature
}
func (l *Ls) Run(ctx *clicontext.CLIContext) error {
features, err := ctx.List(clitypes.FeatureType)
if err != nil {
return err
}
writer := tables.NewFeature(ctx)
return writer.Write(features)
}
type Enable struct {
A_Answers string `desc:"Answer file in with key/value pairs in yaml or json"`
}
func (e *Enable) Run(ctx *clicontext.CLIContext) error {
if len(ctx.CLI.Args()) != 1 {
return fmt.Errorf("feature name is required")
}
resource, err := lookup.Lookup(ctx, ctx.CLI.Args()[0], clitypes.FeatureType)
if err != nil {
return err
}
answers, err := apply.ReadAnswers(e.A_Answers)
if err != nil {
return fmt.Errorf("failed to parse answer file [%s]: %v", e.A_Answers, err)
}
return flipEnableFlag(ctx, resource.Name, answers, true)
}
func flipEnableFlag(ctx *clicontext.CLIContext, featureName string, answers map[string]string, enable bool) error {
feature, err := ctx.Project.Features(ctx.SystemNamespace).Get(featureName, metav1.GetOptions{})
if err != nil {
return err
}
feature.Status.EnableOverride = &enable
if enable {
if len(answers) == 0 {
qs, err := questions.NewQuestions(feature.Spec.Questions, feature.Spec.Answers, true)
if err != nil {
return err
}
answers, err := qs.Ask()
if err != nil {
return err
}
feature.Spec.Answers = answers
} else {
if feature.Spec.Answers == nil {
feature.Spec.Answers = map[string]string{}
}
for k, v := range answers {
feature.Spec.Answers[k] = v
}
}
}
return ctx.UpdateObject(feature)
}