-
Notifications
You must be signed in to change notification settings - Fork 18
/
target_flags.go
133 lines (109 loc) · 4.07 KB
/
target_flags.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package target
import (
"github.com/spf13/pflag"
)
// TargetFlags represents the target cobra flags.
type TargetFlags interface {
// GardenName returns the value that is tied to the corresponding cobra flag.
GardenName() string
// ProjectName returns the value that is tied to the corresponding cobra flag.
ProjectName() string
// SeedName returns the value that is tied to the corresponding cobra flag.
SeedName() string
// ShootName returns the value that is tied to the corresponding cobra flag.
ShootName() string
// ControlPlane returns the value that is tied to the corresponding cobra flag.
ControlPlane() bool
// AddFlags binds all target configuration flags to a given flagset
AddFlags(flags *pflag.FlagSet)
// AddGardenFlag adds the garden flag to the provided flag set
AddGardenFlag(flags *pflag.FlagSet)
// AddProjectFlag adds the project flag to the provided flag set
AddProjectFlag(flags *pflag.FlagSet)
// AddSeedFlag adds the seed flag to the provided flag set
AddSeedFlag(flags *pflag.FlagSet)
// AddShootFlag adds the shoot flag to the provided flag set
AddShootFlag(flags *pflag.FlagSet)
// AddControlPlaneFlag adds the control-plane flag to the provided flag set
AddControlPlaneFlag(flags *pflag.FlagSet)
// ToTarget converts the flags to a target
ToTarget() Target
// IsTargetValid returns true if the set of given CLI flags is enough
// to create a meaningful target. For example, if only the SeedName is
// given, false is returned because for targeting a seed, the GardenName
// must also be given. If ShootName and GardenName are set, false is
// returned because either project or seed have to be given as well.
IsTargetValid() bool
// IsEmpty returns true if no flags were given by the user
IsEmpty() bool
}
func NewTargetFlags(garden, project, seed, shoot string, controlPlane bool) TargetFlags {
return &targetFlagsImpl{
gardenName: garden,
projectName: project,
seedName: seed,
shootName: shoot,
controlPlane: controlPlane,
}
}
type targetFlagsImpl struct {
gardenName string
projectName string
seedName string
shootName string
controlPlane bool
}
func (tf *targetFlagsImpl) GardenName() string {
return tf.gardenName
}
func (tf *targetFlagsImpl) ProjectName() string {
return tf.projectName
}
func (tf *targetFlagsImpl) SeedName() string {
return tf.seedName
}
func (tf *targetFlagsImpl) ShootName() string {
return tf.shootName
}
func (tf *targetFlagsImpl) ControlPlane() bool {
return tf.controlPlane
}
func (tf *targetFlagsImpl) AddFlags(flags *pflag.FlagSet) {
tf.AddGardenFlag(flags)
tf.AddProjectFlag(flags)
tf.AddSeedFlag(flags)
tf.AddShootFlag(flags)
tf.AddControlPlaneFlag(flags)
}
func (tf *targetFlagsImpl) AddGardenFlag(flags *pflag.FlagSet) {
flags.StringVar(&tf.gardenName, "garden", "", "target the given garden cluster")
}
func (tf *targetFlagsImpl) AddProjectFlag(flags *pflag.FlagSet) {
flags.StringVar(&tf.projectName, "project", "", "target the given project")
}
func (tf *targetFlagsImpl) AddSeedFlag(flags *pflag.FlagSet) {
flags.StringVar(&tf.seedName, "seed", "", "target the given seed cluster")
}
func (tf *targetFlagsImpl) AddShootFlag(flags *pflag.FlagSet) {
flags.StringVar(&tf.shootName, "shoot", "", "target the given shoot cluster")
}
func (tf *targetFlagsImpl) AddControlPlaneFlag(flags *pflag.FlagSet) {
flags.BoolVar(&tf.controlPlane, "control-plane", tf.controlPlane, "target control plane of shoot, use together with shoot argument")
}
func (tf *targetFlagsImpl) ToTarget() Target {
return NewTarget(tf.gardenName, tf.projectName, tf.seedName, tf.shootName).WithControlPlane(tf.controlPlane)
}
func (tf *targetFlagsImpl) IsEmpty() bool {
return tf.gardenName == "" && tf.projectName == "" && tf.seedName == "" && tf.shootName == "" && !tf.controlPlane
}
func (tf *targetFlagsImpl) IsTargetValid() bool {
// garden name is always required for a complete set of flags
if tf.gardenName == "" {
return false
}
return tf.ToTarget().Validate() == nil
}