-
Notifications
You must be signed in to change notification settings - Fork 4
/
view.go
56 lines (47 loc) · 1.58 KB
/
view.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
package clusters
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/toqueteos/webbrowser"
"github.com/jetstack/jsctl/internal/client"
"github.com/jetstack/jsctl/internal/cluster"
internalerrors "github.com/jetstack/jsctl/internal/command/errors"
"github.com/jetstack/jsctl/internal/command/types"
"github.com/jetstack/jsctl/internal/config"
)
// View returns a new cobra.Command for viewing a cluster in the JSCP api
func View(run types.RunFunc, apiURL *string) *cobra.Command {
return &cobra.Command{
Use: "view name",
Short: "Opens a browser window to the cluster's dashboard",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: run(func(ctx context.Context, args []string) error {
cnf, ok := config.FromContext(ctx)
if !ok || cnf.Organization == "" {
return internalerrors.ErrNoOrganizationName
}
http := client.New(ctx, *apiURL)
name := args[0]
if name == "" {
return errors.New("you must specify a cluster name")
}
clusters, err := cluster.List(ctx, http, cnf.Organization)
if err != nil {
return fmt.Errorf("failed to list clusters: %w", err)
}
const urlFormat = "https://platform.jetstack.io/org/%s/certinventory/cluster/%s/certificates"
for _, cl := range clusters {
if cl.Name == name {
url := fmt.Sprintf(urlFormat, cnf.Organization, name)
if err = webbrowser.Open(url); err != nil {
fmt.Printf("Navigate to the URL below to view your cluster:\n%s\n", url)
}
return nil
}
}
return fmt.Errorf("cluster %s does not exist in organization %s", name, cnf.Organization)
}),
}
}