extend ~ to home directory (#404)

This commit is contained in:
Andreas Wachter
2026-06-22 09:45:16 +02:00
committed by GitHub
parent d5caefb310
commit 029f4e9fb4
2 changed files with 90 additions and 3 deletions
+33 -3
View File
@@ -92,9 +92,7 @@ func readConfigFile(configfile string) []*types.Conf {
continue
}
for i, local := range c.Destination.Local {
c.Destination.Local[i].Path = substituteHomeForTildeInPath(local.Path)
}
expandConfigPaths(&c)
if !reflect.ValueOf(c).IsZero() {
if len(conf) > 0 {
@@ -110,6 +108,38 @@ func readConfigFile(configfile string) []*types.Conf {
return conf
}
func expandConfigPaths(c *types.Conf) {
c.Log.FileLogging.Dir = substituteHomeForTildeInPath(c.Log.FileLogging.Dir)
expandGenRepoPaths(c.Source.Gogs)
expandGenRepoPaths(c.Source.Gitlab)
expandGenRepoPaths(c.Source.Github)
expandGenRepoPaths(c.Source.Gitea)
expandGenRepoPaths(c.Source.BitBucket)
expandGenRepoPaths(c.Source.OneDev)
expandGenRepoPaths(c.Source.Sourcehut)
expandGenRepoPaths(c.Source.Any)
expandGenRepoPaths(c.Destination.Gitlab)
expandGenRepoPaths(c.Destination.Github)
expandGenRepoPaths(c.Destination.Gitea)
expandGenRepoPaths(c.Destination.Gogs)
expandGenRepoPaths(c.Destination.OneDev)
expandGenRepoPaths(c.Destination.Sourcehut)
for i, local := range c.Destination.Local {
c.Destination.Local[i].Path = substituteHomeForTildeInPath(local.Path)
}
}
func expandGenRepoPaths(repos []types.GenRepo) {
for i := range repos {
repos[i].TokenFile = substituteHomeForTildeInPath(repos[i].TokenFile)
repos[i].SSHKey = substituteHomeForTildeInPath(repos[i].SSHKey)
repos[i].AppPrivateKeyFile = substituteHomeForTildeInPath(repos[i].AppPrivateKeyFile)
}
}
func getUserHome() (string, error) {
usr, err := user.Current()
if err != nil {
+57
View File
@@ -84,6 +84,63 @@ destination:
}
}
func TestReadConfigFile_ExpandsHomeInFileBackedConfigKeys(t *testing.T) {
t.Parallel()
config := `source:
github:
- token_file: "~/github-token"
sshkey: "~/github-ssh"
app_private_key_file: "~/github-app.pem"
destination:
gitea:
- token_file: "~/gitea-token"
sshkey: "~/gitea-ssh"
app_private_key_file: "~/gitea-app.pem"
log:
file-logging:
dir: "~/gickup-logs"
file: "gickup.log"
`
f, err := os.CreateTemp(t.TempDir(), "gickup-test-*.yml")
if err != nil {
t.Fatalf("create temp file: %v", err)
}
configPath := f.Name()
if _, err := f.WriteString(config); err != nil {
t.Fatalf("write config: %v", err)
}
f.Close()
confs := readConfigFile(configPath)
if len(confs) != 1 {
t.Fatalf("expected 1 config, got %d", len(confs))
}
source := confs[0].Source.Github[0]
for name, path := range map[string]string{
"source token_file": source.TokenFile,
"source sshkey": source.SSHKey,
"source app_private_key_file": source.AppPrivateKeyFile,
} {
if strings.HasPrefix(path, "~") {
t.Fatalf("%s was not expanded: %q", name, path)
}
}
destination := confs[0].Destination.Gitea[0]
for name, path := range map[string]string{
"destination token_file": destination.TokenFile,
"destination sshkey": destination.SSHKey,
"destination app_private_key_file": destination.AppPrivateKeyFile,
"log file-logging dir": confs[0].Log.FileLogging.Dir,
} {
if strings.HasPrefix(path, "~") {
t.Fatalf("%s was not expanded: %q", name, path)
}
}
}
func TestReadConfigFile_S3UseStaticCredsTrue(t *testing.T) {
t.Parallel()