Skip to content

Commit

Permalink
refactor(cmd): Clean up code dealing with the current version
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 3, 2024
1 parent 1a15dd7 commit f666b50
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ builds:
- CGO_ENABLED=0
flags:
- -trimpath
ldflags:
- -s
- -w
- -X github.com/gabe565/ascii-movie/cmd.version={{ .Version }}
tags:
- gzip
goarch:
Expand Down
16 changes: 6 additions & 10 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import (
flag "github.com/spf13/pflag"
)

func NewCommand(version, commit string) *cobra.Command {
var version = "beta"

func NewCommand() *cobra.Command {
cmdVersion, commit := buildVersion(version)
cmd := &cobra.Command{
Use: "ascii-movie",
Short: "Command line ASCII movie player.",
Version: buildVersion(version, commit),
Version: cmdVersion,

PersistentPreRunE: preRun,
DisableAutoGenTag: true,
}
cmd.AddCommand(
play.NewCommand(),
serve.NewCommand(),
serve.NewCommand(version, commit),
ls.NewCommand(),
get.NewCommand(),
)
Expand Down Expand Up @@ -57,10 +60,3 @@ func loadFlagEnvs(flags *flag.FlagSet) error {
})
return errors.Join(errs...)
}

func buildVersion(version, commit string) string {
if commit != "" {
version += " (" + commit + ")"
}
return version
}
9 changes: 8 additions & 1 deletion cmd/serve/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import (
"github.com/gabe565/ascii-movie/internal/movie"
"github.com/gabe565/ascii-movie/internal/server"
"github.com/muesli/termenv"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

func NewCommand() *cobra.Command {
func NewCommand(version, commit string) *cobra.Command {
cmd := &cobra.Command{
Use: "serve [movie]",
Aliases: []string{"server", "listen"},
Args: cobra.MaximumNArgs(1),
Short: "Serve an ASCII movie over Telnet and SSH.",
RunE: run,

Annotations: map[string]string{"version": version, "commit": commit},
ValidArgsFunction: movie.CompleteMovieName,
}

Expand All @@ -35,6 +37,11 @@ func NewCommand() *cobra.Command {
var ErrAllDisabled = errors.New("all server types are disabled")

func run(cmd *cobra.Command, args []string) error {
log.Info().
Str("version", cmd.Annotations["version"]).
Str("commit", cmd.Annotations["commit"]).
Msg("ASCII Movie")

var path string
if len(args) > 0 {
path = args[0]
Expand Down
31 changes: 31 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import "runtime/debug"

func buildVersion(version string) (string, string) {
var commit string
var modified bool
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
commit = setting.Value
case "vcs.modified":
if setting.Value == "true" {
modified = true
}
}
}
}

if commit != "" {
if len(commit) > 8 {
commit = commit[:8]
}
if modified {
commit = "*" + commit
}
version += " (" + commit + ")"
}
return version, commit
}
12 changes: 12 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_buildVersion(t *testing.T) {
got, _ := buildVersion("0.0.0-next")
assert.Equal(t, "0.0.0-next", got)
}
2 changes: 1 addition & 1 deletion internal/generate/completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
log.Fatal().Err(err).Msg("failed to create completions dir")
}

rootCmd := cmd.NewCommand("latest", "")
rootCmd := cmd.NewCommand()
name := rootCmd.Name()
var buf bytes.Buffer
rootCmd.SetOut(&buf)
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
log.Fatal().Err(err).Msg("failed to create directory")
}

rootCmd := cmd.NewCommand("latest", "")
rootCmd := cmd.NewCommand()
if err := doc.GenMarkdownTree(rootCmd, output); err != nil {
log.Fatal().Err(err).Msg("failed to generate docs")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/manpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
log.Fatal().Err(err).Msg("failed to create manpages dir")
}

rootCmd := cmd.NewCommand("beta", "")
rootCmd := cmd.NewCommand()
rootName := rootCmd.Name()

date, err := time.Parse(time.RFC3339, dateParam)
Expand Down
8 changes: 1 addition & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ import (

//go:generate go run ./internal/generate/gzip

//nolint:gochecknoglobals
var (
version = "beta"
commit = ""
)

func main() {
if err := cmd.NewCommand(version, commit).Execute(); err != nil {
if err := cmd.NewCommand().Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit f666b50

Please sign in to comment.