forked from openshift/origin
/
login.go
65 lines (50 loc) · 2.35 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/cmd/cli/config"
"github.com/openshift/origin/pkg/cmd/templates"
osclientcmd "github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const longDescription = `Logs in to the OpenShift server and saves a config file that
will be used by subsequent commands.
First-time users of the OpenShift client must run this command to configure the server,
establish a session against it, and save it to a configuration file. The default
configuration will be in your home directory under ".config/openshift/.config".
The information required to login, like username and password, a session token, or
the server details, can be provided through flags. If not provided, the command will
prompt for user input as needed.
`
func NewCmdLogin(f *osclientcmd.Factory, reader io.Reader, out io.Writer) *cobra.Command {
options := &LoginOptions{}
cmds := &cobra.Command{
Use: "login [--username=<username>] [--password=<password>] [--server=<server>] [--context=<context>] [--certificate-authority=<path>]",
Short: "Logs in and save the configuration",
Long: longDescription,
Run: func(cmd *cobra.Command, args []string) {
options.Reader = reader
options.ClientConfig = f.OpenShiftClientConfig
checkErr(options.GatherInfo())
forcePath := cmdutil.GetFlagString(cmd, config.OpenShiftConfigFlagName)
options.PathToSaveConfig = forcePath
newFileCreated, err := options.SaveConfig()
checkErr(err)
if newFileCreated {
fmt.Println("Welcome to OpenShift! See 'osc help' for to get started.")
}
},
}
// TODO flags below should be DE-REGISTERED from the persistent flags and kept only here.
// Login is the only command that can negotiate a session token against the auth server.
cmds.Flags().StringVarP(&options.Username, "username", "u", "", "Username, will prompt if not provided")
cmds.Flags().StringVarP(&options.Password, "password", "p", "", "Password, will prompt if not provided")
templater := templates.Templater{
UsageTemplate: templates.CliUsageTemplate(),
Exposed: []string{"server", "username", "password", "certificate-authority", "insecure-skip-tls-verify", "context"},
}
cmds.SetUsageFunc(templater.UsageFunc())
cmds.SetHelpTemplate(templates.CliHelpTemplate())
return cmds
}