Skip to content

Commit

Permalink
Automatically check for new versions of audius-ctl (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
phelpsdb authored Apr 24, 2024
1 parent be6b540 commit 4d030a5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
26 changes: 22 additions & 4 deletions cmd/audius-ctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,31 @@ var (
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: contextCompletionFunction,
RunE: func(cmd *cobra.Command, args []string) error {
ctxName, err := conf.GetCurrentContextName()
if err != nil {
return logger.Error(err)
}
var ctxName string
if len(args) > 0 {
ctxName = args[0]
ctxs, err := conf.GetContexts()
if err != nil {
return logger.Error("Error getting available contexts:", err)
}
contains := false
for _, ctx := range ctxs {
if ctx == ctxName {
contains = true
break
}
}
if !contains {
return logger.Errorf("No context named '%s'. Use 'audius-ctl config create-context %s'", ctxName, ctxName)
}
} else {
var err error
ctxName, err = conf.GetCurrentContextName()
if err != nil {
return logger.Error(err)
}
}

if err := EditConfig(ctxName); err != nil {
return logger.Error(err)
}
Expand Down
66 changes: 61 additions & 5 deletions cmd/audius-ctl/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"regexp"
"strings"
"syscall"

"github.com/AudiusProject/audius-d/pkg/logger"
"github.com/google/go-github/v61/github"
"github.com/spf13/cobra"
)

var (
Version string
displayVersion bool
debugLogging bool
Version string
displayVersion bool
debugLogging bool
audiusctlSemverRegex = regexp.MustCompile(`\d+\.\d+\.\d+`)
)

func main() {
msgCh := make(chan string)
go checkNewVersion(msgCh)

var rootCmd = &cobra.Command{
Use: "audius-ctl [command]",
Short: "CLI for provisioning and interacting with audius nodes",
Expand All @@ -44,13 +52,61 @@ func main() {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Fprintf(os.Stderr, "Interrupted\n")
fmt.Fprintf(os.Stderr, "\nInterrupted\n")
fmt.Fprintf(os.Stderr, "View full debug logs at %s\n", logger.GetLogFilepath())
os.Exit(1)
}()

if err := rootCmd.Execute(); err != nil {
err := rootCmd.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "View full debug logs at %s\n", logger.GetLogFilepath())
}

vmsg := <-msgCh
if vmsg != "" {
fmt.Fprintf(os.Stderr, vmsg)
}

if err != nil {
os.Exit(1)
}
}

func checkNewVersion(msg chan string) {
// Skip new version check for development builds
if !audiusctlSemverRegex.MatchString(Version) {
logger.Debug("Skipping check for new audius-ctl versions.")
msg <- ""
return
}

// Fetch the latest release information from GitHub API
ghClient := github.NewClient(nil)
release, resp, err := ghClient.Repositories.GetLatestRelease(context.Background(), "AudiusProject", "audius-d")
if err != nil {
logger.Debugf("Failed to check for new versions of audius-ctl: %s", err.Error())
msg <- ""
return
}
defer resp.Body.Close()
if !strings.HasPrefix(resp.Status, "2") {
logger.Debugf("Failed to check for new versions of audius-ctl: got code %s", resp.Status)
msg <- ""
return
}

if release.TagName == nil {
logger.Debug("Failed to check for new versions of audius-ctl: Null tag for latest release")
msg <- ""
return
}
splitTag := strings.Split(*release.TagName, "@")
latestVersion := splitTag[len(splitTag)-1]
if latestVersion != Version {
logger.Debugf("Found new version of audius-ctl: %s (current: %s)", latestVersion, Version)
msg <- fmt.Sprintf("\nNew version of audius-ctl available: %s (you have %s)\nRun `curl -sSL https://install.audius.org | sh` to install.\n", latestVersion, Version)
} else {
logger.Debug("Current version of audius-ctl is up-to-date.")
msg <- ""
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ require (
github.com/golang/glog v1.1.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-github/v61 v61.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,14 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5pNlu1go=
github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
Expand Down

0 comments on commit 4d030a5

Please sign in to comment.