Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a version command line parameter that reads from a const in a … #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
invoice
*.pdf
*.json
*.yaml
Expand Down
9 changes: 9 additions & 0 deletions get_version_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Get the version.
version=`git describe --tags --long`
# Write out the package.
cat << EOF > version_linux.go
package main

//go:generate bash ./get_version_linux.sh
var version = "$version"
EOF
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func DefaultInvoice() Invoice {
var (
importPath string
output string
show_ver_only bool
file = Invoice{}
defaultInvoice = DefaultInvoice()
)
Expand Down Expand Up @@ -88,6 +89,8 @@ func init() {
generateCmd.Flags().StringVarP(&file.Note, "note", "n", "", "Note")
generateCmd.Flags().StringVarP(&output, "output", "o", "invoice.pdf", "Output file (.pdf)")

versionCmd.Flags().BoolVarP(&show_ver_only, "short", "s", false, "Show version number only")

flag.Parse()
}

Expand All @@ -97,6 +100,20 @@ var rootCmd = &cobra.Command{
Long: `Invoice generates invoices from the command line.`,
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version information",
Long: `Show version information`,
RunE: func(cmd *cobra.Command, args []string) error {
if show_ver_only {
fmt.Printf("%s\n", version)
} else {
fmt.Printf("invoice version %s\n%s\n", version, "(c) by Maas Lalani (https://github.com/maaslalani)")
}
return nil
},
}

var generateCmd = &cobra.Command{
Use: "generate",
Short: "Generate an invoice",
Expand Down Expand Up @@ -166,6 +183,7 @@ var generateCmd = &cobra.Command{
}

func main() {
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(generateCmd)
err := rootCmd.Execute()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions version_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

//go:generate bash ./get_version_linux.sh
var version = "v0.1.0-6-g40960ce"