Skip to content

Commit 3b95b9b

Browse files
committed
Version flag implementation
1 parent 002b3ab commit 3b95b9b

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

main.go

+13
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@ import (
1616

1717
"github.com/arduino/arduino-language-server/ls"
1818
"github.com/arduino/arduino-language-server/streams"
19+
"github.com/arduino/arduino-language-server/version"
1920
"github.com/arduino/go-paths-helper"
2021
"github.com/mattn/go-isatty"
2122
)
2223

2324
func main() {
25+
showVersion := flag.Bool("version", false, "Show version information")
26+
flag.BoolVar(showVersion, "v", false, "Show version information")
27+
28+
// Parse flags early to handle version flag
29+
flag.Parse()
30+
31+
if *showVersion {
32+
info := version.NewInfo("arduino-language-server")
33+
fmt.Println(info.ShortString())
34+
os.Exit(0)
35+
}
36+
2437
if len(os.Args) > 1 && os.Args[1] == "remove-temp-files" {
2538
for _, tmpFile := range os.Args[2:] {
2639
// SAFETY CHECK

version/version.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
package version
1717

18-
import "fmt"
18+
import (
19+
"fmt"
20+
"strings"
21+
)
1922

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

52+
// ShortString returns a string with the application name, version, commit and date
53+
func (i *Info) ShortString() string {
54+
base := fmt.Sprintf("%s %s", i.Application, i.VersionString)
55+
if i.Commit == "" && i.Date == "" {
56+
return base
57+
}
58+
59+
details := []string{}
60+
if i.Commit != "" {
61+
details = append(details, i.Commit)
62+
}
63+
if i.Date != "" {
64+
shortDate := strings.Split(i.Date, "T")[0]
65+
details = append(details, shortDate)
66+
}
67+
68+
return fmt.Sprintf("%s (%s)", base, strings.Join(details, " "))
69+
}
70+
4971
//nolint:gochecknoinits
5072
func init() {
5173
if versionString == "" {

version/version_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package version
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNewInfo(t *testing.T) {
8+
info := NewInfo("TestApp")
9+
if info.Application != "TestApp" {
10+
t.Errorf("Expected application name 'TestApp', got '%s'", info.Application)
11+
}
12+
}
13+
14+
func TestInfoString(t *testing.T) {
15+
info := &Info{
16+
Application: "TestApp",
17+
VersionString: "1.0.0",
18+
Commit: "abc123",
19+
Date: "2023-01-01",
20+
}
21+
expected := "TestApp Version: 1.0.0 Commit: abc123 Date: 2023-01-01"
22+
if got := info.String(); got != expected {
23+
t.Errorf("Expected '%s', got '%s'", expected, got)
24+
}
25+
}
26+
27+
func TestInfoShortString(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
info Info
31+
expected string
32+
}{
33+
{
34+
name: "full info",
35+
info: Info{
36+
Application: "TestApp",
37+
VersionString: "1.0.0",
38+
Commit: "abc123",
39+
Date: "2023-01-01T12:00:00Z",
40+
},
41+
expected: "TestApp 1.0.0 (abc123 2023-01-01)",
42+
},
43+
{
44+
name: "no commit",
45+
info: Info{
46+
Application: "TestApp",
47+
VersionString: "1.0.0",
48+
Date: "2023-01-01T12:00:00Z",
49+
},
50+
expected: "TestApp 1.0.0 (2023-01-01)",
51+
},
52+
{
53+
name: "no date",
54+
info: Info{
55+
Application: "TestApp",
56+
VersionString: "1.0.0",
57+
Commit: "abc123",
58+
},
59+
expected: "TestApp 1.0.0 (abc123)",
60+
},
61+
{
62+
name: "version only",
63+
info: Info{
64+
Application: "TestApp",
65+
VersionString: "1.0.0",
66+
},
67+
expected: "TestApp 1.0.0",
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
if got := tt.info.ShortString(); got != tt.expected {
74+
t.Errorf("Expected '%s', got '%s'", tt.expected, got)
75+
}
76+
})
77+
}
78+
}

0 commit comments

Comments
 (0)