From 029f4e9fb417f5ef36fc785bf2f2907db7fda53b Mon Sep 17 00:00:00 2001 From: Andreas Wachter Date: Mon, 22 Jun 2026 09:45:16 +0200 Subject: [PATCH] extend ~ to home directory (#404) --- main.go | 36 ++++++++++++++++++++++++++++++--- main_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 2c828f2..32685c1 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/main_test.go b/main_test.go index da2b02f..b49d3ca 100644 --- a/main_test.go +++ b/main_test.go @@ -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()