2023-02-19 04:42:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
const versionStr = "0.3.0"
|
2023-02-19 04:42:15 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
versionCmd = &cobra.Command{
|
2023-03-10 22:01:02 +00:00
|
|
|
Use: "version [flags]",
|
2023-07-02 02:46:54 +00:00
|
|
|
Short: "Prints the version and exits",
|
|
|
|
Long: "Prints the version and exits. No arguments just prints the version number only.",
|
2023-02-19 04:42:15 +00:00
|
|
|
Run: version,
|
|
|
|
}
|
2023-03-10 22:01:02 +00:00
|
|
|
numOnly bool
|
2023-05-12 05:42:14 +00:00
|
|
|
vPre bool
|
2023-02-19 04:42:15 +00:00
|
|
|
)
|
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
func init() {
|
2023-07-02 03:02:32 +00:00
|
|
|
versionCmd.PersistentFlags().BoolVarP(&numOnly, "num", "n", false, "Output the version number only.")
|
2023-07-02 02:46:54 +00:00
|
|
|
versionCmd.PersistentFlags().BoolVarP(&vPre, "vpre", "V", false, "Output the version with v prefixed.")
|
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
|
2023-07-02 02:46:54 +00:00
|
|
|
func version(cmd *cobra.Command, args []string) {
|
2023-05-12 05:42:14 +00:00
|
|
|
|
|
|
|
if numOnly && !vPre {
|
2023-03-10 22:01:02 +00:00
|
|
|
fmt.Printf("%s\n", versionStr)
|
2023-07-02 02:46:54 +00:00
|
|
|
} else if vPre && !numOnly {
|
|
|
|
fmt.Printf("v%s\n", versionStr)
|
2023-03-10 22:01:02 +00:00
|
|
|
} else {
|
2023-07-02 02:46:54 +00:00
|
|
|
if vPre && numOnly {
|
|
|
|
fmt.Println("vpre flag and num flag both detected!")
|
|
|
|
}
|
|
|
|
fmt.Printf("Backy version: %s\n", versionStr)
|
2023-03-10 22:01:02 +00:00
|
|
|
}
|
2023-02-19 04:42:15 +00:00
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|