feat: auto detect hostname
This commit is contained in:
parent
2c27823e18
commit
c0baf4fa3b
13 changed files with 300 additions and 125 deletions
|
|
@ -87,7 +87,14 @@ func (c *Config) SaveToPath(path string) error {
|
|||
return os.WriteFile(path, data, 0600)
|
||||
}
|
||||
|
||||
func (c *Config) GetHost(hostname string) (HostConfig, error) {
|
||||
// GetHost resolves the hostname to use for API client creation.
|
||||
// Priority order:
|
||||
// 1. Explicitly provided hostname parameter
|
||||
// 2. CLI flag (--hostname)
|
||||
// 3. Environment variable (FGJ_HOST)
|
||||
// 4. Auto-detected hostname from git remote
|
||||
// 5. Default to codeberg.org
|
||||
func (c *Config) GetHost(hostname string, detectedHost string) (HostConfig, error) {
|
||||
if hostname == "" {
|
||||
hostname = viper.GetString("hostname")
|
||||
}
|
||||
|
|
@ -96,6 +103,10 @@ func (c *Config) GetHost(hostname string) (HostConfig, error) {
|
|||
hostname = os.Getenv("FGJ_HOST")
|
||||
}
|
||||
|
||||
if hostname == "" {
|
||||
hostname = detectedHost
|
||||
}
|
||||
|
||||
if hostname == "" {
|
||||
hostname = "codeberg.org"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ func TestConfig_GetHost(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
host, err := cfg.GetHost("codeberg.org")
|
||||
host, err := cfg.GetHost("codeberg.org", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ func TestConfig_GetHost(t *testing.T) {
|
|||
t.Errorf("Expected hostname 'codeberg.org', got '%s'", host.Hostname)
|
||||
}
|
||||
|
||||
_, err = cfg.GetHost("nonexistent.org")
|
||||
_, err = cfg.GetHost("nonexistent.org", "")
|
||||
if err == nil {
|
||||
t.Error("Expected error for nonexistent host")
|
||||
}
|
||||
|
|
@ -261,7 +261,7 @@ func TestConfig_GetHost_EmptyString(t *testing.T) {
|
|||
}
|
||||
|
||||
// Empty hostname should default to codeberg.org
|
||||
host, err := cfg.GetHost("")
|
||||
host, err := cfg.GetHost("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -282,7 +282,7 @@ func TestConfig_GetHost_WhitespaceString(t *testing.T) {
|
|||
}
|
||||
|
||||
// Whitespace-only hostname should default to codeberg.org
|
||||
host, err := cfg.GetHost(" ")
|
||||
host, err := cfg.GetHost(" ", "")
|
||||
if err == nil {
|
||||
t.Logf("Got host: %+v (this may be expected behavior)", host)
|
||||
} else {
|
||||
|
|
@ -301,7 +301,7 @@ func TestConfig_SetHost_EmptyToken(t *testing.T) {
|
|||
|
||||
cfg.SetHost("codeberg.org", hostConfig)
|
||||
|
||||
host, err := cfg.GetHost("codeberg.org")
|
||||
host, err := cfg.GetHost("codeberg.org", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -331,7 +331,7 @@ func TestConfig_SetHost_OverwriteExisting(t *testing.T) {
|
|||
|
||||
cfg.SetHost("codeberg.org", newConfig)
|
||||
|
||||
host, err := cfg.GetHost("codeberg.org")
|
||||
host, err := cfg.GetHost("codeberg.org", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -374,7 +374,7 @@ func TestConfig_MultipleHosts(t *testing.T) {
|
|||
|
||||
// Verify each host can be retrieved correctly
|
||||
for _, h := range hosts {
|
||||
host, err := cfg.GetHost(h.hostname)
|
||||
host, err := cfg.GetHost(h.hostname, "")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get host %s: %v", h.hostname, err)
|
||||
continue
|
||||
|
|
@ -408,12 +408,12 @@ func TestConfig_GitProtocol(t *testing.T) {
|
|||
})
|
||||
|
||||
// Verify protocols are stored correctly
|
||||
sshHost, _ := cfg.GetHost("test-ssh.org")
|
||||
sshHost, _ := cfg.GetHost("test-ssh.org", "")
|
||||
if sshHost.GitProtocol != "ssh" {
|
||||
t.Errorf("Expected git_protocol 'ssh', got '%s'", sshHost.GitProtocol)
|
||||
}
|
||||
|
||||
httpsHost, _ := cfg.GetHost("test-https.org")
|
||||
httpsHost, _ := cfg.GetHost("test-https.org", "")
|
||||
if httpsHost.GitProtocol != "https" {
|
||||
t.Errorf("Expected git_protocol 'https', got '%s'", httpsHost.GitProtocol)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue