diff --git a/main.go b/main.go index b1b5883..535d7f7 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/version/version.go b/version/version.go index 8ae9162..4334714 100644 --- a/version/version.go +++ b/version/version.go @@ -15,7 +15,10 @@ package version -import "fmt" +import ( + "fmt" + "strings" +) var ( defaultVersionString = "0.0.0-git" @@ -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 { + 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 == "" { diff --git a/version/version_test.go b/version/version_test.go new file mode 100644 index 0000000..37fe788 --- /dev/null +++ b/version/version_test.go @@ -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) + } + }) + } +}