Skip to content

Commit c3a0e49

Browse files
committed
Version flag implementation
1 parent 002b3ab commit c3a0e49

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

Diff for: 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

Diff for: version/version.go

+22-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,24 @@ 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+
func (i *Info) ShortString() string {
53+
base := fmt.Sprintf("%s %s", i.Application, i.VersionString)
54+
if i.Commit == "" && i.Date == "" {
55+
return base
56+
}
57+
58+
details := []string{}
59+
if i.Commit != "" {
60+
details = append(details, i.Commit)
61+
}
62+
if i.Date != "" {
63+
shortDate := strings.Split(i.Date, "T")[0]
64+
details = append(details, shortDate)
65+
}
66+
67+
return fmt.Sprintf("%s (%s)", base, strings.Join(details, " "))
68+
}
69+
4970
//nolint:gochecknoinits
5071
func init() {
5172
if versionString == "" {

Diff for: 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)