/
console.go
129 lines (114 loc) · 4.09 KB
/
console.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"errors"
"fmt"
"io"
"os"
"github.com/crc-org/crc/v2/pkg/crc/api/client"
"github.com/crc-org/crc/v2/pkg/crc/daemonclient"
crcErrors "github.com/crc-org/crc/v2/pkg/crc/errors"
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
var (
consolePrintURL bool
consolePrintCredentials bool
)
func init() {
addOutputFormatFlag(consoleCmd)
consoleCmd.Flags().BoolVar(&consolePrintURL, "url", false, "Print the URL for the OpenShift Web Console")
consoleCmd.Flags().BoolVar(&consolePrintCredentials, "credentials", false, "Print the credentials for the OpenShift Web Console")
rootCmd.AddCommand(consoleCmd)
}
// consoleCmd represents the console command
var consoleCmd = &cobra.Command{
Use: "console",
Aliases: []string{"dashboard"},
Short: "Open the OpenShift Web Console in the default browser",
Long: `Open the OpenShift Web Console in the default browser or print its URL or credentials`,
RunE: func(cmd *cobra.Command, args []string) error {
return runConsole(os.Stdout, daemonclient.New(), consolePrintURL, consolePrintCredentials, outputFormat)
},
}
func showConsole(client *daemonclient.Client) (*client.ConsoleResult, error) {
res, err := client.APIClient.WebconsoleURL()
return res, err
}
func runConsole(writer io.Writer, client *daemonclient.Client, consolePrintURL, consolePrintCredentials bool, outputFormat string) error {
result, err := showConsole(client)
return render(&consoleResult{
Success: err == nil,
state: toState(result),
ClusterConfig: toConsoleClusterConfig(result),
Error: crcErrors.ToSerializableError(err),
consolePrintURL: consolePrintURL,
consolePrintCredentials: consolePrintCredentials,
}, writer, outputFormat)
}
type consoleResult struct {
Success bool `json:"success"`
state state.State
Error *crcErrors.SerializableError `json:"error,omitempty"`
ClusterConfig *clusterConfig `json:"clusterConfig,omitempty"`
consolePrintURL bool
consolePrintCredentials bool
}
func (s *consoleResult) prettyPrintTo(writer io.Writer) error {
if s.Error != nil {
return s.Error
}
if s.consolePrintURL {
if _, err := fmt.Fprintln(writer, s.ClusterConfig.WebConsoleURL); err != nil {
return err
}
}
if s.consolePrintCredentials {
if _, err := fmt.Fprintf(writer, "To login as a regular user, run 'oc login -u %s -p %s %s'.\n",
s.ClusterConfig.DeveloperCredentials.Username, s.ClusterConfig.DeveloperCredentials.Password, s.ClusterConfig.URL); err != nil {
return err
}
if _, err := fmt.Fprintf(writer, "To login as an admin, run 'oc login -u %s -p %s %s'\n",
s.ClusterConfig.AdminCredentials.Username, s.ClusterConfig.AdminCredentials.Password, s.ClusterConfig.URL); err != nil {
return err
}
}
if s.consolePrintURL || s.consolePrintCredentials {
return nil
}
if s.state != state.Running {
return errors.New("The OpenShift cluster is not running, cannot open the OpenShift Web Console")
}
if _, err := fmt.Fprintln(writer, "Opening the OpenShift Web Console in the default browser..."); err != nil {
return err
}
if err := browser.OpenURL(s.ClusterConfig.WebConsoleURL); err != nil {
return fmt.Errorf("Failed to open the OpenShift Web Console, you can access it by opening %s in your web browser", s.ClusterConfig.WebConsoleURL)
}
return nil
}
func toState(result *client.ConsoleResult) state.State {
if result == nil {
return state.Error
}
return result.State
}
func toConsoleClusterConfig(result *client.ConsoleResult) *clusterConfig {
if result == nil {
return nil
}
return &clusterConfig{
ClusterType: result.ClusterConfig.ClusterType,
ClusterCACert: result.ClusterConfig.ClusterCACert,
WebConsoleURL: result.ClusterConfig.WebConsoleURL,
URL: result.ClusterConfig.ClusterAPI,
AdminCredentials: credentials{
Username: "kubeadmin",
Password: result.ClusterConfig.KubeAdminPass,
},
DeveloperCredentials: credentials{
Username: "developer",
Password: "developer",
},
}
}