Skip to content

Commit 1bbd0d8

Browse files
Add version subcommand to stellar-etl CLI (#347)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: leighmcculloch <351529+leighmcculloch@users.noreply.github.com>
1 parent c93554b commit 1bbd0d8

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

cmd/version.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"runtime/debug"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var versionCmd = &cobra.Command{
12+
Use: "version",
13+
Short: "Display version information",
14+
Long: `Display the version of stellar-etl and the versions of XDR libs.`,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
buildInfo, ok := debug.ReadBuildInfo()
17+
if !ok {
18+
fmt.Fprintf(cmd.OutOrStdout(), "stellar-etl (unknown)\n")
19+
return
20+
}
21+
fmt.Fprintf(cmd.OutOrStdout(), "stellar-etl %s\n", buildInfo.Main.Version)
22+
23+
// Find and display versions of libs containing XDR
24+
printDepVersion(cmd.OutOrStdout(), buildInfo, "github.com/stellar/go")
25+
printDepVersion(cmd.OutOrStdout(), buildInfo, "github.com/stellar/go-stellar-xdr-json")
26+
},
27+
}
28+
29+
func printDepVersion(out io.Writer, buildInfo *debug.BuildInfo, name string) {
30+
version := "(unknown)"
31+
for _, dep := range buildInfo.Deps {
32+
if dep.Path == name {
33+
version = dep.Version
34+
break
35+
}
36+
}
37+
fmt.Fprintf(out, "%s %s\n", name, version)
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(versionCmd)
42+
}

cmd/version_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestVersionCommand(t *testing.T) {
13+
cmd := &cobra.Command{}
14+
cmd.AddCommand(versionCmd)
15+
out := new(bytes.Buffer)
16+
cmd.SetOut(out)
17+
cmd.SetArgs([]string{"version"})
18+
err := cmd.Execute()
19+
require.NoError(t, err)
20+
21+
outStr := out.String()
22+
assert.Contains(t, outStr, "stellar-etl")
23+
assert.Contains(t, outStr, "github.com/stellar/go")
24+
assert.Contains(t, outStr, "github.com/stellar/go-stellar-xdr-json")
25+
}

0 commit comments

Comments
 (0)