use github.com/spf13/cobra for app cmds
All checks were successful
continuous-integration/drone/push Build is passing

* add 'version' command - "go-xkcdreader version" returns a formatted
  version of the app
* test (among other things) that the version in flake.nix matches the
  version hardcoded in app's go code (cmd/version.go)
This commit is contained in:
surtur 2022-05-12 15:54:16 +02:00
parent f009bb2cc1
commit 4b6e655ebf
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
9 changed files with 211 additions and 15 deletions

35
cmd/root.go Normal file
View File

@ -0,0 +1,35 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
// appName is the name of the app
const appName = "go-xkcdreader"
// Root is the main go-xkcdreader command
var Root = &cobra.Command{
Use: appName,
Short: "an offline-capable xkcd webcomic reader written in Go",
RunE: func(cmd *cobra.Command, args []string) error {
// the app will be started from here
// a noop atm
return nil
},
}
// GetAppName returns the name of the application
func GetAppName() string {
return appName
}
// Execute is the entrypoint of cobra's poison
func Execute() {
if err := Root.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

28
cmd/root_test.go Normal file
View File

@ -0,0 +1,28 @@
package cmd
import (
"testing"
)
func TestGetAppName(t *testing.T) {
want := "go-xkcdreader"
got := GetAppName()
if got != want {
t.Errorf("App name strings don't match, want: %q', got: %q ", want, got)
}
}
func TestExecuteRootCmd(t *testing.T) {
rootcmd := *Root
err := rootcmd.Execute()
if err != nil {
t.Error("Failed to execute the root cmd")
}
}
// is this valid? dunno
func TestExecuteFunc(t *testing.T) {
Execute()
}

47
cmd/version.go Normal file
View File

@ -0,0 +1,47 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
// version holds app version string
var version = "v0.0.0"
// used to determine whether to print short or long version string
var shortVersion = false
var cmdVersion = &cobra.Command{
Use: "version",
Short: "Print version information and exit",
Long: `All software has versions. This is ` + appName + `'s`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(getVersion())
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// exit with code 0 after showing the version
os.Exit(0)
},
}
// get (short if -s/--short flag is supplied) version
func getVersion() string {
if !shortVersion {
return appName + " - " + version
}
return GetShortVersion()
}
// GetShortVersion returns a bare version string
func GetShortVersion() string {
return version
}
// the init func registers the commands and flags with cobra
func init() {
cmdVersion.Flags().BoolVarP(&shortVersion, "short", "s", false, "print just the version string and nothing else")
Root.AddCommand(cmdVersion)
}

66
cmd/version_test.go Normal file
View File

@ -0,0 +1,66 @@
package cmd
import (
"bytes"
"os"
"regexp"
"testing"
)
func TestNixFlake_GosrcVersionStringEquivalence(t *testing.T) {
want := GetShortVersion()
fData, err := os.ReadFile("../flake.nix")
if err != nil {
t.Errorf("Failed to open flake.nix: %q", err)
}
got := bytes.Contains(fData, []byte(" version = \""+want+"\""))
if !got {
t.Errorf("Version string '%q' not found in ../flake.nix", want)
}
}
func TestVersionStringFormat(t *testing.T) {
v := version
// this expression matches the format of "vX.Y.Z" where {X,Y,Z} are digits
// (such as 0 or 123)
re := regexp.MustCompile(`^(v\d+\.\d+\.\d+)$`)
match := re.MatchString(v)
if !match {
t.Errorf("Incorrect version string format, must match regex: '%q', provided version string: '%q'", re.String(), v)
}
}
func TestGetVersion(t *testing.T) {
want := appName + " - " + version
got := getVersion()
if want != got {
t.Errorf("Incorrect version string, want: '%q', got: '%q'", want, got)
}
}
// set shortVersion variable manually
func TestGetVersionWithShortVersionVar(t *testing.T) {
shortVersion = true
want := version
got := getVersion()
if want != got {
t.Errorf("Incorrect version string when 'shortVersion' is true, want: '%q', got: '%q'", want, got)
}
}
// explicitly get short version
func TestGetShortVersion(t *testing.T) {
want := version
got := GetShortVersion()
if want != got {
t.Errorf("Incorrect 'short' version string, want: '%q', got: '%q'", want, got)
}
}

View File

@ -69,13 +69,12 @@
ldflags = [
"-s"
"-w"
"-X main.Version=${version}"
"-X main.Commit=${version}"
"-X cmd.version=${version}"
];
modSha256 = lib.fakeSha256;
# vendorSha256 = "";
vendorSha256 = "sha256-7ofcdvYLiAhR7bTyzcBMbyj3Sd8UIcGiFOOxgI87Yx8=";
vendorSha256 = "sha256-i6VOX1O/IqIOWUzgc/2U/a95KhzAG0/NOIG/D8E0BEk=";
# In 'nix develop', we don't need a copy of the source tree
# in the Nix store.
@ -115,7 +114,7 @@
name = "go-xkcdreader-" + version;
GOFLAGS = "-buildmode=pie -trimpath -mod=readonly -modcacherw";
GOLDFLAGS = "-s -w -X main.Version=${version} -X main.Commit=${version}";
GOLDFLAGS = "-s -w -X cmd.version=${version}";
CGO_CFLAGS = "-g2 -Og -mtune=generic";
CGO_LDFLAGS = "-Wl,-O1,-sort-common,-as-needed,-z,relro,-z,now,-flto -pthread";

9
go.mod
View File

@ -2,7 +2,10 @@ module git.dotya.ml/wanderer/go-xkcdreader
go 1.17
require fyne.io/fyne/v2 v2.1.4
require (
fyne.io/fyne/v2 v2.1.4
github.com/spf13/cobra v1.4.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
@ -12,7 +15,9 @@ require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211024062804-40e447a793be // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect
github.com/stretchr/testify v1.5.1 // indirect
@ -21,5 +26,5 @@ require (
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

12
go.sum
View File

@ -5,6 +5,7 @@ github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -21,6 +22,8 @@ github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff h1:W71vTCKoxtdXgnm1ECDFkfQnpdqAO00zzGXLA5yaEX8=
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc=
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -35,9 +38,14 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 h1:HunZiaEKNGVdhTRQOVpMmj5MQnGnv+e8uZNu3xFLgyM=
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564/go.mod h1:afMbS0qvv1m5tfENCwnOdZGOF8RGR/FsZ7bvBxQGZG4=
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 h1:m59mIOBO4kfcNCEzJNy71UkeF4XIx2EVmL9KLwDQdmM=
@ -91,5 +99,5 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

13
main.go
View File

@ -3,20 +3,23 @@
import (
"fmt"
"git.dotya.ml/wanderer/go-xkcdreader/cmd"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
const appGreeting = "welcome to go-xkcdreader"
// Version provides app version string
const Version = "0.0.0"
var version = cmd.GetShortVersion()
func main() {
fmt.Println("Starting go-xkcdreader " + getVersion())
cmd.Execute()
fmt.Println("Starting " + cmd.GetAppName() + " " + getVersion())
a := app.New()
w := a.NewWindow("go-xkcd_reader")
w := a.NewWindow(cmd.GetAppName())
w.SetContent(makeGreeting())
@ -30,5 +33,5 @@ func makeGreeting() *widget.Label {
}
func getVersion() string {
return "v" + Version
return version
}

View File

@ -1,10 +1,15 @@
package main
import "testing"
import (
"testing"
"git.dotya.ml/wanderer/go-xkcdreader/cmd"
)
func TestGreetingText(t *testing.T) {
want := "welcome to go-xkcdreader"
want := "welcome to " + cmd.GetAppName()
got := makeGreeting()
if got.Text != want {
t.Errorf("Incorrect initial greeting, want: %q, got: %q", want, got.Text)
}