package gitw import ( "errors" "fmt" "os/exec" "regexp" "strings" "gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea" ) func Discover() (gitea.Repository, error) { var repository gitea.Repository cmd := exec.Command("git", "remote", "get-url", "origin") out, err := cmd.Output() if err != nil { return repository, err } re := regexp.MustCompile("^git@.*:(.*).git") matches := re.FindAllStringSubmatch(string(out), -1) var match string if len(matches) > 0 && len(matches[0]) == 2 { match = matches[0][1] // get the first match which is the Full_name of the repository if len(match) <= 0 { return repository, errors.New("No matching group found. Are you in a git repository?") } } else { return repository, errors.New("No matching group found. Are you in a git repository?") } repo := strings.Split(match, "/") if len(repo) != 2 { msg := fmt.Sprintf("Expected remote url to contain {owner}/{repo}, but found: %#v", repo) return repository, errors.New(msg) } return gitea.NewRepository(repo[1], repo[0]), nil }