Skip to content

Commit

Permalink
Now shows commit id + version
Browse files Browse the repository at this point in the history
  • Loading branch information
SSROCKS30 committed Oct 22, 2024
1 parent 9cd46a2 commit 676a945
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
export

start:
go run main.go
go run main.go

# Add or modify a build target
build:
go build -ldflags "-X main.version=$(shell git describe --tags) -X main.commitID=#$(shell git rev-parse --short HEAD)" -o digger cli/cmd/digger/main.go
15 changes: 15 additions & 0 deletions cli/cmd/digger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/diggerhq/digger/libs/storage"
"log"
"os"
"github.com/spf13/cobra"
)

var (
version = "dev"
commitID = "unknown"
)

func exec(actor string, projectName string, repoNamespace string, command string, prNumber int, lock core_locking.Lock, policyChecker core_policy.Checker, prService ci.PullRequestService, orgService ci.OrgService, reporter reporting.Reporter, backendApi core_backend.Api) {
Expand Down Expand Up @@ -75,6 +81,15 @@ func main() {
usage.ReportErrorAndExit("", fmt.Sprintf("Error occured during command exec: %v", err), 8)
}

fmt.Printf("Running digger %s (commit: %s)\n", version, commitID)
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version number of Digger",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Running digger %s (commit: %s)\n", version, commitID)
},
}
rootCmd.AddCommand(versionCmd)
}

func getImpactedProjectsAsString(projects []digger_config.Project, prNumber int) string {
Expand Down
30 changes: 30 additions & 0 deletions cli/cmd/digger/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"bytes"
"fmt"
"io"
"os"
)

var githubContextNewPullRequestJson = `{
Expand Down Expand Up @@ -1029,3 +1033,29 @@ func TestGitHubTestPRCommandCaseInsensitivity(t *testing.T) {
assert.Equal(t, "digger plan", jobs[0].Commands[0])
assert.NoError(t, err)
}
func TestVersionPrinting(t *testing.T) {
// Save the original os.Stdout
oldStdout := os.Stdout

// Create a pipe to capture the output
r, w, _ := os.Pipe()
os.Stdout = w

// Call the main function (this will print the version)
main()

// Close the write end of the pipe to flush it
w.Close()

// Restore the original stdout
os.Stdout = oldStdout

// Read the captured output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()

// Check if the output contains the version information
assert.Contains(t, output, "Running digger")
assert.Contains(t, output, "commit:")
}

0 comments on commit 676a945

Please sign in to comment.