Skip to content

Commit

Permalink
Version flag implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
artis101 committed Oct 30, 2024
1 parent 002b3ab commit c3a0e49
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@ import (

"github.com/arduino/arduino-language-server/ls"
"github.com/arduino/arduino-language-server/streams"
"github.com/arduino/arduino-language-server/version"
"github.com/arduino/go-paths-helper"
"github.com/mattn/go-isatty"
)

func main() {
showVersion := flag.Bool("version", false, "Show version information")
flag.BoolVar(showVersion, "v", false, "Show version information")

// Parse flags early to handle version flag
flag.Parse()

if *showVersion {
info := version.NewInfo("arduino-language-server")
fmt.Println(info.ShortString())
os.Exit(0)
}

if len(os.Args) > 1 && os.Args[1] == "remove-temp-files" {
for _, tmpFile := range os.Args[2:] {
// SAFETY CHECK
Expand Down
23 changes: 22 additions & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

package version

import "fmt"
import (
"fmt"
"strings"
)

var (
defaultVersionString = "0.0.0-git"
Expand Down Expand Up @@ -46,6 +49,24 @@ func (i *Info) String() string {
return fmt.Sprintf("%[1]s Version: %[2]s Commit: %[3]s Date: %[4]s", i.Application, i.VersionString, i.Commit, i.Date)
}

func (i *Info) ShortString() string {

Check failure on line 52 in version/version.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported method Info.ShortString should have comment or be unexported
base := fmt.Sprintf("%s %s", i.Application, i.VersionString)
if i.Commit == "" && i.Date == "" {
return base
}

details := []string{}
if i.Commit != "" {
details = append(details, i.Commit)
}
if i.Date != "" {
shortDate := strings.Split(i.Date, "T")[0]
details = append(details, shortDate)
}

return fmt.Sprintf("%s (%s)", base, strings.Join(details, " "))
}

//nolint:gochecknoinits
func init() {
if versionString == "" {
Expand Down
78 changes: 78 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package version

import (
"testing"
)

func TestNewInfo(t *testing.T) {
info := NewInfo("TestApp")
if info.Application != "TestApp" {
t.Errorf("Expected application name 'TestApp', got '%s'", info.Application)
}
}

func TestInfoString(t *testing.T) {
info := &Info{
Application: "TestApp",
VersionString: "1.0.0",
Commit: "abc123",
Date: "2023-01-01",
}
expected := "TestApp Version: 1.0.0 Commit: abc123 Date: 2023-01-01"
if got := info.String(); got != expected {
t.Errorf("Expected '%s', got '%s'", expected, got)
}
}

func TestInfoShortString(t *testing.T) {
tests := []struct {
name string
info Info
expected string
}{
{
name: "full info",
info: Info{
Application: "TestApp",
VersionString: "1.0.0",
Commit: "abc123",
Date: "2023-01-01T12:00:00Z",
},
expected: "TestApp 1.0.0 (abc123 2023-01-01)",
},
{
name: "no commit",
info: Info{
Application: "TestApp",
VersionString: "1.0.0",
Date: "2023-01-01T12:00:00Z",
},
expected: "TestApp 1.0.0 (2023-01-01)",
},
{
name: "no date",
info: Info{
Application: "TestApp",
VersionString: "1.0.0",
Commit: "abc123",
},
expected: "TestApp 1.0.0 (abc123)",
},
{
name: "version only",
info: Info{
Application: "TestApp",
VersionString: "1.0.0",
},
expected: "TestApp 1.0.0",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.info.ShortString(); got != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, got)
}
})
}
}

0 comments on commit c3a0e49

Please sign in to comment.