mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-27 21:49:15 +02:00
Version command for piper go (#922)
This commit is contained in:
parent
3a128001f2
commit
5b2d3a1663
@ -8,6 +8,8 @@ RUN go test ./... -cover
|
||||
## ONLY tests so far, building to be added later
|
||||
# execute build
|
||||
# RUN go build -o piper
|
||||
RUN export GIT_COMMIT=$(git rev-parse HEAD) && \
|
||||
go build -ldflags "-X github.com/SAP/jenkins-library/cmd.GitCommit=${GIT_COMMIT}" -o piper
|
||||
|
||||
# FROM gcr.io/distroless/base:latest
|
||||
# COPY --from=build-env /build/piper /piper
|
||||
|
@ -39,6 +39,7 @@ var generalConfig generalConfigOptions
|
||||
func Execute() {
|
||||
|
||||
rootCmd.AddCommand(ConfigCommand())
|
||||
rootCmd.AddCommand(VersionCommand())
|
||||
rootCmd.AddCommand(KarmaExecuteTestsCommand())
|
||||
|
||||
addRootFlags(rootCmd)
|
||||
|
28
cmd/version.go
Normal file
28
cmd/version.go
Normal file
@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GitCommit ...
|
||||
var GitCommit string
|
||||
|
||||
// GitTag ...
|
||||
var GitTag string
|
||||
|
||||
func version(myVersionOptions versionOptions) error {
|
||||
|
||||
gitCommit, gitTag := "<n/a>", "<n/a>"
|
||||
|
||||
if len(GitCommit) > 0 {
|
||||
gitCommit = GitCommit
|
||||
}
|
||||
|
||||
if len(GitTag) > 0 {
|
||||
gitTag = GitTag
|
||||
}
|
||||
|
||||
_, err := fmt.Printf("piper-version:\n commit: \"%s\"\n tag: \"%s\"\n", gitCommit, gitTag)
|
||||
|
||||
return err
|
||||
}
|
49
cmd/version_generated.go
Normal file
49
cmd/version_generated.go
Normal file
@ -0,0 +1,49 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
//"os"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type versionOptions struct {
|
||||
}
|
||||
|
||||
var myVersionOptions versionOptions
|
||||
var versionStepConfigJSON string
|
||||
|
||||
// VersionCommand Returns the version of the piper binary
|
||||
func VersionCommand() *cobra.Command {
|
||||
metadata := versionMetadata()
|
||||
var createVersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Returns the version of the piper binary",
|
||||
Long: `Writes the commit hash and the tag (if any) to stdout and exits with 0.`,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
return PrepareConfig(cmd, &metadata, "version", &myVersionOptions, openPiperFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return version(myVersionOptions)
|
||||
},
|
||||
}
|
||||
|
||||
addVersionFlags(createVersionCmd)
|
||||
return createVersionCmd
|
||||
}
|
||||
|
||||
func addVersionFlags(cmd *cobra.Command) {
|
||||
|
||||
}
|
||||
|
||||
// retrieve step metadata
|
||||
func versionMetadata() config.StepData {
|
||||
var theMetaData = config.StepData{
|
||||
Spec: config.StepSpec{
|
||||
Inputs: config.StepInputs{
|
||||
Parameters: []config.StepParameters{},
|
||||
},
|
||||
},
|
||||
}
|
||||
return theMetaData
|
||||
}
|
16
cmd/version_generated_test.go
Normal file
16
cmd/version_generated_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestVersionCommand(t *testing.T) {
|
||||
|
||||
testCmd := VersionCommand()
|
||||
|
||||
// only high level testing performed - details are tested in step generation procudure
|
||||
assert.Equal(t, "version", testCmd.Use, "command name incorrect")
|
||||
|
||||
}
|
61
cmd/version_test.go
Normal file
61
cmd/version_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"os"
|
||||
"bytes"
|
||||
"io"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
|
||||
t.Run("versionAndTagInitialValues", func(t *testing.T) {
|
||||
|
||||
result := runVersionCommand(t, "", "")
|
||||
assert.Contains(t, result, "commit: \"<n/a>\"")
|
||||
assert.Contains(t, result, "tag: \"<n/a>\"")
|
||||
})
|
||||
|
||||
|
||||
t.Run("versionAndTagSet", func(t *testing.T) {
|
||||
|
||||
result := runVersionCommand(t, "16bafe", "v1.2.3")
|
||||
assert.Contains(t, result, "commit: \"16bafe\"")
|
||||
assert.Contains(t, result, "tag: \"v1.2.3\"")
|
||||
})
|
||||
}
|
||||
|
||||
func runVersionCommand(t *testing.T, commitID, tag string) string {
|
||||
|
||||
orig := os.Stdout
|
||||
defer func() {os.Stdout = orig}()
|
||||
|
||||
r,w,e := os.Pipe()
|
||||
if e != nil {
|
||||
t.Error("Cannot setup pipes.")
|
||||
}
|
||||
|
||||
os.Stdout = w
|
||||
|
||||
//
|
||||
// needs to be set in the free wild by the build process:
|
||||
// go build -ldflags "-X github.com/SAP/jenkins-library/cmd.GitCommit=${GIT_COMMIT} -X github.com/SAP/jenkins-library/cmd.GitTag=${GIT_TAG}"
|
||||
if len(commitID) > 0 { GitCommit = commitID; }
|
||||
if len(tag) > 0 { GitTag = tag }
|
||||
defer func() { GitCommit = ""; GitTag = "" }()
|
||||
//
|
||||
//
|
||||
|
||||
var myVersionOptions versionOptions
|
||||
e = version(myVersionOptions)
|
||||
if e != nil {
|
||||
t.Error("Version command failed.")
|
||||
}
|
||||
|
||||
w.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
return buf.String()
|
||||
}
|
5
resources/metadata/version.yaml
Normal file
5
resources/metadata/version.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
metadata:
|
||||
name: version
|
||||
description: Returns the version of the piper binary
|
||||
longDescription: |
|
||||
Writes the commit hash and the tag (if any) to stdout and exits with 0.
|
Loading…
x
Reference in New Issue
Block a user