Skip to content

Commit ad0dcb1

Browse files
committed
update project errors and short project output
1 parent 17b46d7 commit ad0dcb1

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

pkg/cmd/cli/cmd/projects.go

+29-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"io"
66

77
// kapi "k8s.io/kubernetes/pkg/api"
8-
kapierrors "k8s.io/kubernetes/pkg/api/errors"
8+
// kapierrors "k8s.io/kubernetes/pkg/api/errors"
99
"k8s.io/kubernetes/pkg/client/restclient"
1010
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
1111
kubecmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
@@ -14,7 +14,7 @@ import (
1414
"github.com/openshift/origin/pkg/client"
1515
cliconfig "github.com/openshift/origin/pkg/cmd/cli/config"
1616
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
17-
"github.com/openshift/origin/pkg/project/api"
17+
// "github.com/openshift/origin/pkg/project/api"
1818

1919
"github.com/spf13/cobra"
2020
)
@@ -65,6 +65,8 @@ func NewCmdProjects(fullName string, f *clientcmd.Factory, out io.Writer) *cobra
6565
}
6666
},
6767
}
68+
69+
cmd.Flags().BoolVarP(&options.DisplayShort, "short", "q", false, "If true, display only the project names")
6870
return cmd
6971
}
7072

@@ -94,9 +96,6 @@ func (o *ProjectsOptions) Complete(f *clientcmd.Factory, args []string, out io.W
9496

9597
return nil
9698
}
97-
func (o ProjectsOptions) Validate() error {
98-
return nil
99-
}
10099

101100
// RunProjects lists all projects a user belongs to
102101
func (o ProjectsOptions) RunProjects() error {
@@ -108,29 +107,17 @@ func (o ProjectsOptions) RunProjects() error {
108107
currentProject := currentContext.Namespace
109108

110109
var currentProjectExists bool = false
110+
var currentProjectErr error = nil
111111

112112
client, err := o.ClientFn()
113113
if err != nil {
114114
return err
115115
}
116116

117117
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
131120
}
132-
133-
currentProjectExists = true
134121
}
135122

136123
defaultContextName := cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
@@ -142,35 +129,49 @@ func (o ProjectsOptions) RunProjects() error {
142129
switch len(projects) {
143130
case 0:
144131
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]))
147132
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+
}
149139
for _, project := range projects {
140+
count = count + 1
150141
displayName := project.Annotations["openshift.io/display-name"]
142+
linebreak := "\n"
151143
if len(displayName) == 0 {
152144
displayName = project.Annotations["displayName"]
153145
}
154146

155147
current = ""
156-
if currentProjectExists && currentProject == project.Name {
148+
if currentProjectExists && currentProject == project.Name && !o.DisplayShort {
157149
current = " (current)"
158150
}
159-
if len(displayName) > 0 && displayName != project.Name {
151+
if len(displayName) > 0 && displayName != project.Name && !o.DisplayShort {
160152
msg += fmt.Sprintf("\n* %s (%s)%s", displayName, project.Name, current)
161153
} 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)
163158
}
164159
}
165160
}
166161
fmt.Println(msg)
167162

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+
169171
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
170172
// a context name they didn't choose
171173
if config.CurrentContext == defaultContextName {
172174
fmt.Fprintf(out, "\nUsing project %q on server %q.\n", currentProject, clientCfg.Host)
173-
174175
} else {
175176
fmt.Fprintf(out, "\nUsing project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
176177
}

0 commit comments

Comments
 (0)