From 5f3b7f34fcd388b11f42c626336339e7cbb6c118 Mon Sep 17 00:00:00 2001 From: utgwkk Date: Thu, 24 Dec 2020 08:50:58 +0900 Subject: [PATCH] override repository information from cwd --- cli.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cli.go b/cli.go index f781e65..b0f3d82 100644 --- a/cli.go +++ b/cli.go @@ -6,7 +6,9 @@ import ( "os" "regexp" "sort" + "strings" + "github.com/go-git/go-git/v5" "github.com/google/go-github/v32/github" "github.com/jessevdk/go-flags" "golang.org/x/sync/errgroup" @@ -45,6 +47,43 @@ func (cli *CLI) logfVerbose(format string, args ...interface{}) { log.Printf(format, args...) } +func (cli *CLI) overrideRepositoryFromCWD(config *ProfileConfig) { + if config.Owner != "" && config.Repository != "" { + return + } + cwd, err := os.Getwd() + if err != nil { + return + } + repo, err := git.PlainOpen(cwd) + if err != nil { + cli.loglnVerbose(err) + return + } + remote, err := repo.Remote("origin") + if err != nil { + cli.loglnVerbose(err) + return + } + urls := remote.Config().URLs + if len(urls) == 0 { + return + } + url := urls[0] + splitted := strings.Split(url, "/") + if len(splitted) <= 1 { + return + } + owner, repoName := splitted[len(splitted)-2], splitted[len(splitted)-1] + repoName = strings.Replace(repoName, ".git", "", 1) + if lastColonPos := strings.LastIndex(owner, ":"); lastColonPos != -1 { + // git@github.com:owner/repo.git + owner = owner[lastColonPos:] + } + config.Owner = owner + config.Repository = repoName +} + func (cli *CLI) Start(ctx context.Context, args []string) { var config *ProfileConfig = DefaultProfileConfig() var configFromArgs ProfileConfigCLIArgs @@ -65,6 +104,7 @@ func (cli *CLI) Start(ctx context.Context, args []string) { } else { config = OverrideCLIArgs(DefaultProfileConfig(), &configFromArgs) } + cli.overrideRepositoryFromCWD(config) cli.SetVerbosity(config.Verbose) cli.logfVerbose("config=%v", configTomlPath)