File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed
Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments