From 3b95b9b1ac56bdeb9b221481591d3420785a3d5d Mon Sep 17 00:00:00 2001 From: Artis Avotins Date: Wed, 30 Oct 2024 22:46:36 +0200 Subject: [PATCH] Version flag implementation --- main.go | 13 +++++++ version/version.go | 24 ++++++++++++- version/version_test.go | 78 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 version/version_test.go 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..2477040 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,25 @@ 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) } +// ShortString returns a string with the application name, version, commit and 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) + } + }) + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy