Use ghru/v2 for latest version checks and self-update functionality

This commit is contained in:
Ralph Slooten 2025-06-29 17:19:28 +12:00
parent 4d08aff7ec
commit 118c5bc9ac
3 changed files with 47 additions and 23 deletions

8
go.mod
View File

@ -1,14 +1,14 @@
module github.com/axllent/goptimize
go 1.21
go 1.23.0
toolchain go1.22.2
toolchain go1.23.1
require (
github.com/axllent/ghru v1.2.1
github.com/axllent/ghru/v2 v2.0.1
github.com/kovidgoyal/imaging v1.6.3
github.com/spf13/pflag v1.0.5
golang.org/x/image v0.18.0
)
require github.com/axllent/semver v0.0.1 // indirect
require golang.org/x/mod v0.25.0 // indirect

8
go.sum
View File

@ -1,10 +1,10 @@
github.com/axllent/ghru v1.2.1 h1:PuJQQeILJJ42O9nvjTwObWQGZDyZ9/F71st9jNAqgoU=
github.com/axllent/ghru v1.2.1/go.mod h1:YgznIILRJpnII5x8N080q/G8Milzk3sy9Sh4dV1eGQw=
github.com/axllent/semver v0.0.1 h1:QqF+KSGxgj8QZzSXAvKFqjGWE5792ksOnQhludToK8E=
github.com/axllent/semver v0.0.1/go.mod h1:2xSPzvG8n9mRfdtxSvWvfTfQGWfHsMsHO1iZnKATMSc=
github.com/axllent/ghru/v2 v2.0.1 h1:/9XHbMqJRGRxlyNMq2XiHQpBLz7hlq8xCdgJ4ZhjCoM=
github.com/axllent/ghru/v2 v2.0.1/go.mod h1:seMMjx8/0r5ZAL7c0vwTPIRoyN0AoTUqAylZEWZWGK4=
github.com/kovidgoyal/imaging v1.6.3 h1:iNPpv7ygiaB/NOztc6APMT7yr9UwBS+rOZwIbAdtyY8=
github.com/kovidgoyal/imaging v1.6.3/go.mod h1:sHvcLOOVhJuto2IoNdPLEqnAUoL5ZfHEF0PpNH+882g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=

54
main.go
View File

@ -1,3 +1,4 @@
// Package main is the main application
package main
import (
@ -9,7 +10,7 @@ import (
"strconv"
"sync"
"github.com/axllent/ghru"
"github.com/axllent/ghru/v2"
"github.com/spf13/pflag"
)
@ -27,6 +28,13 @@ var (
copyExif bool
threads = 1
version = "dev"
// ghruConf is the configuration for the ghru package
ghruConf = ghru.Config{
Repo: "axllent/goptimize",
ArchiveName: "goptimize-{{.OS}}-{{.Arch}}",
BinaryName: "goptimize",
CurrentVersion: version,
}
)
func main() {
@ -55,7 +63,7 @@ func main() {
}
var maxSizes string
var multiThreaded, update, showversion, showhelp bool
var multiThreaded, update, showVersion, showHelp bool
flag.IntVarP(&quality, "quality", "q", 75, "quality, JPEG only")
flag.StringVarP(&maxSizes, "max", "m", "", "downscale to a maximum width & height in pixels (<width>x<height>)")
@ -64,8 +72,8 @@ func main() {
flag.BoolVarP(&copyExif, "exif", "e", false, "copy exif data")
flag.BoolVarP(&update, "update", "u", false, "update to latest release")
flag.BoolVarP(&multiThreaded, "threaded", "t", false, "run multi-threaded (use all CPU cores)")
flag.BoolVarP(&showversion, "version", "v", false, "show version number")
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
flag.BoolVarP(&showVersion, "version", "v", false, "show version number")
flag.BoolVarP(&showHelp, "help", "h", false, "show help")
// third-party optimizers
flag.StringVar(&jpegtran, "jpegtran", "jpegtran", "jpegtran binary")
@ -86,28 +94,44 @@ func main() {
optipng, _ = exec.LookPath(optipng)
pngquant, _ = exec.LookPath(pngquant)
if showhelp {
if showHelp {
flag.Usage()
os.Exit(1)
}
if showversion {
fmt.Println(fmt.Sprintf("Version: %s", version))
latest, _, _, err := ghru.Latest("axllent/goptimize", "goptimize")
if err == nil && ghru.GreaterThan(latest, version) {
fmt.Printf("Update available: %s\nRun `%s -u` to update.\n", latest, os.Args[0])
if showVersion {
fmt.Printf("Version: %s\n", version)
release, err := ghruConf.Latest()
if err != nil {
fmt.Printf("Error getting latest release: %s\n", err.Error())
os.Exit(1)
}
return
// The latest version is the same version
if release.Tag == version {
os.Exit(0)
}
// A newer release is available
fmt.Printf(
"Update available: %s\nRun `%s -u` to update (requires read/write access to install directory).\n",
release.Tag,
os.Args[0],
)
os.Exit(0)
}
if update {
rel, err := ghru.Update("axllent/goptimize", "goptimize", version)
// Update the app
rel, err := ghruConf.SelfUpdate()
if err != nil {
fmt.Println(err)
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Printf("Updated %s to version %s\n", os.Args[0], rel)
return
fmt.Printf("Updated %s to version %s\n", os.Args[0], rel.Tag)
os.Exit(0)
}
if len(flag.Args()) < 1 {