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