2022-05-21 00:49:13 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-05-12 15:54:16 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-09 23:47:05 +02:00
|
|
|
"fmt"
|
2022-05-12 15:54:16 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// version holds app version string.
|
2022-05-15 11:27:48 +02:00
|
|
|
var version = "v0.0.13"
|
2022-05-12 15:54:16 +02:00
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// used to determine whether to print short or long version string.
|
2022-05-12 15:54:16 +02:00
|
|
|
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 {
|
2022-06-09 23:47:05 +02:00
|
|
|
fmt.Fprintln(os.Stderr, getVersion())
|
2022-05-12 15:54:16 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// exit with code 0 after showing the version
|
|
|
|
os.Exit(0)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// get (short if -s/--short flag is supplied) version.
|
2022-05-12 15:54:16 +02:00
|
|
|
func getVersion() string {
|
|
|
|
if !shortVersion {
|
|
|
|
return appName + " - " + version
|
|
|
|
}
|
2022-05-27 23:08:17 +02:00
|
|
|
|
2022-05-12 15:54:16 +02:00
|
|
|
return GetShortVersion()
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// GetShortVersion returns a bare version string.
|
2022-05-12 15:54:16 +02:00
|
|
|
func GetShortVersion() string {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2022-05-27 22:14:29 +02:00
|
|
|
// the init func registers the commands and flags with cobra.
|
2022-05-12 15:54:16 +02:00
|
|
|
func init() {
|
|
|
|
cmdVersion.Flags().BoolVarP(&shortVersion, "short", "s", false, "print just the version string and nothing else")
|
|
|
|
Root.AddCommand(cmdVersion)
|
|
|
|
}
|