Fix CLI URL scheme handling when using http:// addresses
The browse command and config loading were incorrectly prepending https:// to hosts that already had a scheme, resulting in malformed URLs like https://http://localhost:8080. Added normalizeHost() function to strip URL schemes when loading config, and added scheme detection in the browse command before prepending https://. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -17,9 +17,11 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -147,6 +149,16 @@ func Load() (*Config, error) {
|
||||
cfg.Hosts = make(map[string]*HostConfig)
|
||||
}
|
||||
|
||||
normalized := make(map[string]*HostConfig, len(cfg.Hosts))
|
||||
for host, hc := range cfg.Hosts {
|
||||
normalized[normalizeHost(host)] = hc
|
||||
}
|
||||
cfg.Hosts = normalized
|
||||
|
||||
if cfg.ActiveHost != "" {
|
||||
cfg.ActiveHost = normalizeHost(cfg.ActiveHost)
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
@@ -172,8 +184,19 @@ func (c *Config) Save() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeHost(host string) string {
|
||||
if strings.HasPrefix(host, "http://") || strings.HasPrefix(host, "https://") {
|
||||
if u, err := url.Parse(host); err == nil {
|
||||
return u.Host
|
||||
}
|
||||
}
|
||||
|
||||
return strings.TrimRight(host, "/")
|
||||
}
|
||||
|
||||
func (c *Config) DefaultHost() (string, *HostConfig, error) {
|
||||
if host := os.Getenv("PROBO_HOST"); host != "" {
|
||||
host = normalizeHost(host)
|
||||
hc := &HostConfig{}
|
||||
if saved, ok := c.Hosts[host]; ok {
|
||||
*hc = *saved
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.probo.inc/probo/pkg/cmd/cmdutil"
|
||||
@@ -57,11 +58,15 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
|
||||
flagOrg = hc.Organization
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
|
||||
host = "https://" + host
|
||||
}
|
||||
|
||||
var url string
|
||||
if flagOrg != "" {
|
||||
url = fmt.Sprintf("https://%s/organizations/%s", host, flagOrg)
|
||||
url = fmt.Sprintf("%s/organizations/%s", host, flagOrg)
|
||||
} else {
|
||||
url = fmt.Sprintf("https://%s", host)
|
||||
url = host
|
||||
}
|
||||
|
||||
if flagNoBrowser || f.IOStreams.ForceNonInteractive {
|
||||
|
||||
Reference in New Issue
Block a user