Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect url prompt #36

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dpastoor/wbi/internal/authentication"
"github.com/dpastoor/wbi/internal/config"
"github.com/dpastoor/wbi/internal/connect"
"github.com/dpastoor/wbi/internal/jupyter"
"github.com/dpastoor/wbi/internal/languages"
"github.com/dpastoor/wbi/internal/license"
Expand Down Expand Up @@ -149,6 +150,22 @@ func newSetup(setupOpts setupOpts) error {
return fmt.Errorf("issue handling authentication: %w", AuthErr)
}

// Connect URL
connectChoice, err := connect.PromptConnectChoice()
if err != nil {
return fmt.Errorf("issue in prompt for Connect URL choice: %w", err)
}
if connectChoice {
rawConnectURL, err := connect.PromptConnectURL()
if err != nil {
return fmt.Errorf("issue entering Connect URL: %w", err)
}
WBConfig.ConnectURL, err = connect.VerifyConnectURL(rawConnectURL)
if err != nil {
return fmt.Errorf("issue with checking the Connect URL: %w", err)
}
}

// Write config to console
WBConfig.ConfigStructToText()

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type WBConfig struct {
RConfig RConfig
PythonConfig PythonConfig
AuthConfig AuthConfig
ConnectURL string
}

type AuthConfig struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/config/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func (WBConfig *WBConfig) ConfigStructToText() {
WBConfig.PythonConfig.PythonStructToText()
WBConfig.SSLConfig.SSLStructToText()
WBConfig.AuthConfig.AuthStructToText()
WBConfig.ConnectStringToText()
fmt.Println("\n=== Please restart Workbench after making these changes")
}

Expand Down Expand Up @@ -55,3 +56,9 @@ func (OIDCConfig *OIDCConfig) AuthOIDCStructToText() {
fmt.Println("client-id=" + OIDCConfig.ClientID)
fmt.Println("client-secret=" + OIDCConfig.ClientSecret)
}

// Prints the ConnectURL configuration string information to the console
func (WBConfig *WBConfig) ConnectStringToText() {
fmt.Println("\n=== Add to config file: /etc/rstudio/rsession.conf:")
fmt.Println("default-rsconnect-server=" + WBConfig.ConnectURL)
}
34 changes: 34 additions & 0 deletions internal/connect/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package connect

import (
"errors"
"fmt"

"github.com/AlecAivazis/survey/v2"
)

// Prompt users if they wish to add a default Connect URL to Workbench
func PromptConnectChoice() (bool, error) {
name := true
prompt := &survey.Confirm{
Message: "Would you like to provide a default Connect URL for Workbench?",
}
err := survey.AskOne(prompt, &name)
if err != nil {
return false, errors.New("there was an issue with the Connect URL prompt")
}
return name, nil
}

// Prompt users for a default Connect URL
func PromptConnectURL() (string, error) {
target := ""
prompt := &survey.Input{
Message: "Connect URL:",
}
err := survey.AskOne(prompt, &target)
if err != nil {
return "", fmt.Errorf("issue prompting for a Connect URL: %w", err)
}
return target, nil
}
44 changes: 44 additions & 0 deletions internal/connect/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package connect

import (
"context"
"errors"
"fmt"
"net/http"
"time"
)

func cleanConnectURL(connectURL string) string {
// remove trailing slash if present
if connectURL[len(connectURL)-1] == '/' {
connectURL = connectURL[:len(connectURL)-1]
}
return connectURL
}

// VerifyConnectURL checks if the Connect URL is valid
func VerifyConnectURL(connectURL string) (string, error) {

cleanConnectURL := cleanConnectURL(connectURL)
fullTestURL := cleanConnectURL + "/__ping__"

client := &http.Client{
Timeout: 30 * time.Second,
}
req, err := http.NewRequestWithContext(context.Background(),
http.MethodGet, fullTestURL, nil)
if err != nil {
return "", errors.New("error creating request")
}
res, err := client.Do(req)
if err != nil {
return "", errors.New("error retrieving JSON data")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", errors.New("error in HTTP status code")
}

fmt.Println("\nConnect URL has been successfull validated.\n")
return cleanConnectURL, nil
}