|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "runtime" |
| 10 | + |
| 11 | + "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/executils" |
| 12 | + "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github" |
| 13 | + ns "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/namespace" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var openDeploymentCmd = &cobra.Command{ |
| 18 | + Use: "open", |
| 19 | + Short: "Open a renku deployment in the browser", |
| 20 | + Run: openDeployment, |
| 21 | +} |
| 22 | + |
| 23 | +func openDeployment(cmd *cobra.Command, args []string) { |
| 24 | + ctx := context.Background() |
| 25 | + |
| 26 | + if namespace == "" { |
| 27 | + cli, err := github.NewGitHubCLI("") |
| 28 | + if err != nil { |
| 29 | + fmt.Println(err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + namespace, err = ns.FindCurrentNamespace(ctx, cli) |
| 33 | + if err != nil { |
| 34 | + fmt.Println(err) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // TODO: Can we derive the URL by inspecting ingresses in the k8s namespace? |
| 40 | + openURL, err := url.Parse(fmt.Sprintf("https://%s.dev.renku.ch", namespace)) |
| 41 | + if err != nil { |
| 42 | + fmt.Println(err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + openURLStr := openURL.String() |
| 46 | + fmt.Printf("Open URL: %s\n", openURLStr) |
| 47 | + |
| 48 | + if runtime.GOOS == "darwin" { |
| 49 | + cmd := exec.CommandContext(ctx, "open", openURLStr) |
| 50 | + _, err = executils.FormatOutput(cmd.Output()) |
| 51 | + if err != nil { |
| 52 | + fmt.Println(err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if runtime.GOOS == "linux" { |
| 59 | + cmd := exec.CommandContext(ctx, "xdg-open", openURLStr) |
| 60 | + _, err = executils.FormatOutput(cmd.Output()) |
| 61 | + if err != nil { |
| 62 | + fmt.Println(err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Printf("Sorry, I do not know how to \"open\" on '%s'\n", runtime.GOOS) |
| 69 | + os.Exit(1) |
| 70 | +} |
| 71 | + |
| 72 | +func init() { |
| 73 | + openDeploymentCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "k8s namespace") |
| 74 | +} |
0 commit comments