-
Notifications
You must be signed in to change notification settings - Fork 18
/
options.go
138 lines (110 loc) · 3.86 KB
/
options.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
134
135
136
137
138
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package base
import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/yaml"
"github.com/gardener/gardenctl-v2/internal/util"
)
//go:generate mockgen -destination=./mocks/mock_options.go -package=mocks github.com/gardener/gardenctl-v2/pkg/cmd/base Runnable
// Runnable is the base interface for command options.
type Runnable interface {
// Complete adapts from the command line args to the data required.
Complete(util.Factory, *cobra.Command, []string) error
// Validate validates the provided options.
Validate() error
// Run does the actual work of the command.
Run(util.Factory) error
}
// Options contains all settings that are used across all commands in gardenctl.
type Options struct {
// IOStreams provides the standard names for iostreams
IOStreams util.IOStreams
// Output defines the output format of the version information. Either 'yaml' or 'json'
Output string
}
var _ Runnable = &Options{}
// WrapRunE creates a cobra RunE function that has access to the factory.
func WrapRunE(o Runnable, f util.Factory) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if err := o.Complete(f, cmd, args); err != nil {
return fmt.Errorf("failed to complete command options: %w", err)
}
if err := o.Validate(); err != nil {
return err
}
return o.Run(f)
}
}
// NewOptions returns initialized Options.
func NewOptions(ioStreams util.IOStreams) *Options {
return &Options{
IOStreams: ioStreams,
}
}
// AddFlags adds flags to adjust the output to a cobra command.
func (o *Options) AddFlags(flags *pflag.FlagSet) {
flags.StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
}
// RegisterCompletionsForOutputFlag adds output flag completion to the command.
func (o *Options) RegisterCompletionsForOutputFlag(cmd *cobra.Command) {
utilruntime.Must(cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return o.AllowedOutputFormats(), cobra.ShellCompDirectiveNoFileComp
}))
}
// AllowedOutputFormats returns the allowed formats for the output flag.
func (o *Options) AllowedOutputFormats() []string {
return []string{"json", "yaml"}
}
// PrintObject prints an object to IOStreams.out, using o.Output to print in the selected output format.
func (o *Options) PrintObject(obj interface{}) error {
switch o.Output {
case "":
if _, ok := obj.(fmt.Stringer); ok {
_, err := fmt.Fprintf(o.IOStreams.Out, "%s", obj)
return err
}
_, err := fmt.Fprintf(o.IOStreams.Out, "%v", obj)
return err
case "yaml":
marshalled, err := yaml.Marshal(&obj)
if err != nil {
return err
}
_, err = fmt.Fprint(o.IOStreams.Out, string(marshalled))
return err
case "json":
marshalled, err := json.MarshalIndent(&obj, "", " ")
if err != nil {
return err
}
_, err = fmt.Fprintln(o.IOStreams.Out, string(marshalled))
return err
default:
// There is a bug in the program if we hit this case.
// However, we follow a policy of never panicking.
return fmt.Errorf("options were not validated: --output=%q should have been rejected", o.Output)
}
}
// Validate validates the provided options.
func (o *Options) Validate() error {
if o.Output != "" && o.Output != "yaml" && o.Output != "json" {
return errors.New("--output must be either 'yaml' or 'json'")
}
return nil
}
// Complete adapts from the command line args to the data required.
func (o *Options) Complete(_ util.Factory, _ *cobra.Command, _ []string) error {
return nil
}
// Run does the actual work of the command.
func (o *Options) Run(util.Factory) error {
return errors.New("method \"Run\" not implemented")
}