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:
Bryan Frimin
2026-03-18 17:19:44 +01:00
parent d92632933b
commit 0f1fc2f069
2 changed files with 30 additions and 2 deletions

View File

@@ -17,9 +17,11 @@ package config
import ( import (
"fmt" "fmt"
"maps" "maps"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"slices" "slices"
"strings"
"time" "time"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -147,6 +149,16 @@ func Load() (*Config, error) {
cfg.Hosts = make(map[string]*HostConfig) 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 return &cfg, nil
} }
@@ -172,8 +184,19 @@ func (c *Config) Save() error {
return nil 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) { func (c *Config) DefaultHost() (string, *HostConfig, error) {
if host := os.Getenv("PROBO_HOST"); host != "" { if host := os.Getenv("PROBO_HOST"); host != "" {
host = normalizeHost(host)
hc := &HostConfig{} hc := &HostConfig{}
if saved, ok := c.Hosts[host]; ok { if saved, ok := c.Hosts[host]; ok {
*hc = *saved *hc = *saved

View File

@@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"runtime" "runtime"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.probo.inc/probo/pkg/cmd/cmdutil" "go.probo.inc/probo/pkg/cmd/cmdutil"
@@ -57,11 +58,15 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
flagOrg = hc.Organization flagOrg = hc.Organization
} }
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
host = "https://" + host
}
var url string var url string
if flagOrg != "" { if flagOrg != "" {
url = fmt.Sprintf("https://%s/organizations/%s", host, flagOrg) url = fmt.Sprintf("%s/organizations/%s", host, flagOrg)
} else { } else {
url = fmt.Sprintf("https://%s", host) url = host
} }
if flagNoBrowser || f.IOStreams.ForceNonInteractive { if flagNoBrowser || f.IOStreams.ForceNonInteractive {