Expand pipeline panes and select the verified Docker client endpoint
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfiguredURLValidationAndMissingConfig(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", directory)
|
||||
if value, err := ConfiguredURL(); err != nil || value != "" {
|
||||
t.Fatal(value, err)
|
||||
}
|
||||
path := filepath.Join(directory, "pipelines", "client.json")
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
text string
|
||||
valid bool
|
||||
}{
|
||||
{`{"server_url":"ws://127.0.0.1:8081/connect","custom":"keep"}`, true},
|
||||
{`{"server_url":"wss://server.example/connect"}`, true},
|
||||
{`{"server_url":"http://wrong/connect"}`, false},
|
||||
{`{"server_url":"ws://secret:token@server/connect"}`, false},
|
||||
{`not JSON`, false},
|
||||
} {
|
||||
if err := os.WriteFile(path, []byte(test.text), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := ConfiguredURL()
|
||||
if (err == nil) != test.valid || (test.valid && value == "") {
|
||||
t.Fatal(test, value, err)
|
||||
}
|
||||
if data, _ := os.ReadFile(path); string(data) != test.text {
|
||||
t.Fatal("configuration mutated")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user