-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceec51d
commit fcaf024
Showing
6 changed files
with
273 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package config | ||
|
||
import "github.com/AkashRajpurohit/git-sync/pkg/logger" | ||
|
||
func SetSensibleDefaults(cfg *Config) { | ||
if cfg.Platform != "" && (cfg.Server.Domain == "" || cfg.Server.Protocol == "") { | ||
if cfg.Platform == "github" { | ||
cfg.Server.Domain = "github.com" | ||
cfg.Server.Protocol = "https" | ||
} | ||
|
||
if cfg.Platform == "gitlab" { | ||
cfg.Server.Domain = "gitlab.com" | ||
cfg.Server.Protocol = "https" | ||
} | ||
|
||
if cfg.Platform == "bitbucket" { | ||
cfg.Server.Domain = "bitbucket.org" | ||
cfg.Server.Protocol = "https" | ||
} | ||
|
||
if cfg.Platform == "forgejo" { | ||
cfg.Server.Domain = "v9.next.forgejo.org" | ||
cfg.Server.Protocol = "https" | ||
} | ||
} | ||
|
||
// TODO: Remove these before v1.0.0 release | ||
// If concurrency is not set, set it to 5 | ||
if cfg.Concurrency == 0 { | ||
logger.Warn("Concurrency is required but not set. Add the 'concurrency' field to the config file as mentioned in the docs: https://github.com/AkashRajpurohit/git-sync/wiki/Configuration. Setting it to 5.") | ||
cfg.Concurrency = 5 | ||
} | ||
|
||
// If no clone_type is not set in the config file, set it to bare | ||
if cfg.CloneType == "" { | ||
logger.Warn("Clone type is required but not set. Add the 'clone_type' field to the config file as mentioned in the docs: https://github.com/AkashRajpurohit/git-sync/wiki/Configuration. Setting it to 'bare'.") | ||
cfg.CloneType = "bare" | ||
} | ||
|
||
// If both are set, merge them with single token being first | ||
if cfg.Token != "" && len(cfg.Tokens) > 0 { | ||
logger.Warn("Both 'token' and 'tokens' fields are set. 'token' field is deprecated and will be merged with 'tokens'.") | ||
cfg.Tokens = append([]string{cfg.Token}, cfg.Tokens...) | ||
} | ||
|
||
// If single token is set and tokens array is empty, convert single token to array | ||
if cfg.Token != "" && len(cfg.Tokens) == 0 { | ||
logger.Warn("Using 'token' field is deprecated. Please use 'tokens' array instead.") | ||
cfg.Tokens = []string{cfg.Token} | ||
} | ||
|
||
// Clear the deprecated token field | ||
cfg.Token = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/AkashRajpurohit/git-sync/pkg/logger" | ||
) | ||
|
||
func TestSetSensibleDefaults(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg Config | ||
expected Config | ||
}{ | ||
{ | ||
name: "GitHub platform defaults", | ||
cfg: Config{ | ||
Platform: "github", | ||
}, | ||
expected: Config{ | ||
Platform: "github", | ||
Server: Server{ | ||
Domain: "github.com", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "GitLab platform defaults", | ||
cfg: Config{ | ||
Platform: "gitlab", | ||
}, | ||
expected: Config{ | ||
Platform: "gitlab", | ||
Server: Server{ | ||
Domain: "gitlab.com", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Bitbucket platform defaults", | ||
cfg: Config{ | ||
Platform: "bitbucket", | ||
}, | ||
expected: Config{ | ||
Platform: "bitbucket", | ||
Server: Server{ | ||
Domain: "bitbucket.org", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Forgejo platform defaults", | ||
cfg: Config{ | ||
Platform: "forgejo", | ||
}, | ||
expected: Config{ | ||
Platform: "forgejo", | ||
Server: Server{ | ||
Domain: "v9.next.forgejo.org", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Default concurrency when not set", | ||
cfg: Config{ | ||
Platform: "github", | ||
Server: Server{ | ||
Domain: "github.com", | ||
Protocol: "https", | ||
}, | ||
}, | ||
expected: Config{ | ||
Platform: "github", | ||
Server: Server{ | ||
Domain: "github.com", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Default clone type when not set", | ||
cfg: Config{ | ||
Platform: "github", | ||
Server: Server{ | ||
Domain: "github.com", | ||
Protocol: "https", | ||
}, | ||
Concurrency: 5, | ||
}, | ||
expected: Config{ | ||
Platform: "github", | ||
Server: Server{ | ||
Domain: "github.com", | ||
Protocol: "https", | ||
}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Merge single token with tokens array", | ||
cfg: Config{ | ||
Token: "single-token", | ||
Tokens: []string{"token1", "token2"}, | ||
}, | ||
expected: Config{ | ||
Tokens: []string{"single-token", "token1", "token2"}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Convert single token to tokens array", | ||
cfg: Config{ | ||
Token: "single-token", | ||
}, | ||
expected: Config{ | ||
Tokens: []string{"single-token"}, | ||
CloneType: "bare", | ||
Concurrency: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Keep existing values if already set", | ||
cfg: Config{ | ||
Platform: "github", | ||
CloneType: "mirror", | ||
Concurrency: 10, | ||
Server: Server{ | ||
Domain: "custom.github.com", | ||
Protocol: "ssh", | ||
}, | ||
}, | ||
expected: Config{ | ||
Platform: "github", | ||
CloneType: "mirror", | ||
Concurrency: 10, | ||
Server: Server{ | ||
Domain: "custom.github.com", | ||
Protocol: "ssh", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
logger.InitLogger("fatal") | ||
cfg := tt.cfg | ||
SetSensibleDefaults(&cfg) | ||
|
||
if cfg.Server.Domain != tt.expected.Server.Domain { | ||
t.Errorf("Server Domain = %v, want %v", cfg.Server.Domain, tt.expected.Server.Domain) | ||
} | ||
if cfg.Server.Protocol != tt.expected.Server.Protocol { | ||
t.Errorf("Server Protocol = %v, want %v", cfg.Server.Protocol, tt.expected.Server.Protocol) | ||
} | ||
if cfg.CloneType != tt.expected.CloneType { | ||
t.Errorf("CloneType = %v, want %v", cfg.CloneType, tt.expected.CloneType) | ||
} | ||
if cfg.Concurrency != tt.expected.Concurrency { | ||
t.Errorf("Concurrency = %v, want %v", cfg.Concurrency, tt.expected.Concurrency) | ||
} | ||
if cfg.Token != "" { | ||
t.Error("Token should be cleared after conversion") | ||
} | ||
if len(cfg.Tokens) != len(tt.expected.Tokens) { | ||
t.Errorf("Tokens length = %v, want %v", len(cfg.Tokens), len(tt.expected.Tokens)) | ||
} | ||
for i, token := range tt.expected.Tokens { | ||
if i < len(cfg.Tokens) && cfg.Tokens[i] != token { | ||
t.Errorf("Token at index %d = %v, want %v", i, cfg.Tokens[i], token) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.