mirror of
https://github.com/axllent/goptimize.git
synced 2024-12-21 09:36:23 -05:00
Switch to pflag
This commit is contained in:
parent
36e9b4c8fa
commit
97fa6c94c5
@ -1,5 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## [dev]
|
||||
|
||||
- Switch to [pflag](https://github.com/spf13/pflag) for better flag management
|
||||
|
||||
|
||||
## [0.0.1]
|
||||
|
||||
- Initial release
|
||||
|
3
Makefile
3
Makefile
@ -6,7 +6,7 @@ build = echo "\n\nBuilding $(1)-$(2)" && GOOS=$(1) GOARCH=$(2) go build ${LDFLAG
|
||||
&& bzip2 dist/goptimize_${VERSION}_$(1)_$(2)
|
||||
|
||||
goptimize: *.go
|
||||
go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel
|
||||
go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel github.com/spf13/pflag
|
||||
go build ${LDFLAGS} -o goptimize
|
||||
rm -rf /tmp/go-*
|
||||
|
||||
@ -16,6 +16,7 @@ clean:
|
||||
release:
|
||||
mkdir -p dist
|
||||
rm -f dist/goptimize_${VERSION}_*
|
||||
go get github.com/disintegration/imaging golang.org/x/image/bmp golang.org/x/image/tiff github.com/axllent/gitrel github.com/spf13/pflag
|
||||
$(call build,linux,amd64)
|
||||
$(call build,linux,386)
|
||||
$(call build,linux,arm)
|
||||
|
14
goptimize.go
14
goptimize.go
@ -116,19 +116,19 @@ func Goptimize(file string) {
|
||||
if format.String() == "JPEG" {
|
||||
// run one or the other, running both has no advantage
|
||||
if jpegtran != "" {
|
||||
RunOptimiser(tmpFilename, true, jpegtran, "-optimize", "-outfile")
|
||||
RunOptimizer(tmpFilename, true, jpegtran, "-optimize", "-outfile")
|
||||
} else if jpegoptim != "" {
|
||||
RunOptimiser(tmpFilename, false, jpegoptim, "-f", "-s", "-o")
|
||||
RunOptimizer(tmpFilename, false, jpegoptim, "-f", "-s", "-o")
|
||||
}
|
||||
} else if format.String() == "PNG" {
|
||||
if pngquant != "" {
|
||||
RunOptimiser(tmpFilename, true, pngquant, "-f", "--output")
|
||||
RunOptimizer(tmpFilename, true, pngquant, "-f", "--output")
|
||||
}
|
||||
if optipng != "" {
|
||||
RunOptimiser(tmpFilename, true, optipng, "-out")
|
||||
RunOptimizer(tmpFilename, true, optipng, "-out")
|
||||
}
|
||||
} else if format.String() == "GIF" && gifsicle != "" {
|
||||
RunOptimiser(tmpFilename, true, gifsicle, "-o")
|
||||
RunOptimizer(tmpFilename, true, gifsicle, "-o")
|
||||
}
|
||||
|
||||
// re-open modified temporary file
|
||||
@ -215,9 +215,9 @@ func Goptimize(file string) {
|
||||
|
||||
}
|
||||
|
||||
// RunOptimiser will run the specified command on a copy of the temporary file,
|
||||
// RunOptimizer will run the specified command on a copy of the temporary file,
|
||||
// and overwrite it if the output is smaller than the original
|
||||
func RunOptimiser(src string, outFileArg bool, args ...string) {
|
||||
func RunOptimizer(src string, outFileArg bool, args ...string) {
|
||||
// create a new temp file
|
||||
tmpFile, err := ioutil.TempFile(os.TempDir(), "Goptimized-")
|
||||
|
||||
|
32
main.go
32
main.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -9,6 +8,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/axllent/gitrel"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -26,11 +26,15 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// set up new flag instance
|
||||
flag := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||
|
||||
// set the default help
|
||||
flag.Usage = func() {
|
||||
fmt.Println("Goptimize - downscales and optimizes images")
|
||||
fmt.Printf("\nUsage: %s [options] <images>\n", os.Args[0])
|
||||
fmt.Println("\nOptions:")
|
||||
flag.SortFlags = false
|
||||
flag.PrintDefaults()
|
||||
fmt.Println("\nExamples:")
|
||||
fmt.Printf(" %s image.png\n", os.Args[0])
|
||||
@ -47,14 +51,15 @@ func main() {
|
||||
}
|
||||
|
||||
var maxSizes string
|
||||
var update, showversion bool
|
||||
var update, showversion, showhelp bool
|
||||
|
||||
flag.IntVar(&quality, "q", 75, "quality - JPEG only")
|
||||
flag.StringVar(&outputDir, "o", "", "output directory (default overwrites original)")
|
||||
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.BoolVar(&update, "u", false, "update to latest release")
|
||||
flag.BoolVar(&showversion, "v", false, "show version number")
|
||||
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>)")
|
||||
flag.StringVarP(&outputDir, "out", "o", "", "output directory (default overwrites original)")
|
||||
flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times")
|
||||
flag.BoolVarP(&update, "update", "u", false, "update to latest release")
|
||||
flag.BoolVarP(&showversion, "version", "v", false, "show version number")
|
||||
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
|
||||
|
||||
// third-party optimizers
|
||||
flag.StringVar(&gifsicle, "gifsicle", "gifsicle", "gifsicle binary")
|
||||
@ -63,8 +68,10 @@ func main() {
|
||||
flag.StringVar(&optipng, "optipng", "optipng", "optipng binary")
|
||||
flag.StringVar(&pngquant, "pngquant", "pngquant", "pngquant binary")
|
||||
|
||||
// parse flags
|
||||
flag.Parse()
|
||||
flag.SortFlags = false
|
||||
|
||||
// parse args excluding os.Args[0]
|
||||
flag.Parse(os.Args[1:])
|
||||
|
||||
// detect optimizer paths
|
||||
gifsicle, _ = exec.LookPath(gifsicle)
|
||||
@ -73,6 +80,11 @@ func main() {
|
||||
optipng, _ = exec.LookPath(optipng)
|
||||
pngquant, _ = exec.LookPath(pngquant)
|
||||
|
||||
if showhelp {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if showversion {
|
||||
fmt.Println(fmt.Sprintf("Version: %s", version))
|
||||
latest, _, _, err := gitrel.Latest("axllent/goptimize", "goptimize")
|
||||
|
Loading…
Reference in New Issue
Block a user