9603c66e |
package cmd
import (
"fmt"
"io" |
3286bfd6 |
"sort" |
9603c66e |
"k8s.io/kubernetes/pkg/client/restclient" |
d00a6ef4 |
kclient "k8s.io/kubernetes/pkg/client/unversioned" |
37b6c5f1 |
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" |
9603c66e |
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/client"
cliconfig "github.com/openshift/origin/pkg/cmd/cli/config" |
6267dded |
"github.com/openshift/origin/pkg/cmd/templates" |
9603c66e |
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/project/api"
"github.com/spf13/cobra"
)
type ProjectsOptions struct {
Config clientcmdapi.Config
ClientConfig *restclient.Config
Client *client.Client |
d00a6ef4 |
KubeClient kclient.Interface |
9603c66e |
Out io.Writer |
37b6c5f1 |
PathOptions *kclientcmd.PathOptions |
9603c66e |
|
5913cbe3 |
// internal strings
CommandName string
|
9603c66e |
DisplayShort bool
}
|
3286bfd6 |
// SortByProjectName is sort
type SortByProjectName []api.Project
func (p SortByProjectName) Len() int {
return len(p)
}
func (p SortByProjectName) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p SortByProjectName) Less(i, j int) bool {
return p[i].Name < p[j].Name
}
|
6267dded |
var (
projectsLong = templates.LongDesc(`
Display information about the current active project and existing projects on the server. |
9603c66e |
|
6267dded |
For advanced configuration, or to manage the contents of your config file, use the 'config'
command.`) |
9603c66e |
)
// NewCmdProjects implements the OpenShift cli rollback command
func NewCmdProjects(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &ProjectsOptions{}
cmd := &cobra.Command{ |
55a0a2fd |
Use: "projects",
Short: "Display existing projects",
Long: projectsLong, |
9603c66e |
Run: func(cmd *cobra.Command, args []string) {
options.PathOptions = cliconfig.NewPathOptions(cmd)
|
5913cbe3 |
if err := options.Complete(f, args, fullName, out); err != nil { |
9603c66e |
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.RunProjects(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().BoolVarP(&options.DisplayShort, "short", "q", false, "If true, display only the project names")
return cmd
}
|
5913cbe3 |
func (o *ProjectsOptions) Complete(f *clientcmd.Factory, args []string, commandName string, out io.Writer) error { |
9603c66e |
if len(args) > 0 {
return fmt.Errorf("no arguments should be passed")
}
|
5913cbe3 |
o.CommandName = commandName
|
9603c66e |
var err error
o.Config, err = f.OpenShiftClientConfig.RawConfig()
if err != nil {
return err
}
o.ClientConfig, err = f.OpenShiftClientConfig.ClientConfig()
if err != nil {
return err
}
|
d00a6ef4 |
o.Client, o.KubeClient, err = f.Clients() |
9603c66e |
if err != nil {
return err
}
o.Out = out
return nil
}
// RunProjects lists all projects a user belongs to
func (o ProjectsOptions) RunProjects() error {
config := o.Config
clientCfg := o.ClientConfig
out := o.Out
|
37608e05 |
var currentProject string |
9603c66e |
currentContext := config.Contexts[config.CurrentContext] |
37608e05 |
if currentContext != nil {
currentProject = currentContext.Namespace
} |
9603c66e |
|
3286bfd6 |
var currentProjectExists bool
var currentProjectErr error |
9603c66e |
client := o.Client
if len(currentProject) > 0 { |
d00a6ef4 |
if currentProjectErr := confirmProjectAccess(currentProject, o.Client, o.KubeClient); currentProjectErr == nil { |
9603c66e |
currentProjectExists = true
}
}
|
37608e05 |
var defaultContextName string
if currentContext != nil {
defaultContextName = cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
} |
9603c66e |
var msg string |
d00a6ef4 |
projects, err := getProjects(client, o.KubeClient) |
9603c66e |
if err == nil {
switch len(projects) {
case 0:
msg += "You are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1: |
b386d875 |
if o.DisplayShort {
msg += fmt.Sprintf("%s", api.DisplayNameAndNameForProject(&projects[0]))
} else {
msg += fmt.Sprintf("You have one project on this server: %q.", api.DisplayNameAndNameForProject(&projects[0]))
} |
9603c66e |
default:
asterisk := ""
count := 0
if !o.DisplayShort { |
5913cbe3 |
msg += fmt.Sprintf("You have access to the following projects and can switch between them with '%s project <projectname>':\n", o.CommandName) |
9603c66e |
} |
3286bfd6 |
sort.Sort(SortByProjectName(projects)) |
9603c66e |
for _, project := range projects {
count = count + 1
displayName := project.Annotations["openshift.io/display-name"]
linebreak := "\n"
if len(displayName) == 0 {
displayName = project.Annotations["displayName"]
}
|
b386d875 |
if currentProjectExists && !o.DisplayShort {
asterisk = " "
if currentProject == project.Name {
asterisk = " * "
} |
9603c66e |
}
if len(displayName) > 0 && displayName != project.Name && !o.DisplayShort { |
d650f0ad |
msg += fmt.Sprintf("\n"+asterisk+"%s - %s", project.Name, displayName) |
9603c66e |
} else {
if o.DisplayShort && count == 1 {
linebreak = ""
} |
b386d875 |
msg += fmt.Sprintf(linebreak+asterisk+"%s", project.Name) |
9603c66e |
}
}
}
fmt.Println(msg)
if len(projects) > 0 && !o.DisplayShort {
if !currentProjectExists {
if clientcmd.IsForbidden(currentProjectErr) { |
eebdf7b4 |
fmt.Printf("You do not have rights to view project %q. Please switch to an existing one.\n", currentProject) |
9603c66e |
}
return currentProjectErr
}
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
if config.CurrentContext == defaultContextName {
fmt.Fprintf(out, "\nUsing project %q on server %q.\n", currentProject, clientCfg.Host)
} else {
fmt.Fprintf(out, "\nUsing project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host)
}
}
return nil
}
return err
} |