From 46cfb08113030dd7149704e1738ffedaa77dcaca Mon Sep 17 00:00:00 2001 From: jedai47 <47899257+jedai47@users.noreply.github.com> Date: Mon, 3 Jun 2024 06:48:40 +0200 Subject: [PATCH] Add versioning Co-authored-by: Hakkin Lain --- main.go | 6 ++++++ options.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/main.go b/main.go index 05165af..f886a48 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,12 @@ var stdErr = log.New(os.Stderr, "", 0) func main() { getopt.Parse() + + if showVersion { + printVersion() + os.Exit(0) + } + if len(flag.Args()) < 1 { printUsage() os.Exit(1) diff --git a/options.go b/options.go index 5665ddb..703e1c1 100644 --- a/options.go +++ b/options.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "runtime/debug" "strings" "rsc.io/getopt" @@ -50,6 +51,9 @@ var ( groupList bool groupListDefault = false + showVersion bool + showVersionDefault = false + accessTokenPlatform string accessTokenPlatformDefault = "web" @@ -67,12 +71,14 @@ func init() { flag.BoolVar(&archiveMode, "a", archiveModeDefault, "Start downloading from the oldest segment rather than the newest") flag.StringVar(&groupSelect, "g", groupSelectDefault, "Select specified playlist group\n\t\"best\" will select the best available group") flag.BoolVar(&groupList, "G", groupListDefault, "List available playlist groups and exit") + flag.BoolVar(&showVersion, "v", showVersionDefault, "Show version information and exit") getopt.Aliases( "f", "force-output", "u", "url", "a", "archive", "g", "group", "G", "list-groups", + "v", "version", ) flag.StringVar(&accessTokenPlatform, "access-token-platform", accessTokenPlatformDefault, "The platform to send when acquiring an access token") @@ -82,6 +88,38 @@ func init() { flag.Var(&accessTokenDeviceID, "access-token-device-id", "Device ID to send when acquiring an access token (optional)") } +func printVersion() { + bi, ok := debug.ReadBuildInfo() + if !ok { + stdErr.Println("failed to read build info") + return + } + + var versionString string + + versionString += fmt.Sprintf("twitchpipe %s", bi.Main.Version) + + if bi.Main.Version == "(devel)" { + var rev string + for _, s := range bi.Settings { + if s.Key == "vcs.revision" { + rev = s.Value + break + } + } + + if rev != "" { + revLen := 7 + if len(rev) < revLen { + revLen = len(rev) + } + versionString += fmt.Sprintf(" %s", rev[:revLen]) + } + } + + stdErr.Println(versionString) +} + func printUsage() { stdErr.Println("Usage: twitchpipe [OPTIONS...] [COMMAND...]") stdErr.Println()