Skip to content

Commit a79f183

Browse files
committed
feat: add open command to view a deployment
1 parent ee3a6d9 commit a79f183

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

pkg/cmd/opendeployment.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

pkg/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func runRoot(cmd *cobra.Command, args []string) error {
3030
func init() {
3131
rootCmd.AddCommand(cleanupDeploymentCmd)
3232
rootCmd.AddCommand(copyKeycloakAdminPasswordCmd)
33+
rootCmd.AddCommand(openDeploymentCmd)
3334
rootCmd.AddCommand(versionCmd)
3435
}
3536

pkg/executils/formatoutput.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package executils
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
func FormatOutput(output []byte, err error) ([]byte, error) {
9+
if err != nil {
10+
if ee, ok := err.(*exec.ExitError); ok {
11+
return output, fmt.Errorf("%s", string(ee.Stderr))
12+
}
13+
}
14+
return output, err
15+
}

pkg/github/cli.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"os/exec"
7+
8+
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/executils"
79
)
810

911
type GitHubCLI struct {
@@ -27,5 +29,5 @@ func NewGitHubCLI(gh string) (*GitHubCLI, error) {
2729

2830
func (cli *GitHubCLI) RunCmd(ctx context.Context, arg ...string) ([]byte, error) {
2931
cmd := exec.CommandContext(ctx, cli.gh, arg...)
30-
return cmd.Output()
32+
return executils.FormatOutput(cmd.Output())
3133
}

pkg/helm/cli.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"os/exec"
7+
8+
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/executils"
79
)
810

911
type HelmCLI struct {
@@ -27,5 +29,5 @@ func NewHelmCLI(helm string) (cli *HelmCLI, err error) {
2729

2830
func (cli *HelmCLI) RunCmd(ctx context.Context, arg ...string) ([]byte, error) {
2931
cmd := exec.CommandContext(ctx, cli.helm, arg...)
30-
return cmd.Output()
32+
return executils.FormatOutput(cmd.Output())
3133
}

0 commit comments

Comments
 (0)