commit 1bc682f12cfdddaf33a639c218ffd361c95e33db
parent 5fdb3ed2de4345cf12601fc28b0f5badff095f80
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Tue, 30 Jan 2018 17:30:59 +0100
[+] Enable unix piping.
Diffstat:
| M | cmd/main.go | | | 58 | ++++++++++++++++++++++++++++++++++------------------------ |
| A | cmd/pipe.go | | | 21 | +++++++++++++++++++++ |
2 files changed, 55 insertions(+), 24 deletions(-)
diff --git a/cmd/main.go b/cmd/main.go
@@ -2,6 +2,7 @@ package main
import (
"image"
+ "io"
"log"
"os"
"path/filepath"
@@ -13,35 +14,48 @@ import (
"github.com/spf13/pflag"
)
-var fs = afero.NewOsFs()
+var (
+ fs = afero.NewOsFs()
+ piping bool
+ input io.Reader
+ output io.Writer
+)
func main() {
var (
- input = pflag.StringP("input", "i", "", "Input image for conversion to icns (jpg|png)")
- output = pflag.StringP("output", "o", "", "Output path, defaults to <path/to/image>.icns")
- resize = pflag.IntP("resize", "r", 5, "Quality of resize algorithm. Values range from 0 to 5, fastest to highest quality. Defaults to highest quality.")
+ inputPath = pflag.StringP("input", "i", "", "Input image for conversion to icns (jpg|png)")
+ outputPath = pflag.StringP("output", "o", "", "Output path, defaults to <path/to/image>.icns")
+ resize = pflag.IntP("resize", "r", 5, "Quality of resize algorithm. Values range from 0 to 5, fastest to slowest execution time. Defaults to slowest for best quality.")
)
pflag.Parse()
- in, out, algorithm := sanitiseInputs(*input, *output, *resize)
- sourcef, err := fs.Open(in)
- if err != nil {
- log.Fatalf("opening source image: %v", err)
+ in, out, algorithm := sanitiseInputs(*inputPath, *outputPath, *resize)
+ if !piping {
+ if in == "" {
+ usage()
+ os.Exit(0)
+ }
+ sourcef, err := fs.Open(in)
+ if err != nil {
+ log.Fatalf("opening source image: %v", err)
+ }
+ defer sourcef.Close()
+ input = sourcef
+ if err := fs.MkdirAll(filepath.Dir(out), 0755); err != nil {
+ log.Fatalf("preparing output directory: %v", err)
+ }
+ outputf, err := fs.Create(out)
+ if err != nil {
+ log.Fatalf("creating icns file: %v", err)
+ }
+ defer outputf.Close()
+ output = outputf
}
- defer sourcef.Close()
- img, _, err := image.Decode(sourcef)
+ img, _, err := image.Decode(input)
if err != nil {
log.Fatalf("decoding image: %v", err)
}
- if err := fs.MkdirAll(filepath.Dir(out), 0755); err != nil {
- log.Fatalf("preparing output directory: %v", err)
- }
- outputf, err := fs.Create(out)
- if err != nil {
- log.Fatalf("creating icns file: %v", err)
- }
- defer outputf.Close()
if err := icns.EncodeWithInterpolationFunction(
- outputf,
+ output,
img,
algorithm,
); err != nil {
@@ -54,11 +68,7 @@ func sanitiseInputs(
outputPath string,
resize int,
) (string, string, icns.InterpolationFunction) {
- if inputPath == "" {
- pflag.Usage()
- os.Exit(0)
- }
- if outputPath == "" {
+ if inputPath != "" && outputPath == "" {
outputPath = changeExtensionTo(inputPath, "icns")
}
if filepath.Ext(outputPath) == "" {
diff --git a/cmd/pipe.go b/cmd/pipe.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+ "fmt"
+ "os"
+)
+
+func init() {
+ info, err := os.Stdin.Stat()
+ if err != nil {
+ panic(fmt.Sprintf("getting info on stdin file descriptor: %v", err))
+ }
+ if (info.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
+ return
+ }
+ if info.Size() > 0 {
+ piping = true
+ input = os.Stdin
+ output = os.Stdout
+ }
+}