5
5
"io"
6
6
7
7
// kapi "k8s.io/kubernetes/pkg/api"
8
- kapierrors "k8s.io/kubernetes/pkg/api/errors"
8
+ // kapierrors "k8s.io/kubernetes/pkg/api/errors"
9
9
"k8s.io/kubernetes/pkg/client/restclient"
10
10
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
11
11
kubecmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
@@ -14,7 +14,7 @@ import (
14
14
"github.com/openshift/origin/pkg/client"
15
15
cliconfig "github.com/openshift/origin/pkg/cmd/cli/config"
16
16
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
17
- "github.com/openshift/origin/pkg/project/api"
17
+ // "github.com/openshift/origin/pkg/project/api"
18
18
19
19
"github.com/spf13/cobra"
20
20
)
@@ -65,6 +65,8 @@ func NewCmdProjects(fullName string, f *clientcmd.Factory, out io.Writer) *cobra
65
65
}
66
66
},
67
67
}
68
+
69
+ cmd .Flags ().BoolVarP (& options .DisplayShort , "short" , "q" , false , "If true, display only the project names" )
68
70
return cmd
69
71
}
70
72
@@ -94,9 +96,6 @@ func (o *ProjectsOptions) Complete(f *clientcmd.Factory, args []string, out io.W
94
96
95
97
return nil
96
98
}
97
- func (o ProjectsOptions ) Validate () error {
98
- return nil
99
- }
100
99
101
100
// RunProjects lists all projects a user belongs to
102
101
func (o ProjectsOptions ) RunProjects () error {
@@ -108,29 +107,17 @@ func (o ProjectsOptions) RunProjects() error {
108
107
currentProject := currentContext .Namespace
109
108
110
109
var currentProjectExists bool = false
110
+ var currentProjectErr error = nil
111
111
112
112
client , err := o .ClientFn ()
113
113
if err != nil {
114
114
return err
115
115
}
116
116
117
117
if len (currentProject ) > 0 {
118
- if o .DisplayShort {
119
- fmt .Fprintln (out , currentProject )
120
- return nil
121
- }
122
-
123
- if _ , err := client .Projects ().Get (currentProject ); err != nil {
124
- if kapierrors .IsNotFound (err ) {
125
- fmt .Printf ("the project %q specified in your config does not exist." , currentProject )
126
- }
127
- if clientcmd .IsForbidden (err ) {
128
- fmt .Printf ("you do not have rights to view project %q." , currentProject )
129
- }
130
- return err
118
+ if _ , currentProjectErr := client .Projects ().Get (currentProject ); currentProjectErr == nil {
119
+ currentProjectExists = true
131
120
}
132
-
133
- currentProjectExists = true
134
121
}
135
122
136
123
defaultContextName := cliconfig .GetContextNickname (currentContext .Namespace , currentContext .Cluster , currentContext .AuthInfo )
@@ -142,35 +129,49 @@ func (o ProjectsOptions) RunProjects() error {
142
129
switch len (projects ) {
143
130
case 0 :
144
131
msg += "You are not a member of any projects. You can request a project to be created with the 'new-project' command."
145
- case 1 :
146
- msg += fmt .Sprintf ("You have one project on this server: %s" , api .DisplayNameAndNameForProject (& projects [0 ]))
147
132
default :
148
- msg += "You have access to the following projects and can switch between them with 'oc project <projectname>':\n "
133
+ asterisk := ""
134
+ count := 0
135
+ if ! o .DisplayShort {
136
+ msg += "You have access to the following projects and can switch between them with 'oc project <projectname>':\n "
137
+ asterisk = "* "
138
+ }
149
139
for _ , project := range projects {
140
+ count = count + 1
150
141
displayName := project .Annotations ["openshift.io/display-name" ]
142
+ linebreak := "\n "
151
143
if len (displayName ) == 0 {
152
144
displayName = project .Annotations ["displayName" ]
153
145
}
154
146
155
147
current = ""
156
- if currentProjectExists && currentProject == project .Name {
148
+ if currentProjectExists && currentProject == project .Name && ! o . DisplayShort {
157
149
current = " (current)"
158
150
}
159
- if len (displayName ) > 0 && displayName != project .Name {
151
+ if len (displayName ) > 0 && displayName != project .Name && ! o . DisplayShort {
160
152
msg += fmt .Sprintf ("\n * %s (%s)%s" , displayName , project .Name , current )
161
153
} else {
162
- msg += fmt .Sprintf ("\n * %s%s" , project .Name , current )
154
+ if o .DisplayShort && count == 1 {
155
+ linebreak = ""
156
+ }
157
+ msg += fmt .Sprintf (linebreak + asterisk + "%s%s" , project .Name , current )
163
158
}
164
159
}
165
160
}
166
161
fmt .Println (msg )
167
162
168
- if len (projects ) > 0 {
163
+ if len (projects ) > 0 && ! o .DisplayShort {
164
+ if ! currentProjectExists {
165
+ if clientcmd .IsForbidden (currentProjectErr ) {
166
+ fmt .Printf ("you do not have rights to view project %q. Please switch to an existing one." , currentProject )
167
+ }
168
+ return currentProjectErr
169
+ }
170
+
169
171
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
170
172
// a context name they didn't choose
171
173
if config .CurrentContext == defaultContextName {
172
174
fmt .Fprintf (out , "\n Using project %q on server %q.\n " , currentProject , clientCfg .Host )
173
-
174
175
} else {
175
176
fmt .Fprintf (out , "\n Using project %q from context named %q on server %q.\n " , currentProject , config .CurrentContext , clientCfg .Host )
176
177
}
0 commit comments