Skip to content

Commit

Permalink
Merge pull request #3 from norwd/main
Browse files Browse the repository at this point in the history
Heuristically guess username from gh auth status
  • Loading branch information
rsese committed Dec 29, 2022
2 parents c8aab85 + f011980 commit 5f5eb03
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,42 @@ func getWorkflows(repoData repositoryData, last time.Duration) ([]*workflow, err
}

func parseArgs() (*options, error) {
var selector string

repositories := flag.StringSliceP("repos", "r", []string{}, "One or more repository names from the provided org or user")
last := flag.StringP("last", "l", "30d", "What period of time to cover in hours (eg 1h) or days (eg 30d). Default: 30d")

flag.Parse()

if len(flag.Args()) != 1 {
return nil, errors.New("need exactly one argument, either an organization or user name")
// Try to determine user or org name form single argument
if len(flag.Args()) == 1 {
// Single argument to use as org/user name
selector = flag.Arg(0)
} else if len(flag.Args()) != 0 {
// Too many arguments, don't try to infer anything, just fail
return nil, errors.New("need exactly one argument, either an organization or user name.")
} else if _, stderr, err := gh("auth", "status"); err != nil {
// Couldn't infer username, gh auth returned error
return nil, fmt.Errorf("need exactly one argument, either an organization or user name. Could not determine username from auth status: %w", err)
} else if status := stderr.String(); status != "" {
// Successfully got auth status, look through it for something that
// looks like a username.

search := "Logged in to github.com as "
for _, line := range strings.Split(status, "\n") {
if start := strings.Index(line, search); start >= 0 {
tokens := strings.Split(line[start+len(search):], " ")

// Stop looking if username was found
if len(tokens) > 0 {
selector = tokens[0]
break
}
}
}
} else {
// Couldn't infer username
return nil, errors.New("need exactly one argument, either an organization or user name.")
}

lastVal := *last
Expand Down Expand Up @@ -434,7 +463,7 @@ func parseArgs() (*options, error) {
return &options{
Repositories: *repositories,
Last: duration,
Selector: flag.Arg(0),
Selector: selector,
}, nil
}

Expand Down

0 comments on commit 5f5eb03

Please sign in to comment.