|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "k8s.io/kubernetes/pkg/client/restclient" |
| 8 | + clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" |
| 9 | + kubecmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config" |
| 10 | + kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" |
| 11 | + |
| 12 | + "github.com/openshift/origin/pkg/client" |
| 13 | + cliconfig "github.com/openshift/origin/pkg/cmd/cli/config" |
| 14 | + "github.com/openshift/origin/pkg/cmd/util/clientcmd" |
| 15 | + "github.com/openshift/origin/pkg/project/api" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +type ProjectsOptions struct { |
| 21 | + Config clientcmdapi.Config |
| 22 | + ClientConfig *restclient.Config |
| 23 | + Client *client.Client |
| 24 | + Out io.Writer |
| 25 | + PathOptions *kubecmdconfig.PathOptions |
| 26 | + |
| 27 | + DisplayShort bool |
| 28 | +} |
| 29 | + |
| 30 | +const ( |
| 31 | + projectsLong = ` |
| 32 | +Display information about the current active project and existing projects on the server. |
| 33 | +
|
| 34 | +For advanced configuration, or to manage the contents of your config file, use the 'config' |
| 35 | +command.` |
| 36 | + |
| 37 | + projectsExample = ` # Display the projects that currently exist |
| 38 | + %[1]s` |
| 39 | +) |
| 40 | + |
| 41 | +// NewCmdProjects implements the OpenShift cli rollback command |
| 42 | +func NewCmdProjects(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command { |
| 43 | + options := &ProjectsOptions{} |
| 44 | + |
| 45 | + cmd := &cobra.Command{ |
| 46 | + Use: "projects", |
| 47 | + Short: "Display existing projects", |
| 48 | + Long: projectsLong, |
| 49 | + Example: fmt.Sprintf(projectsExample, fullName), |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + options.PathOptions = cliconfig.NewPathOptions(cmd) |
| 52 | + |
| 53 | + if err := options.Complete(f, args, out); err != nil { |
| 54 | + kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error())) |
| 55 | + } |
| 56 | + |
| 57 | + if err := options.RunProjects(); err != nil { |
| 58 | + kcmdutil.CheckErr(err) |
| 59 | + } |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + cmd.Flags().BoolVarP(&options.DisplayShort, "short", "q", false, "If true, display only the project names") |
| 64 | + return cmd |
| 65 | +} |
| 66 | + |
| 67 | +func (o *ProjectsOptions) Complete(f *clientcmd.Factory, args []string, out io.Writer) error { |
| 68 | + if len(args) > 0 { |
| 69 | + return fmt.Errorf("no arguments should be passed") |
| 70 | + } |
| 71 | + |
| 72 | + var err error |
| 73 | + o.Config, err = f.OpenShiftClientConfig.RawConfig() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + o.ClientConfig, err = f.OpenShiftClientConfig.ClientConfig() |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + o.Client, _, err = f.Clients() |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + o.Out = out |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// RunProjects lists all projects a user belongs to |
| 94 | +func (o ProjectsOptions) RunProjects() error { |
| 95 | + config := o.Config |
| 96 | + clientCfg := o.ClientConfig |
| 97 | + out := o.Out |
| 98 | + |
| 99 | + currentContext := config.Contexts[config.CurrentContext] |
| 100 | + currentProject := currentContext.Namespace |
| 101 | + |
| 102 | + var currentProjectExists bool = false |
| 103 | + var currentProjectErr error = nil |
| 104 | + |
| 105 | + client := o.Client |
| 106 | + |
| 107 | + if len(currentProject) > 0 { |
| 108 | + if _, currentProjectErr := client.Projects().Get(currentProject); currentProjectErr == nil { |
| 109 | + currentProjectExists = true |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + defaultContextName := cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo) |
| 114 | + |
| 115 | + var msg string |
| 116 | + var current string |
| 117 | + projects, err := getProjects(client) |
| 118 | + if err == nil { |
| 119 | + switch len(projects) { |
| 120 | + case 0: |
| 121 | + msg += "You are not a member of any projects. You can request a project to be created with the 'new-project' command." |
| 122 | + case 1: |
| 123 | + msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0])) |
| 124 | + default: |
| 125 | + asterisk := "" |
| 126 | + count := 0 |
| 127 | + if !o.DisplayShort { |
| 128 | + msg += "You have access to the following projects and can switch between them with 'oc project <projectname>':\n" |
| 129 | + asterisk = " * " |
| 130 | + } |
| 131 | + for _, project := range projects { |
| 132 | + count = count + 1 |
| 133 | + displayName := project.Annotations["openshift.io/display-name"] |
| 134 | + linebreak := "\n" |
| 135 | + if len(displayName) == 0 { |
| 136 | + displayName = project.Annotations["displayName"] |
| 137 | + } |
| 138 | + |
| 139 | + current = "" |
| 140 | + if currentProjectExists && currentProject == project.Name && !o.DisplayShort { |
| 141 | + current = " (current)" |
| 142 | + } |
| 143 | + if len(displayName) > 0 && displayName != project.Name && !o.DisplayShort { |
| 144 | + msg += fmt.Sprintf("\n * %s (%s)%s", displayName, project.Name, current) |
| 145 | + } else { |
| 146 | + if o.DisplayShort && count == 1 { |
| 147 | + linebreak = "" |
| 148 | + } |
| 149 | + msg += fmt.Sprintf(linebreak+asterisk+"%s%s", project.Name, current) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + fmt.Println(msg) |
| 154 | + |
| 155 | + if len(projects) > 0 && !o.DisplayShort { |
| 156 | + if !currentProjectExists { |
| 157 | + if clientcmd.IsForbidden(currentProjectErr) { |
| 158 | + fmt.Printf("you do not have rights to view project %q. Please switch to an existing one.", currentProject) |
| 159 | + } |
| 160 | + return currentProjectErr |
| 161 | + } |
| 162 | + |
| 163 | + // if they specified a project name and got a generated context, then only show the information they care about. They won't recognize |
| 164 | + // a context name they didn't choose |
| 165 | + if config.CurrentContext == defaultContextName { |
| 166 | + fmt.Fprintf(out, "\nUsing project %q on server %q.\n", currentProject, clientCfg.Host) |
| 167 | + } else { |
| 168 | + fmt.Fprintf(out, "\nUsing project %q from context named %q on server %q.\n", currentProject, config.CurrentContext, clientCfg.Host) |
| 169 | + } |
| 170 | + } |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + return err |
| 175 | +} |
0 commit comments