Skip to content

Add a --version flag #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tasks:
build:
desc: Build the project
cmds:
- go build -v
- go build -v {{.LDFLAGS}}

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

GOLINTFLAGS: "-min_confidence 0.8 -set_exit_status"
Expand Down
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Root() *cobra.Command {
rootCommand.PersistentFlags().String("project-type", "all", "Only check projects of the specified type and their subprojects. Can be {sketch|library|all}.")
rootCommand.PersistentFlags().Bool("recursive", true, "Search path recursively for Arduino projects to check. Can be {true|false}.")
rootCommand.PersistentFlags().String("report-file", "", "Save a report on the checks to this file.")
rootCommand.PersistentFlags().Bool("version", false, "Print version.")

return rootCommand
}
19 changes: 19 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package command

import (
"encoding/json"
"fmt"
"os"

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

if configuration.VersionMode() {
if configuration.OutputFormat() == outputformat.Text {
fmt.Println(configuration.Version())
} else {
versionObject := struct {
Version string `json:"version"`
}{
Version: configuration.Version(),
}
versionJSON, err := json.MarshalIndent(versionObject, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(versionJSON))
}
return
}

result.Results.Initialize()

projects, err := project.FindProjects()
Expand Down
19 changes: 19 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
reportFilePathString, _ := flags.GetString("report-file")
reportFilePath = paths.New(reportFilePathString)

versionMode, _ = flags.GetBool("version")

if len(projectPaths) == 0 {
// Default to using current working directory.
workingDirectoryPath, err := os.Getwd()
Expand Down Expand Up @@ -174,6 +176,23 @@ func ReportFilePath() *paths.Path {
return reportFilePath
}

var versionMode bool

func VersionMode() bool {
return versionMode
}

var version string
var commit string

func Version() string {
if version == "" {
return "0.0.0+" + commit
}

return version
}

var targetPaths paths.PathList

// TargetPaths returns the projects search paths.
Expand Down
19 changes: 19 additions & 0 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ func TestInitializeReportFile(t *testing.T) {
assert.Equal(t, reportFilePath, ReportFilePath())
}

func TestInitializeVersion(t *testing.T) {
flags := test.ConfigurationFlags()

flags.Set("version", "true")
assert.Nil(t, Initialize(flags, projectPaths))
assert.True(t, VersionMode())

flags.Set("version", "false")
assert.Nil(t, Initialize(flags, projectPaths))
assert.False(t, VersionMode())
}

func TestInitializeProjectPath(t *testing.T) {
targetPaths = nil
assert.Nil(t, Initialize(test.ConfigurationFlags(), []string{}))
Expand Down Expand Up @@ -201,3 +213,10 @@ func TestInitializeOfficial(t *testing.T) {
os.Setenv("ARDUINO_CHECK_OFFICIAL", "invalid value")
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths))
}

func TestVersion(t *testing.T) {
commit = "abcd"
assert.Equal(t, "0.0.0+"+commit, Version())
version = "42.1.2"
assert.Equal(t, version, Version())
}
1 change: 1 addition & 0 deletions util/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ConfigurationFlags() *pflag.FlagSet {
flags.String("project-type", "all", "")
flags.Bool("recursive", true, "")
flags.String("report-file", "", "")
flags.Bool("version", false, "")

return flags
}