-
Notifications
You must be signed in to change notification settings - Fork 12
/
resource_ref_flags.go
51 lines (39 loc) · 1.23 KB
/
resource_ref_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
package resource
import (
"fmt"
"strings"
cmdcore "github.com/carvel-dev/kwt/pkg/kwt/cmd/core"
ctlres "github.com/carvel-dev/kwt/pkg/kwt/resources"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type ResourceFlags struct {
Ref ctlres.ResourceRef
}
func (s *ResourceFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory) {
resRef := NewResourceRefFlag(&s.Ref)
cmd.Flags().VarP(resRef, "resource-type", "t", "Specified resource type (format: 'group/version/resource')")
}
type ResourceRefFlag struct {
value *ctlres.ResourceRef
}
var _ pflag.Value = &ResourceRefFlag{}
func NewResourceRefFlag(value *ctlres.ResourceRef) *ResourceRefFlag {
return &ResourceRefFlag{value}
}
func (s *ResourceRefFlag) Set(val string) error {
pieces := strings.Split(val, "/")
if len(pieces) != 3 {
return fmt.Errorf("Expected resource ref '%s' to be in format 'group/version/resource'", val)
}
gvr := schema.GroupVersionResource{
Group: pieces[0],
Version: pieces[1],
Resource: pieces[2],
}
*s.value = ctlres.ResourceRef{gvr}
return nil
}
func (s *ResourceRefFlag) Type() string { return "string" }
func (s *ResourceRefFlag) String() string { return "" } // default for usage