Browse code

Example CLI command

Clayton Coleman authored on 2016/04/18 12:34:26
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,63 @@
0
+package cmd
1
+
2
+import (
3
+	"fmt"
4
+	"io"
5
+	"os"
6
+
7
+	"github.com/spf13/cobra"
8
+
9
+	kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
10
+
11
+	cmdutil "github.com/openshift/origin/pkg/cmd/util"
12
+	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
13
+)
14
+
15
+const (
16
+	internalTYPELong = `
17
+Single line title
18
+
19
+Description body`
20
+
21
+	internalTYPEExample = `%s`
22
+)
23
+
24
+type TYPEOptions struct {
25
+	In          io.Reader
26
+	Out, ErrOut io.Writer
27
+}
28
+
29
+// NewCmdTYPE implements a TYPE command
30
+// This is an example type for templating.
31
+func NewCmdTYPE(fullName string, f *clientcmd.Factory, in io.Reader, out, errout io.Writer) *cobra.Command {
32
+	options := &TYPEOptions{
33
+		In:     in,
34
+		Out:    out,
35
+		ErrOut: errout,
36
+	}
37
+	cmd := &cobra.Command{
38
+		Use:     "NAME [...]",
39
+		Short:   "A short description",
40
+		Long:    internalTYPELong,
41
+		Example: fmt.Sprintf(internalTYPEExample, fullName),
42
+		Run: func(cmd *cobra.Command, args []string) {
43
+			kcmdutil.CheckErr(options.Complete(f, cmd, args))
44
+			kcmdutil.CheckErr(options.Validate())
45
+			if err := options.Run(); err != nil {
46
+				// TODO: move met to kcmdutil
47
+				if err == cmdutil.ErrExit {
48
+					os.Exit(1)
49
+				}
50
+				kcmdutil.CheckErr(err)
51
+			}
52
+		},
53
+	}
54
+	return cmd
55
+}
56
+
57
+func (o *TYPEOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []string) error {
58
+	return nil
59
+}
60
+
61
+func (o *TYPEOptions) Validate() error { return nil }
62
+func (o *TYPEOptions) Run() error      { return nil }