Merge branch 'feature/updater' into develop

This commit is contained in:
Ralph Slooten 2019-08-03 10:24:09 +12:00
commit 7af9911032
3 changed files with 42 additions and 18 deletions

View File

@ -1,6 +1,6 @@
TAG=`git describe --tags` TAG=`git describe --tags`
VERSION ?= `git describe --tags` VERSION ?= `git describe --tags`
LDFLAGS=-ldflags "-s -extldflags \"--static\" -w -X main.version=${VERSION}" LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}"
build = echo "\n\nBuilding $(1)-$(2)" && GOOS=$(1) GOARCH=$(2) go build ${LDFLAGS} -o dist/goptimize_${VERSION}_$(1)_$(2) \ build = echo "\n\nBuilding $(1)-$(2)" && GOOS=$(1) GOARCH=$(2) go build ${LDFLAGS} -o dist/goptimize_${VERSION}_$(1)_$(2) \
&& bzip2 dist/goptimize_${VERSION}_$(1)_$(2) && bzip2 dist/goptimize_${VERSION}_$(1)_$(2)

View File

@ -170,7 +170,7 @@ func Goptimize(file string) {
return return
} }
if preserveModificationTimes { if preserveModTimes {
// transfer original modification times // transfer original modification times
if err := os.Chtimes(dstFile, atime, mtime); err != nil { if err := os.Chtimes(dstFile, atime, mtime); err != nil {
fmt.Printf("Error setting file timestamp: %v\n", err) fmt.Printf("Error setting file timestamp: %v\n", err)
@ -199,7 +199,7 @@ func Goptimize(file string) {
return return
} }
if preserveModificationTimes { if preserveModTimes {
// transfer original modification times // transfer original modification times
if err := os.Chtimes(dstFile, atime, mtime); err != nil { if err := os.Chtimes(dstFile, atime, mtime); err != nil {
fmt.Printf("Error setting file timestamp: %v\n", err) fmt.Printf("Error setting file timestamp: %v\n", err)

54
main.go
View File

@ -7,19 +7,22 @@ import (
"os/exec" "os/exec"
"regexp" "regexp"
"strconv" "strconv"
"github.com/axllent/gitrel"
) )
var ( var (
quality int quality int
maxWidth int maxWidth int
maxHeight int maxHeight int
outputDir string outputDir string
preserveModificationTimes bool preserveModTimes bool
jpegoptim string jpegoptim string
jpegtran string jpegtran string
optipng string optipng string
pngquant string pngquant string
gifsicle string gifsicle string
version = "dev"
) )
func main() { func main() {
@ -44,11 +47,14 @@ func main() {
} }
var maxSizes string var maxSizes string
var update, showversion bool
flag.IntVar(&quality, "q", 75, "quality - JPEG only") flag.IntVar(&quality, "q", 75, "quality - JPEG only")
flag.StringVar(&outputDir, "o", "", "output directory (default overwrites original)") flag.StringVar(&outputDir, "o", "", "output directory (default overwrites original)")
flag.BoolVar(&preserveModificationTimes, "p", true, "preserve file modification times") flag.BoolVar(&preserveModTimes, "p", true, "preserve file modification times")
flag.StringVar(&maxSizes, "m", "", "downscale to a maximum width & height in pixels (<width>x<height>)") flag.StringVar(&maxSizes, "m", "", "downscale to a maximum width & height in pixels (<width>x<height>)")
flag.BoolVar(&update, "u", false, "update to latest release")
flag.BoolVar(&showversion, "v", false, "show version number")
// third-party optimizers // third-party optimizers
flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary") flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary")
@ -67,9 +73,28 @@ func main() {
optipng, _ = exec.LookPath(optipng) optipng, _ = exec.LookPath(optipng)
pngquant, _ = exec.LookPath(pngquant) pngquant, _ = exec.LookPath(pngquant)
if showversion {
fmt.Println(fmt.Sprintf("Version: %s", version))
latest, _, _, err := gitrel.Latest("axllent/goptimize", "goptimize")
if err == nil && latest != version {
fmt.Printf("Update available: %s\nRun `%s -u` to update.\n", latest, os.Args[0])
}
return
}
if update {
rel, err := gitrel.Update("axllent/goptimize", "goptimize", version)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Updated %s to version %s", os.Args[0], rel)
return
}
if len(flag.Args()) < 1 { if len(flag.Args()) < 1 {
flag.Usage() flag.Usage()
return os.Exit(1)
} }
if maxSizes != "" { if maxSizes != "" {
@ -79,7 +104,7 @@ func main() {
if len(matches) != 4 { if len(matches) != 4 {
flag.Usage() flag.Usage()
return os.Exit(1)
} }
maxWidth, _ = strconv.Atoi(matches[1]) maxWidth, _ = strconv.Atoi(matches[1])
@ -95,7 +120,7 @@ func main() {
err := os.MkdirAll(outputDir, os.ModePerm) err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil { if err != nil {
fmt.Printf("Cannot create output directory: %s\n", outputDir) fmt.Printf("Cannot create output directory: %s\n", outputDir)
return os.Exit(1)
} }
} }
} }
@ -109,7 +134,6 @@ func main() {
func displayDelectedOptimizer(name, bin string) error { func displayDelectedOptimizer(name, bin string) error {
exe, err := exec.LookPath(bin) exe, err := exec.LookPath(bin)
if err != nil { if err != nil {
// fmt.Printf(" - %s: [undetected]\n", name)
return err return err
} }