45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// ConfiguredURL is a client-only default. Explicit --url and PIPELINES_URL
|
|
// always take precedence; no server/provider configuration is changed.
|
|
func ConfiguredURL() (string, error) {
|
|
directory := os.Getenv("XDG_CONFIG_HOME")
|
|
if directory == "" {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
directory = filepath.Join(home, ".config")
|
|
}
|
|
path := filepath.Join(directory, "pipelines", "client.json")
|
|
data, err := os.ReadFile(path)
|
|
if os.IsNotExist(err) {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", fmt.Errorf("read client configuration %s: %w", path, err)
|
|
}
|
|
var config struct {
|
|
ServerURL string `json:"server_url"`
|
|
}
|
|
if len(data) > 64<<10 || json.Unmarshal(data, &config) != nil {
|
|
return "", fmt.Errorf("invalid client configuration %s; use --url to override", path)
|
|
}
|
|
if config.ServerURL == "" {
|
|
return "", nil
|
|
}
|
|
u, err := url.Parse(config.ServerURL)
|
|
if err != nil || (u.Scheme != "ws" && u.Scheme != "wss") || u.Hostname() == "" || u.User != nil || u.Fragment != "" {
|
|
return "", fmt.Errorf("invalid server_url in %s; use --url to override", path)
|
|
}
|
|
return config.ServerURL, nil
|
|
}
|