mirror of
https://github.com/axllent/goptimize.git
synced 2024-09-29 14:15:54 -04:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
788c6ecf23 | ||
|
dedca9b41d | ||
|
74cb212d22 | ||
|
1a6942417b | ||
|
2444204b32 | ||
|
423ebc1494 | ||
|
e5ecbe9bdf | ||
|
cc2b9d6b88 | ||
|
db2f3b8534 | ||
|
b9e5afefd4 | ||
|
e8f5085a11 | ||
|
6956f5fb08 | ||
|
e55c5de522 | ||
|
75ca6b7017 | ||
|
ea2d490686 | ||
|
e8c7298c9c | ||
|
bb18d90d0e | ||
|
90240fef3b | ||
|
2b1b34a4e0 | ||
|
529ea6971c | ||
|
4fa6fed8c9 | ||
|
745bed1273 | ||
|
d3cd263300 | ||
|
88af2a0a0c | ||
|
fbb22582d1 | ||
|
adac20101d | ||
|
521f3f24c1 | ||
|
3f9f09880c | ||
|
eadd535e4c |
23
CHANGELOG.md
23
CHANGELOG.md
@ -1,6 +1,27 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [dev]
|
## [0.2.3]
|
||||||
|
|
||||||
|
- Switch to kovidgoyal/imaging to address CVE-2023-36308
|
||||||
|
|
||||||
|
|
||||||
|
## [0.2.2]
|
||||||
|
|
||||||
|
- Optionally preserve exif data for supported formats
|
||||||
|
- Update Go modules
|
||||||
|
|
||||||
|
|
||||||
|
## [0.2.1]
|
||||||
|
|
||||||
|
- Update core modules
|
||||||
|
|
||||||
|
|
||||||
|
## [0.2.0]
|
||||||
|
|
||||||
|
- Add threaded option (`-t`) to use all CPU cores
|
||||||
|
|
||||||
|
|
||||||
|
## [0.1.0]
|
||||||
|
|
||||||
- Switch to go mods - go (>= 1.11 required)
|
- Switch to go mods - go (>= 1.11 required)
|
||||||
- Switch to axllent/semver for app updating
|
- Switch to axllent/semver for app updating
|
||||||
|
2
Makefile
2
Makefile
@ -20,4 +20,4 @@ release:
|
|||||||
$(call build,linux,arm)
|
$(call build,linux,arm)
|
||||||
$(call build,linux,arm64)
|
$(call build,linux,arm64)
|
||||||
$(call build,darwin,amd64)
|
$(call build,darwin,amd64)
|
||||||
$(call build,darwin,386)
|
$(call build,darwin,arm64)
|
||||||
|
@ -35,6 +35,7 @@ Options:
|
|||||||
-m, --max string downscale to a maximum width & height in pixels (<width>x<height>)
|
-m, --max string downscale to a maximum width & height in pixels (<width>x<height>)
|
||||||
-o, --out string output directory (default overwrites original)
|
-o, --out string output directory (default overwrites original)
|
||||||
-p, --preserve preserve file modification times (default true)
|
-p, --preserve preserve file modification times (default true)
|
||||||
|
-t, --threaded run multi-threaded (use all CPU cores)
|
||||||
-u, --update update to latest release
|
-u, --update update to latest release
|
||||||
-v, --version show version number
|
-v, --version show version number
|
||||||
-h, --help show help
|
-h, --help show help
|
||||||
@ -61,7 +62,7 @@ Download the appropriate binary from the [releases](https://github.com/axllent/g
|
|||||||
|
|
||||||
### Build requirements
|
### Build requirements
|
||||||
|
|
||||||
Go >= 1.11 required.
|
Go >= 1.21 required.
|
||||||
|
|
||||||
```
|
```
|
||||||
go get github.com/axllent/goptimize
|
go get github.com/axllent/goptimize
|
||||||
|
171
exifcopy.go
Normal file
171
exifcopy.go
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
// https://stackoverflow.com/a/76779756
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
soi = 0xD8
|
||||||
|
eoi = 0xD9
|
||||||
|
sos = 0xDA
|
||||||
|
exif = 0xE1
|
||||||
|
copyright = 0xEE
|
||||||
|
comment = 0xFE
|
||||||
|
)
|
||||||
|
|
||||||
|
func isMetaTagType(tagType byte) bool {
|
||||||
|
// Adapt as needed
|
||||||
|
return tagType == exif || tagType == copyright || tagType == comment
|
||||||
|
}
|
||||||
|
|
||||||
|
func copySegments(dst *bufio.Writer, src *bufio.Reader, filterSegment func(tagType byte) bool) error {
|
||||||
|
var buf [2]byte
|
||||||
|
_, err := io.ReadFull(src, buf[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if buf != [2]byte{0xFF, soi} {
|
||||||
|
return errors.New("expected SOI")
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
_, err := io.ReadFull(src, buf[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if buf[0] != 0xFF {
|
||||||
|
return errors.New("invalid tag type")
|
||||||
|
}
|
||||||
|
if buf[1] == eoi {
|
||||||
|
// Hacky way to check for EOF
|
||||||
|
_, err := src.Read(buf[:1])
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// don't return an error as some cameras add the exif data at the end.
|
||||||
|
// if n > 0 {
|
||||||
|
// return errors.New("EOF expected after EOI")
|
||||||
|
// }
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sos := buf[1] == 0xDA
|
||||||
|
filter := filterSegment(buf[1])
|
||||||
|
if filter {
|
||||||
|
_, err = dst.Write(buf[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.ReadFull(src, buf[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if filter {
|
||||||
|
_, err = dst.Write(buf[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: Includes the length, but not the tag, so subtract 2
|
||||||
|
tagLength := ((uint16(buf[0]) << 8) | uint16(buf[1])) - 2
|
||||||
|
if filter {
|
||||||
|
_, err = io.CopyN(dst, src, int64(tagLength))
|
||||||
|
} else {
|
||||||
|
_, err = src.Discard(int(tagLength))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if sos {
|
||||||
|
// Find next tag `FF xx` in the stream where `xx != 0` to skip ECS
|
||||||
|
// See https://stackoverflow.com/questions/2467137/parsing-jpeg-file-format-format-of-entropy-coded-segments-ecs
|
||||||
|
for {
|
||||||
|
bytes, err := src.Peek(2)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if bytes[0] == 0xFF {
|
||||||
|
data, rstMrk := bytes[1] == 0, bytes[1] >= 0xD0 && bytes[1] <= 0xD7
|
||||||
|
if !data && !rstMrk {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filter {
|
||||||
|
err = dst.WriteByte(bytes[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = src.Discard(1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyMetadata(outImagePath, imagePath, metadataImagePath string) error {
|
||||||
|
outFile, err := os.Create(outImagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer outFile.Close()
|
||||||
|
writer := bufio.NewWriter(outFile)
|
||||||
|
|
||||||
|
imageFile, err := os.Open(imagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer imageFile.Close()
|
||||||
|
imageReader := bufio.NewReader(imageFile)
|
||||||
|
|
||||||
|
metaFile, err := os.Open(metadataImagePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer metaFile.Close()
|
||||||
|
metaReader := bufio.NewReader(metaFile)
|
||||||
|
|
||||||
|
_, err = writer.Write([]byte{0xFF, soi})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy metadata segments
|
||||||
|
// It seems that they need to come first!
|
||||||
|
err = copySegments(writer, metaReader, isMetaTagType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Copy all non-metadata segments
|
||||||
|
err = copySegments(writer, imageReader, func(tagType byte) bool {
|
||||||
|
return !isMetaTagType(tagType)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = writer.Write([]byte{0xFF, eoi})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush the writer, otherwise the last couple buffered writes (including the EOI) won't get written!
|
||||||
|
return writer.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
func exifCopy(fromPath, toPath string) error {
|
||||||
|
copyPath := toPath + "~"
|
||||||
|
err := os.Rename(toPath, copyPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer os.Remove(copyPath)
|
||||||
|
return copyMetadata(toPath, copyPath, fromPath)
|
||||||
|
}
|
12
go.mod
12
go.mod
@ -1,10 +1,14 @@
|
|||||||
module github.com/axllent/goptimize
|
module github.com/axllent/goptimize
|
||||||
|
|
||||||
go 1.13
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.22.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/axllent/ghru v1.1.3
|
github.com/axllent/ghru v1.2.1
|
||||||
github.com/disintegration/imaging v1.6.1
|
github.com/kovidgoyal/imaging v1.6.3
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8
|
golang.org/x/image v0.18.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/axllent/semver v0.0.1 // indirect
|
||||||
|
18
go.sum
18
go.sum
@ -1,12 +1,10 @@
|
|||||||
github.com/axllent/ghru v1.1.3 h1:n0jYsuqCYaHHAR6DraXZl8hpBY4j0XV47y5Lyym/jGo=
|
github.com/axllent/ghru v1.2.1 h1:PuJQQeILJJ42O9nvjTwObWQGZDyZ9/F71st9jNAqgoU=
|
||||||
github.com/axllent/ghru v1.1.3/go.mod h1:rFvMhcO1UAv2Cv6bXscS8EOc7qqNpfe8ZLp23utzs88=
|
github.com/axllent/ghru v1.2.1/go.mod h1:YgznIILRJpnII5x8N080q/G8Milzk3sy9Sh4dV1eGQw=
|
||||||
github.com/axllent/semver v0.0.0-20191103011746-394cefa91ee9 h1:LHNcCfePzgC/agAJs5a/5K3hFo8uW04bEdDKDkoX4do=
|
github.com/axllent/semver v0.0.1 h1:QqF+KSGxgj8QZzSXAvKFqjGWE5792ksOnQhludToK8E=
|
||||||
github.com/axllent/semver v0.0.0-20191103011746-394cefa91ee9/go.mod h1:2xSPzvG8n9mRfdtxSvWvfTfQGWfHsMsHO1iZnKATMSc=
|
github.com/axllent/semver v0.0.1/go.mod h1:2xSPzvG8n9mRfdtxSvWvfTfQGWfHsMsHO1iZnKATMSc=
|
||||||
github.com/disintegration/imaging v1.6.1 h1:JnBbK6ECIZb1NsWIikP9pd8gIlTIRx7fuDNpU9fsxOE=
|
github.com/kovidgoyal/imaging v1.6.3 h1:iNPpv7ygiaB/NOztc6APMT7yr9UwBS+rOZwIbAdtyY8=
|
||||||
github.com/disintegration/imaging v1.6.1/go.mod h1:xuIt+sRxDFrHS0drzXUlCJthkJ8k7lkkUojDSR247MQ=
|
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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
|
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
|
28
goptimize.go
28
goptimize.go
@ -2,17 +2,17 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/disintegration/imaging"
|
"github.com/kovidgoyal/imaging"
|
||||||
"golang.org/x/image/bmp"
|
"golang.org/x/image/bmp"
|
||||||
"golang.org/x/image/tiff"
|
"golang.org/x/image/tiff"
|
||||||
)
|
)
|
||||||
@ -31,13 +31,22 @@ func Goptimize(file string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// open original, rotate if necessary
|
var src image.Image
|
||||||
src, err := imaging.Open(file, imaging.AutoOrientation(true))
|
|
||||||
|
|
||||||
|
if !copyExif {
|
||||||
|
// rotate if necessary
|
||||||
|
src, err = imaging.Open(file, imaging.AutoOrientation(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %v (%s)\n", err, file)
|
fmt.Printf("Error: %v (%s)\n", err, file)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
src, err = imaging.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %v (%s)\n", err, file)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
format, err := imaging.FormatFromFilename(file)
|
format, err := imaging.FormatFromFilename(file)
|
||||||
|
|
||||||
@ -83,7 +92,7 @@ func Goptimize(file string) {
|
|||||||
resultW := dstBounds.Dx()
|
resultW := dstBounds.Dx()
|
||||||
resultH := dstBounds.Dy()
|
resultH := dstBounds.Dy()
|
||||||
|
|
||||||
tmpFile, err := ioutil.TempFile(os.TempDir(), "Goptimized-")
|
tmpFile, err := os.CreateTemp(os.TempDir(), "Goptimized-")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: cannot create temporary file: %v\n", err)
|
fmt.Printf("Error: cannot create temporary file: %v\n", err)
|
||||||
@ -128,6 +137,13 @@ func Goptimize(file string) {
|
|||||||
} else if jpegoptim != "" {
|
} else if jpegoptim != "" {
|
||||||
RunOptimizer(tmpFilename, false, jpegoptim, "-f", "-s", "-o")
|
RunOptimizer(tmpFilename, false, jpegoptim, "-f", "-s", "-o")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if copyExif {
|
||||||
|
if err := exifCopy(file, tmpFilename); err != nil {
|
||||||
|
fmt.Printf("Error copying exif data: %v (%s)\n", err, file)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if format.String() == "PNG" {
|
} else if format.String() == "PNG" {
|
||||||
if pngquant != "" {
|
if pngquant != "" {
|
||||||
RunOptimizer(tmpFilename, true, pngquant, "-f", "--output")
|
RunOptimizer(tmpFilename, true, pngquant, "-f", "--output")
|
||||||
@ -227,7 +243,7 @@ func Goptimize(file string) {
|
|||||||
// and overwrite it if the output is smaller than the original
|
// and overwrite it if the output is smaller than the original
|
||||||
func RunOptimizer(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 := os.CreateTemp(os.TempDir(), "Goptimized-")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Cannot create temporary file: %v\n", err)
|
fmt.Printf("Cannot create temporary file: %v\n", err)
|
||||||
|
53
main.go
53
main.go
@ -5,7 +5,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/axllent/ghru"
|
"github.com/axllent/ghru"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -22,6 +24,8 @@ var (
|
|||||||
optipng string
|
optipng string
|
||||||
pngquant string
|
pngquant string
|
||||||
gifsicle string
|
gifsicle string
|
||||||
|
copyExif bool
|
||||||
|
threads = 1
|
||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,22 +46,24 @@ func main() {
|
|||||||
fmt.Printf(" %s -o out/ -q 90 -m 1600x1600 *.jpg\n", os.Args[0])
|
fmt.Printf(" %s -o out/ -q 90 -m 1600x1600 *.jpg\n", os.Args[0])
|
||||||
|
|
||||||
fmt.Println("\nDetected optimizers:")
|
fmt.Println("\nDetected optimizers:")
|
||||||
if err := displayDelectedOptimizer("jpegtran ", jpegtran); err != nil {
|
if err := displayDetectedOptimizer("jpegtran ", jpegtran); err != nil {
|
||||||
displayDelectedOptimizer("jpegoptim", jpegoptim)
|
displayDetectedOptimizer("jpegoptim", jpegoptim)
|
||||||
}
|
}
|
||||||
displayDelectedOptimizer("optipng ", optipng)
|
displayDetectedOptimizer("optipng ", optipng)
|
||||||
displayDelectedOptimizer("pngquant ", pngquant)
|
displayDetectedOptimizer("pngquant ", pngquant)
|
||||||
displayDelectedOptimizer("gifsicle ", gifsicle)
|
displayDetectedOptimizer("gifsicle ", gifsicle)
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxSizes string
|
var maxSizes string
|
||||||
var update, showversion, showhelp bool
|
var multiThreaded, update, showversion, showhelp bool
|
||||||
|
|
||||||
flag.IntVarP(&quality, "quality", "q", 75, "quality, JPEG only")
|
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(&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.StringVarP(&outputDir, "out", "o", "", "output directory (default overwrites original)")
|
||||||
flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times")
|
flag.BoolVarP(&preserveModTimes, "preserve", "p", true, "preserve file modification times")
|
||||||
|
flag.BoolVarP(©Exif, "exif", "e", false, "copy exif data")
|
||||||
flag.BoolVarP(&update, "update", "u", false, "update to latest release")
|
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(&showversion, "version", "v", false, "show version number")
|
||||||
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
|
flag.BoolVarP(&showhelp, "help", "h", false, "show help")
|
||||||
|
|
||||||
@ -137,13 +143,38 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range args {
|
if multiThreaded {
|
||||||
Goptimize(img)
|
threads = runtime.NumCPU()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// displayDelectedOptimizer prints whether the optimizer was found
|
processChan := make(chan string)
|
||||||
func displayDelectedOptimizer(name, bin string) error {
|
|
||||||
|
wg := &sync.WaitGroup{} // Signal to main goroutine that worker goroutines are working/done working
|
||||||
|
wg.Add(threads)
|
||||||
|
|
||||||
|
for i := 0; i < threads; i++ {
|
||||||
|
go func() {
|
||||||
|
for nextFile := range processChan {
|
||||||
|
Goptimize(nextFile)
|
||||||
|
}
|
||||||
|
// Channel was closed, so we finished this goroutine.
|
||||||
|
wg.Done() // Let main goroutine know we are done.
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, img := range args {
|
||||||
|
processChan <- img
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the channel. This tells the worker goroutines that they can be done.
|
||||||
|
close(processChan)
|
||||||
|
|
||||||
|
// Wait for all worker goroutines to finish processing the IPs
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// displayDetectedOptimizer prints whether the optimizer was found
|
||||||
|
func displayDetectedOptimizer(name, bin string) error {
|
||||||
exe, err := exec.LookPath(bin)
|
exe, err := exec.LookPath(bin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user