Skip to content

Commit

Permalink
Add version flag
Browse files Browse the repository at this point in the history
The main.version variable can be overridden at build time:

go build -ldflags="-X 'main.version=v0.0.1'"

This is done by default by goreleaser (see
https://goreleaser.com/customization/build/)
  • Loading branch information
sboehler committed Feb 10, 2021
1 parent 31db80d commit 72fd0ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
23 changes: 6 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
package cmd

import (
"fmt"
"os"

"github.com/sboehler/knut/cmd/balance"
"github.com/sboehler/knut/cmd/benchmark"
"github.com/sboehler/knut/cmd/completion"
Expand All @@ -33,11 +30,12 @@ import (
)

// CreateCmd creates the command.
func CreateCmd() *cobra.Command {
func CreateCmd(version string) *cobra.Command {
var c = &cobra.Command{
Use: "knut",
Short: "knut is a plain text accounting tool",
Long: `knut is a plain text accounting tool for tracking personal finances and investments.`,
Use: "knut",
Short: "knut is a plain text accounting tool",
Long: `knut is a plain text accounting tool for tracking personal finances and investments.`,
Version: version,
}
c.AddCommand(balance.CreateCmd())
c.AddCommand(importer.CreateCmd())
Expand All @@ -48,15 +46,6 @@ func CreateCmd() *cobra.Command {
c.AddCommand(benchmark.CreateCmd())
c.AddCommand(web.CreateCmd())
c.AddCommand(completion.CreateCmd(c))
return c
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
c := CreateCmd()
if err := c.Execute(); err != nil {
fmt.Fprintln(c.ErrOrStderr(), err)
os.Exit(1)
}
return c
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package main

import (
"fmt"
"os"

"github.com/sboehler/knut/cmd"

// enable importers here
Expand All @@ -27,6 +30,12 @@ import (
_ "github.com/sboehler/knut/cmd/importer/viac"
)

var version = "development"

func main() {
cmd.Execute()
var c = cmd.CreateCmd(version)
if err := c.Execute(); err != nil {
fmt.Fprintln(c.ErrOrStderr(), err)
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion scripts/builddoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func generate(c *config) error {
}

func run(args []string) string {
var c = cmd.CreateCmd()
var c = cmd.CreateCmd("development")
c.SetArgs(args)
var b strings.Builder
b.WriteString("$ knut")
Expand Down

0 comments on commit 72fd0ea

Please sign in to comment.