diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d60688b..fdf5f6a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -23,5 +23,7 @@ jobs: uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} + - name: Make test + run: make test - name: Make all run: make all \ No newline at end of file diff --git a/Makefile b/Makefile index bb2d30f..a7abf8f 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ all: dist hash gofmt: @test -z $(shell gofmt -l -s $(SOURCE_DIRS) ./ | tee /dev/stderr) || (echo "[WARN] Fix formatting issues with 'make fmt'" && exit 1) +test: + $(GOCMD) test -count=1 -v ./cmd/. + build: $(GOCMD) build . diff --git a/cmd/root.go b/cmd/root.go index 89e6779..1e83ec7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -71,14 +71,14 @@ func init() { } else { ref, _ := r.Head() refHash := ref.Hash().String() + defaultGitCommit = getShortHash(refHash, 7) remotes, _ := r.Remotes() - remoteUsed := remotes[0].Config().URLs[0] - toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")] - defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:] - repoStartPos := strings.LastIndex(remoteUsed, "/") + 1 - defaultGitRepo = remoteUsed[repoStartPos : len(remoteUsed)-4] - defaultGitCommit = refHash - log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit) + if len(remotes) > 0 { + remoteUsed := remotes[0].Config().URLs[0] + log.Printf("Detected a total of %d remotes. Using the 1st remote url %s to retrieve git info", len(remotes), remoteUsed) + defaultGitOrg, defaultGitRepo = fromRemoteToOrgRepo(remoteUsed) + log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit) + } } // Here you will define your flags and configuration settings. @@ -97,6 +97,29 @@ func init() { rootCmd.MarkPersistentFlagRequired("bench") } +// Abbreviate the long hash to a short hash (7 digits) +func getShortHash(hash string, ndigits int) (short string) { + if len(hash) < ndigits { + short = hash + } else { + short = hash[:ndigits] + } + return +} + +func fromRemoteToOrgRepo(remoteUsed string) (defaultGitOrg string, defaultGitRepo string) { + toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")] + defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:] + repoStartPos := strings.LastIndex(remoteUsed, "/") + 1 + repoEndPos := strings.LastIndex(remoteUsed[repoStartPos:], ".") + if repoEndPos < 0 { + defaultGitRepo = remoteUsed[repoStartPos:] + } else { + defaultGitRepo = remoteUsed[repoStartPos : repoStartPos+repoEndPos] + } + return +} + // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..e0733cd --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,56 @@ +package cmd + +import "testing" + +func Test_fromRemoteToOrgRepo(t *testing.T) { + type args struct { + remoteUsed string + } + tests := []struct { + name string + args args + wantDefaultGitOrg string + wantDefaultGitRepo string + }{ + {"git", args{"git@github.com:codeperfio/pprof-exporter.git"}, "codeperfio", "pprof-exporter"}, + {"git-neg", args{"git@github.com:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"}, + {"git-neg2", args{"github.com:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"}, + {"codeperfio/example-go", args{"git@github.com:codeperfio/example-go.git"}, "codeperfio", "example-go"}, + {"codeperfio/example-go-https", args{"https://github.com/codeperfio/example-go.git"}, "codeperfio", "example-go"}, + {"codeperfio/example-go-https-neg", args{"https://github.com/codeperfio/example-go"}, "codeperfio", "example-go"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotDefaultGitOrg, gotDefaultGitRepo := fromRemoteToOrgRepo(tt.args.remoteUsed) + if gotDefaultGitOrg != tt.wantDefaultGitOrg { + t.Errorf("fromRemoteToOrgRepo() gotDefaultGitOrg = %v, want %v", gotDefaultGitOrg, tt.wantDefaultGitOrg) + } + if gotDefaultGitRepo != tt.wantDefaultGitRepo { + t.Errorf("fromRemoteToOrgRepo() gotDefaultGitRepo = %v, want %v", gotDefaultGitRepo, tt.wantDefaultGitRepo) + } + }) + } +} + +func Test_getShortHash(t *testing.T) { + type args struct { + hash string + ndigits int + } + tests := []struct { + name string + args args + wantShort string + }{ + {"small", args{"a", 10}, "a"}, + {"7dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 7}, "1e872b5"}, + {"4dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 4}, "1e87"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if gotShort := getShortHash(tt.args.hash, tt.args.ndigits); gotShort != tt.wantShort { + t.Errorf("getShortHash() = %v, want %v", gotShort, tt.wantShort) + } + }) + } +}