Skip to content

Commit 8c6b423

Browse files
committed
add --version flag, incl platform info
1 parent 7ae3c6c commit 8c6b423

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"runtime"
8+
49
"github.com/nilock/tuido/tui"
10+
"github.com/nilock/tuido/utils"
511
)
612

713
func main() {
14+
var showVersion = flag.Bool("version", false, "show version and platform information")
15+
flag.Parse()
16+
17+
if *showVersion {
18+
showVersionInfo()
19+
os.Exit(0)
20+
}
21+
822
tui.Run()
923
}
24+
25+
func showVersionInfo() {
26+
version := utils.Version()
27+
goos := runtime.GOOS
28+
goarch := runtime.GOARCH
29+
30+
fmt.Printf("tuido %s\n", version)
31+
fmt.Printf("Platform: %s/%s\n", goos, goarch)
32+
33+
// Show expected asset name for this platform
34+
assetName := utils.BuildAssetName(version, goos, goarch)
35+
fmt.Printf("Asset: %s\n", assetName)
36+
37+
// Show if this matches a known release asset (with graceful failure)
38+
if asset, err := utils.GetCurrentPlatformAsset(); err == nil {
39+
fmt.Printf("Available: %s (%d bytes)\n", asset.Name, asset.Size)
40+
} else {
41+
fmt.Printf("Available: (unable to check - %v)\n", err)
42+
}
43+
}

main_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"runtime"
7+
"strings"
8+
"testing"
9+
10+
"github.com/nilock/tuido/utils"
11+
)
12+
13+
func TestShowVersionInfo(t *testing.T) {
14+
// Capture stdout
15+
oldStdout := os.Stdout
16+
r, w, _ := os.Pipe()
17+
os.Stdout = w
18+
19+
showVersionInfo()
20+
21+
w.Close()
22+
os.Stdout = oldStdout
23+
24+
var buf bytes.Buffer
25+
buf.ReadFrom(r)
26+
output := buf.String()
27+
28+
// Test that output contains expected components
29+
expectedVersion := utils.Version()
30+
expectedPlatform := runtime.GOOS + "/" + runtime.GOARCH
31+
expectedAsset := utils.BuildAssetName(expectedVersion, runtime.GOOS, runtime.GOARCH)
32+
33+
if !strings.Contains(output, expectedVersion) {
34+
t.Errorf("Output should contain version %s, got: %s", expectedVersion, output)
35+
}
36+
37+
if !strings.Contains(output, expectedPlatform) {
38+
t.Errorf("Output should contain platform %s, got: %s", expectedPlatform, output)
39+
}
40+
41+
if !strings.Contains(output, expectedAsset) {
42+
t.Errorf("Output should contain asset name %s, got: %s", expectedAsset, output)
43+
}
44+
45+
// Test that output has the expected structure
46+
lines := strings.Split(strings.TrimSpace(output), "\n")
47+
if len(lines) < 3 {
48+
t.Errorf("Expected at least 3 lines of output, got %d: %s", len(lines), output)
49+
}
50+
51+
// Test first line format
52+
if !strings.HasPrefix(lines[0], "tuido ") {
53+
t.Errorf("First line should start with 'tuido ', got: %s", lines[0])
54+
}
55+
56+
// Test second line format
57+
if !strings.HasPrefix(lines[1], "Platform: ") {
58+
t.Errorf("Second line should start with 'Platform: ', got: %s", lines[1])
59+
}
60+
61+
// Test third line format
62+
if !strings.HasPrefix(lines[2], "Asset: ") {
63+
t.Errorf("Third line should start with 'Asset: ', got: %s", lines[2])
64+
}
65+
66+
// Test fourth line format (Available or error message)
67+
if len(lines) >= 4 {
68+
if !strings.HasPrefix(lines[3], "Available: ") {
69+
t.Errorf("Fourth line should start with 'Available: ', got: %s", lines[3])
70+
}
71+
}
72+
}
73+
74+
func TestVersionInfoContainsExpectedAssetName(t *testing.T) {
75+
version := utils.Version()
76+
goos := runtime.GOOS
77+
goarch := runtime.GOARCH
78+
79+
expectedAsset := utils.BuildAssetName(version, goos, goarch)
80+
81+
// Capture stdout
82+
oldStdout := os.Stdout
83+
r, w, _ := os.Pipe()
84+
os.Stdout = w
85+
86+
showVersionInfo()
87+
88+
w.Close()
89+
os.Stdout = oldStdout
90+
91+
var buf bytes.Buffer
92+
buf.ReadFrom(r)
93+
output := buf.String()
94+
95+
if !strings.Contains(output, expectedAsset) {
96+
t.Errorf("Version info should contain asset name %s, but output was: %s", expectedAsset, output)
97+
}
98+
}
99+
100+
func TestVersionInfoResilience(t *testing.T) {
101+
// This test ensures that even if network calls fail,
102+
// the basic version info is still displayed
103+
104+
// Capture stdout
105+
oldStdout := os.Stdout
106+
r, w, _ := os.Pipe()
107+
os.Stdout = w
108+
109+
showVersionInfo()
110+
111+
w.Close()
112+
os.Stdout = oldStdout
113+
114+
var buf bytes.Buffer
115+
buf.ReadFrom(r)
116+
output := buf.String()
117+
118+
// Even if network fails, we should still get basic info
119+
if !strings.Contains(output, "tuido") {
120+
t.Error("Output should always contain 'tuido' even on network failure")
121+
}
122+
123+
if !strings.Contains(output, "Platform:") {
124+
t.Error("Output should always contain 'Platform:' even on network failure")
125+
}
126+
127+
if !strings.Contains(output, "Asset:") {
128+
t.Error("Output should always contain 'Asset:' even on network failure")
129+
}
130+
131+
// Should either show "Available:" with asset info or error message
132+
hasAvailable := strings.Contains(output, "Available:")
133+
if !hasAvailable {
134+
t.Error("Output should contain 'Available:' line with either asset info or error message")
135+
}
136+
}

0 commit comments

Comments
 (0)