1
1
package cmd
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"io"
6
- "strings"
7
7
8
8
"github.com/gonum/graph/encoding/dot"
9
9
"github.com/spf13/cobra"
@@ -14,9 +14,10 @@ import (
14
14
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
15
15
)
16
16
17
- const (
18
- StatusRecommendedName = "status"
17
+ // StatusRecommendedName is the recommended command name.
18
+ const StatusRecommendedName = "status"
19
19
20
+ const (
20
21
statusLong = `
21
22
Show a high level overview of the current project
22
23
@@ -28,36 +29,59 @@ oc describe deploymentConfig, oc describe service).
28
29
You can specify an output format of "-o dot" to have this command output the generated status
29
30
graph in DOT format that is suitable for use by the "dot" command.`
30
31
31
- statusExample = ` # Show an overview of the current project
32
- $ %[1]s`
32
+ statusExample = ` # See an overview of the current project.
33
+ $ %[1]s
34
+
35
+ # Export the overview of the current project in an svg file.
36
+ $ %[1]s -o dot | dot -T svg -o project.svg
37
+
38
+ # See an overview of the current project including details for any identified issues.
39
+ $ %[1]s -v`
33
40
)
34
41
35
- // NewCmdStatus implements the OpenShift cli status command
42
+ // StatusOptions contains all the necessary options for the Openshift cli status command.
43
+ type StatusOptions struct {
44
+ namespace string
45
+ outputFormat string
46
+ describer * describe.ProjectStatusDescriber
47
+ out io.Writer
48
+ verbose bool
49
+ }
50
+
51
+ // NewCmdStatus implements the OpenShift cli status command.
36
52
func NewCmdStatus (name , fullName string , f * clientcmd.Factory , out io.Writer ) * cobra.Command {
37
- outputFormat := ""
53
+ opts := & StatusOptions {}
38
54
39
55
cmd := & cobra.Command {
40
- Use : "status" ,
56
+ Use : fmt . Sprintf ( "%s [-o dot | -v ]" , StatusRecommendedName ) ,
41
57
Short : "Show an overview of the current project" ,
42
58
Long : statusLong ,
43
59
Example : fmt .Sprintf (statusExample , fullName ),
44
60
Run : func (cmd * cobra.Command , args []string ) {
45
- if strings .ToLower (outputFormat ) == "dot" {
46
- cmdutil .CheckErr (RunGraph (f , out ))
47
- return
61
+ err := opts .Complete (f , cmd , args , out )
62
+ cmdutil .CheckErr (err )
63
+
64
+ if err := opts .Validate (); err != nil {
65
+ cmdutil .CheckErr (cmdutil .UsageError (cmd , err .Error ()))
48
66
}
49
67
50
- cmdutil .CheckErr (RunStatus (f , out ))
68
+ err = opts .RunStatus ()
69
+ cmdutil .CheckErr (err )
51
70
},
52
71
}
53
72
54
- cmd .Flags ().StringVarP (& outputFormat , "output" , "o" , outputFormat , "Output format. One of: dot." )
73
+ cmd .Flags ().StringVarP (& opts .outputFormat , "output" , "o" , opts .outputFormat , "Output format. One of: dot." )
74
+ cmd .Flags ().BoolVarP (& opts .verbose , "verbose" , "v" , opts .verbose , "See details for resolving issues." )
55
75
56
76
return cmd
57
77
}
58
78
59
- // RunStatus contains all the necessary functionality for the OpenShift cli status command
60
- func RunStatus (f * clientcmd.Factory , out io.Writer ) error {
79
+ // Complete completes the options for the Openshift cli status command.
80
+ func (o * StatusOptions ) Complete (f * clientcmd.Factory , cmd * cobra.Command , args []string , out io.Writer ) error {
81
+ if len (args ) > 0 {
82
+ return cmdutil .UsageError (cmd , "no arguments should be provided" )
83
+ }
84
+
61
85
client , kclient , err := f .Clients ()
62
86
if err != nil {
63
87
return err
@@ -72,45 +96,53 @@ func RunStatus(f *clientcmd.Factory, out io.Writer) error {
72
96
if err != nil {
73
97
return err
74
98
}
99
+ o .namespace = namespace
75
100
76
- describer := & describe.ProjectStatusDescriber {K : kclient , C : client , Server : config .Host }
77
- s , err := describer .Describe (namespace , "" )
78
- if err != nil {
79
- return err
80
- }
101
+ o .describer = & describe.ProjectStatusDescriber {K : kclient , C : client , Server : config .Host , Suggest : o .verbose }
102
+
103
+ o .out = out
81
104
82
- fmt .Fprintf (out , s )
83
105
return nil
84
106
}
85
107
86
- // RunGraph contains all the necessary functionality for the OpenShift cli graph command
87
- func RunGraph (f * clientcmd.Factory , out io.Writer ) error {
88
- client , kclient , err := f .Clients ()
89
- if err != nil {
90
- return err
108
+ // Validate validates the options for the Openshift cli status command.
109
+ func (o StatusOptions ) Validate () error {
110
+ if len (o .outputFormat ) != 0 && o .outputFormat != "dot" {
111
+ return fmt .Errorf ("invalid output format provided: %s" , o .outputFormat )
91
112
}
92
-
93
- config , err := f .OpenShiftClientConfig .ClientConfig ()
94
- if err != nil {
95
- return err
96
- }
97
-
98
- namespace , _ , err := f .DefaultNamespace ()
99
- if err != nil {
100
- return err
101
- }
102
-
103
- describer := & describe.ProjectStatusDescriber {K : kclient , C : client , Server : config .Host }
104
- g , _ , err := describer .MakeGraph (namespace )
105
- if err != nil {
106
- return err
113
+ if len (o .outputFormat ) > 0 && o .verbose {
114
+ return errors .New ("cannot provide suggestions when output format is dot" )
107
115
}
116
+ return nil
117
+ }
108
118
109
- data , err := dot .Marshal (g , namespace , "" , " " , false )
110
- if err != nil {
111
- return err
119
+ // RunStatus contains all the necessary functionality for the OpenShift cli status command.
120
+ func (o StatusOptions ) RunStatus () error {
121
+ var (
122
+ s string
123
+ err error
124
+ )
125
+
126
+ switch o .outputFormat {
127
+ case "" :
128
+ s , err = o .describer .Describe (o .namespace , "" )
129
+ if err != nil {
130
+ return err
131
+ }
132
+ case "dot" :
133
+ g , _ , err := o .describer .MakeGraph (o .namespace )
134
+ if err != nil {
135
+ return err
136
+ }
137
+ data , err := dot .Marshal (g , o .namespace , "" , " " , false )
138
+ if err != nil {
139
+ return err
140
+ }
141
+ s = string (data )
142
+ default :
143
+ return fmt .Errorf ("invalid output format provided: %s" , o .outputFormat )
112
144
}
113
145
114
- fmt .Fprintf (out , "%s" , string ( data ) )
146
+ fmt .Fprintf (o . out , s )
115
147
return nil
116
148
}
0 commit comments