Skip to content

Commit

Permalink
Fix #29: Get commit via --version flag 🏷
Browse files Browse the repository at this point in the history
If code is "dirty" (i.e. git working tree is not clean):

	gh collab-scanner --version
	Commit 5f2f41a (2022-03-16 05:36:30 +0000 UTC) (dirty)

If code is "clean" (i.e. git working tree is clean):

	gh collab-scanner --version
	Commit d5e9419cf611902420316318442f33bb44d2a37a (2022-03-22 05:33:04 +0000 UTC)
  • Loading branch information
nicokosi committed Mar 23, 2022
1 parent 6c86a8c commit 339065b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ gh-collab-scanner --help

## Develop 🧑‍💻

Requires Go 1.18 or later.

### Build from source code 🧑‍💻▶️

Build then run:
Expand Down
42 changes: 40 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"flag"
"fmt"
"os"
"runtime/debug"
"strconv"
"strings"
"time"

"github.com/cli/go-gh"
)
Expand All @@ -16,6 +18,7 @@ type config struct {
user string
page int
verbose bool
version bool
}

func parseFlags() config {
Expand All @@ -24,8 +27,9 @@ func parseFlags() config {
user := flag.String("user", "", "a optional GitHub user (i.e. 'torvalds') to scan the repositories from (100 max); use repo for current folder if omitted and no 'repo' nor 'org' flag")
page := flag.Int("page", 1, "Page number for 'repo' and 'user' flags, 100 repositories per page")
verbose := flag.Bool("verbose", false, "mode that outputs several lines (otherwise, outputs a one-liner) ; default: false")
version := flag.Bool("version", false, "Output version-related information")
flag.Parse()
return config{*repo, *org, *user, *page, *verbose}
return config{*repo, *org, *user, *page, *verbose, *version}
}

type owner struct{ Login string }
Expand All @@ -43,9 +47,22 @@ type collaborator struct {
login string
}

type version struct {
commit string
date time.Time
dirty bool
}

func main() {
config := parseFlags()
if len(config.org) > 0 || len(config.user) > 0 {
if config.version {
version := getVersion()
dirty := ""
if version.dirty {
dirty = "(dirty)"
}
fmt.Printf("Commit %s (%s) %s\n", version.commit, version.date, dirty)
} else if len(config.org) > 0 || len(config.user) > 0 {
repos := []repo{}
repos, error := getRepos(config)
if error != nil {
Expand Down Expand Up @@ -246,3 +263,24 @@ func scanCommunityScore(config config, repoWithOrg string) string {
}
return message
}

func getVersion() version {
info, ok := debug.ReadBuildInfo()
if !ok {
panic("Cannot read build info")
}
revision := "?"
dirtyBuild := false
date := time.Now()
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
revision = kv.Value
case "vcs.time":
date, _ = time.Parse(time.RFC3339, kv.Value)
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}
return version{revision, date, dirtyBuild}
}
7 changes: 7 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,10 @@ func TestScanCommunityScore_Verbose(t *testing.T) {

assert.Equal(t, " - a community profile score of 42 💯", message)
}

func TestVersion(t *testing.T) {
version := getVersion()

assert.NotEmpty(t, version.commit)
assert.NotEmpty(t, version.date)
}

0 comments on commit 339065b

Please sign in to comment.