-
Notifications
You must be signed in to change notification settings - Fork 506
/
cmds_misc.go
100 lines (86 loc) · 2.85 KB
/
cmds_misc.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
package cli
import (
"context"
"fmt"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/v2/pkg/kates"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/client/actions"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/proc"
)
// getClusterID is a simple command that makes it easier for users to
// figure out what their cluster ID is. For now this is just used when
// people are making licenses for air-gapped environments
func ClusterIdCommand() *cobra.Command {
return &cobra.Command{
Use: "current-cluster-id",
Args: cobra.NoArgs,
Short: "Get cluster ID for your kubernetes cluster",
Long: "Get cluster ID for your kubernetes cluster, mostly used for licenses in air-gapped environments",
RunE: func(cmd *cobra.Command, _ []string) error {
client, err := kates.NewClientFromConfigFlags(kubeConfig)
if err != nil {
return err
}
clusterID, err := actions.GetClusterID(cmd.Context(), client)
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Cluster ID: %s\n", clusterID)
return nil
},
}
}
func connectCommand() *cobra.Command {
return &cobra.Command{
Use: "connect [flags] [-- <command to run while connected>]",
Args: cobra.ArbitraryArgs,
Short: "Connect to a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return withConnector(cmd, true, func(_ context.Context, _ connector.ConnectorClient, _ *connector.ConnectInfo) error {
return nil
})
}
return withConnector(cmd, false, func(ctx context.Context, _ connector.ConnectorClient, _ *connector.ConnectInfo) error {
return proc.Run(ctx, nil, args[0], args[1:]...)
})
},
}
}
func dashboardCommand() *cobra.Command {
return &cobra.Command{
Use: "dashboard",
Args: cobra.NoArgs,
Short: "Open the dashboard in a web page",
RunE: func(cmd *cobra.Command, args []string) error {
cloudCfg := client.GetConfig(cmd.Context()).Cloud
// Ensure we're logged in
resultCode, err := cliutil.EnsureLoggedIn(cmd.Context(), "")
if err != nil {
return err
}
if resultCode == connector.LoginResult_OLD_LOGIN_REUSED {
// The LoginFlow takes the user to the dashboard, so we only need to
// explicitly take the user to the dashboard if they were already
// logged in.
if err := browser.OpenURL(fmt.Sprintf("https://%s/cloud/preview", cloudCfg.SystemaHost)); err != nil {
return err
}
}
return nil
}}
}
func quitCommand() *cobra.Command {
return &cobra.Command{
Use: "quit",
Args: cobra.NoArgs,
Short: "Tell telepresence daemon to quit",
RunE: func(cmd *cobra.Command, _ []string) error {
return cliutil.QuitDaemon(cmd.Context())
},
}
}