Skip to content

Commit

Permalink
Add show version feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiya-kume committed Jan 11, 2025
1 parent 4d7c39b commit 62be27b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ builds:
- amd64
- arm64
ldflags:
- "-s -w"
- "-s -w -X main.version={{ .Version }} -X main.architecture={{ .Arch }}"
archives:
- format: tar.gz
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
Expand Down
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"os"
)

var (
version string
architecture string
)

func initFlags() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flag.BoolVar(&versionFlag, "version", false, "Print version information")
flag.BoolVar(&vFlag, "v", false, "Print version information")
}

var (
versionFlag bool
vFlag bool
)

func run() int {
if len(os.Args) < 2 {
printUsage()
Expand Down Expand Up @@ -66,5 +82,16 @@ func handlePush(configs []Config) int {
}

func main() {
initFlags()
flag.Parse()

if versionFlag || vFlag {
fmt.Printf("Version: %s\n", version)
fmt.Printf("Architecture: %s\n", architecture)
return
}

fmt.Printf("Version: %s\n", version)
fmt.Printf("Architecture: %s\n", architecture)
os.Exit(run())
}
38 changes: 37 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"flag"
"io"
"os"
"testing"
Expand All @@ -12,6 +13,10 @@ var osExit = os.Exit

// Remove remaining pull-related tests

func resetFlags() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}

func TestMain_NoCommand_ShowsHelp(t *testing.T) {
// Save the original os.Exit function and restore it after the test
originalExit := osExit
Expand Down Expand Up @@ -40,17 +45,48 @@ func TestMain_NoCommand_ShowsHelp(t *testing.T) {
}
}

func TestMain_VersionFlag(t *testing.T) {
resetFlags()
originalArgs := os.Args
defer func() { os.Args = originalArgs }()

os.Args = []string{"cmd", "--version"}
output := captureOutput(func() {
main()
})

if !bytes.Contains([]byte(output), []byte("Version: ")) || !bytes.Contains([]byte(output), []byte("Architecture: ")) {
t.Errorf("Expected version and architecture information to be printed, got: %s", output)
}
}

func TestMain_VFlag(t *testing.T) {
resetFlags()
originalArgs := os.Args
defer func() { os.Args = originalArgs }()

os.Args = []string{"cmd", "-v"}
output := captureOutput(func() {
main()
})

if !bytes.Contains([]byte(output), []byte("Version: ")) || !bytes.Contains([]byte(output), []byte("Architecture: ")) {
t.Errorf("Expected version and architecture information to be printed, got: %s", output)
}
}

// Helper function to capture output
func captureOutput(f func()) string {
r, w, _ := os.Pipe()
originalStdout := os.Stdout
os.Stdout = w

f()

w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
os.Stdout = os.Stdout
os.Stdout = originalStdout

return buf.String()
}

0 comments on commit 62be27b

Please sign in to comment.