Skip to content

Version flag implementation #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 23 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,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 == "" {
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)
}
})
}
}
Loading