Skip to content

Commit aaa9cc0

Browse files
authored
Merge pull request #71 from arduino/per1234/version-flag
Add a --version flag
2 parents 85982a5 + 670e32a commit aaa9cc0

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

Taskfile.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tasks:
44
build:
55
desc: Build the project
66
cmds:
7-
- go build -v
7+
- go build -v {{.LDFLAGS}}
88

99
test:
1010
desc: Run tests
@@ -181,6 +181,11 @@ vars:
181181
sh: echo `go list ./... | tr '\n' ' '`
182182
DEFAULT_PATHS:
183183
sh: echo '`go list -f '{{"{{"}}.Dir{{"}}"}}' ./...`'
184+
# build vars
185+
COMMIT:
186+
sh: echo "$(git log -n 1 --format=%h)"
187+
LDFLAGS: >
188+
-ldflags '-X github.com/arduino/arduino-check/configuration.commit={{.COMMIT}}'
184189
GOFLAGS: "-timeout 10m -v -coverpkg=./... -covermode=atomic"
185190

186191
GOLINTFLAGS: "-min_confidence 0.8 -set_exit_status"

cli/cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Root() *cobra.Command {
3939
rootCommand.PersistentFlags().String("project-type", "all", "Only check projects of the specified type and their subprojects. Can be {sketch|library|all}.")
4040
rootCommand.PersistentFlags().Bool("recursive", true, "Search path recursively for Arduino projects to check. Can be {true|false}.")
4141
rootCommand.PersistentFlags().String("report-file", "", "Save a report on the checks to this file.")
42+
rootCommand.PersistentFlags().Bool("version", false, "Print version.")
4243

4344
return rootCommand
4445
}

command/command.go

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package command
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"os"
2223

@@ -36,6 +37,24 @@ func ArduinoCheck(rootCommand *cobra.Command, cliArguments []string) {
3637
os.Exit(1)
3738
}
3839

40+
if configuration.VersionMode() {
41+
if configuration.OutputFormat() == outputformat.Text {
42+
fmt.Println(configuration.Version())
43+
} else {
44+
versionObject := struct {
45+
Version string `json:"version"`
46+
}{
47+
Version: configuration.Version(),
48+
}
49+
versionJSON, err := json.MarshalIndent(versionObject, "", " ")
50+
if err != nil {
51+
panic(err)
52+
}
53+
fmt.Println(string(versionJSON))
54+
}
55+
return
56+
}
57+
3958
result.Results.Initialize()
4059

4160
projects, err := project.FindProjects()

configuration/configuration.go

+19
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
8181
reportFilePathString, _ := flags.GetString("report-file")
8282
reportFilePath = paths.New(reportFilePathString)
8383

84+
versionMode, _ = flags.GetBool("version")
85+
8486
if len(projectPaths) == 0 {
8587
// Default to using current working directory.
8688
workingDirectoryPath, err := os.Getwd()
@@ -174,6 +176,23 @@ func ReportFilePath() *paths.Path {
174176
return reportFilePath
175177
}
176178

179+
var versionMode bool
180+
181+
func VersionMode() bool {
182+
return versionMode
183+
}
184+
185+
var version string
186+
var commit string
187+
188+
func Version() string {
189+
if version == "" {
190+
return "0.0.0+" + commit
191+
}
192+
193+
return version
194+
}
195+
177196
var targetPaths paths.PathList
178197

179198
// TargetPaths returns the projects search paths.

configuration/configuration_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ func TestInitializeReportFile(t *testing.T) {
171171
assert.Equal(t, reportFilePath, ReportFilePath())
172172
}
173173

174+
func TestInitializeVersion(t *testing.T) {
175+
flags := test.ConfigurationFlags()
176+
177+
flags.Set("version", "true")
178+
assert.Nil(t, Initialize(flags, projectPaths))
179+
assert.True(t, VersionMode())
180+
181+
flags.Set("version", "false")
182+
assert.Nil(t, Initialize(flags, projectPaths))
183+
assert.False(t, VersionMode())
184+
}
185+
174186
func TestInitializeProjectPath(t *testing.T) {
175187
targetPaths = nil
176188
assert.Nil(t, Initialize(test.ConfigurationFlags(), []string{}))
@@ -201,3 +213,10 @@ func TestInitializeOfficial(t *testing.T) {
201213
os.Setenv("ARDUINO_CHECK_OFFICIAL", "invalid value")
202214
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths))
203215
}
216+
217+
func TestVersion(t *testing.T) {
218+
commit = "abcd"
219+
assert.Equal(t, "0.0.0+"+commit, Version())
220+
version = "42.1.2"
221+
assert.Equal(t, version, Version())
222+
}

util/test/test.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func ConfigurationFlags() *pflag.FlagSet {
2929
flags.String("project-type", "all", "")
3030
flags.Bool("recursive", true, "")
3131
flags.String("report-file", "", "")
32+
flags.Bool("version", false, "")
3233

3334
return flags
3435
}

0 commit comments

Comments
 (0)