Skip to content
29 changes: 16 additions & 13 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"crypto/tls"
"fmt"
"github.com/pelican-dev/wings/utils"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -53,13 +55,15 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
}

if _, err := os.Stat(configureArgs.ConfigPath); err == nil && !configureArgs.Override {
survey.AskOne(&survey.Confirm{Message: "Override existing configuration file"}, &configureArgs.Override)
err := survey.AskOne(&survey.Confirm{Message: "Override existing configuration file"}, &configureArgs.Override)
if err != nil && err != terminal.InterruptErr {
log.Fatal(err)
}
if !configureArgs.Override {
fmt.Println("Aborting process; a configuration file already exists for this node.")
os.Exit(1)
log.Fatal("Aborting process; a configuration file already exists for this node.")
}
} else if err != nil && !os.IsNotExist(err) {
panic(err)
log.Fatal(err)
}

var questions []*survey.Question
Expand Down Expand Up @@ -111,8 +115,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
if err == terminal.InterruptErr {
return
}

panic(err)
log.Fatal(err)
}

c := &http.Client{
Expand All @@ -121,7 +124,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {

req, err := getRequest()
if err != nil {
panic(err)
log.Fatal(err)
}

fmt.Printf("%+v", req.Header)
Expand All @@ -132,7 +135,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
fmt.Println("Failed to fetch configuration from the panel.\n", err.Error())
os.Exit(1)
}
defer res.Body.Close()
defer utils.CloseResponseBodyWithErrorHandling(res.Body)

if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized {
fmt.Println("The authentication credentials provided were not valid.")
Expand All @@ -148,18 +151,18 @@ func configureCmdRun(cmd *cobra.Command, args []string) {

cfg, err := config.NewAtPath(configPath)
if err != nil {
panic(err)
log.Fatal(err)
}

if err := json.Unmarshal(b, cfg); err != nil {
panic(err)
log.Fatal(err)
}

// Manually specify the Panel URL as it won't be decoded from JSON.
cfg.PanelLocation = configureArgs.PanelURL

if err = config.WriteToDisk(cfg); err != nil {
panic(err)
log.Fatal(err)
}

fmt.Println("Successfully configured wings.")
Expand All @@ -168,7 +171,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
func getRequest() (*http.Request, error) {
u, err := url.Parse(configureArgs.PanelURL)
if err != nil {
panic(err)
log.Fatal(err)
}

u.Path = path.Join(u.Path, fmt.Sprintf("api/application/nodes/%s/configuration", configureArgs.Node))
Expand Down
Loading
Loading