Skip to content

Commit

Permalink
Add versioning
Browse files Browse the repository at this point in the history
Co-authored-by: Hakkin Lain <hakkin@github>
  • Loading branch information
jedai47 and Hakkin authored Jun 3, 2024
1 parent dba5ed5 commit 46cfb08
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"runtime/debug"
"strings"

"rsc.io/getopt"
Expand Down Expand Up @@ -50,6 +51,9 @@ var (
groupList bool
groupListDefault = false

showVersion bool
showVersionDefault = false

accessTokenPlatform string
accessTokenPlatformDefault = "web"

Expand All @@ -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")
Expand All @@ -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...] <USERNAME> [COMMAND...]")
stdErr.Println()
Expand Down

0 comments on commit 46cfb08

Please sign in to comment.