Skip to content

Commit e739313

Browse files
committed
use stdin/stdout
for pipe in/out data
1 parent 3ed66ce commit e739313

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.idea/
2-
bin/
3-
go.sum
2+
.vscode/
3+
dist/
4+
*.ico
5+
*.png
6+
*.syso

Makefile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
build:
2-
go mod tidy
1+
build: tidy
32
go build -ldflags "-s -w" -o bin/ .
43

4+
run: tidy
5+
go run main.go
6+
7+
compile: tidy icon
8+
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o dist/iconize-linux-amd64 .
9+
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o dist/iconize-windows-amd64.exe .
10+
GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o dist/iconize-macos-amd64 .
11+
GOOS=freebsd GOARCH=amd64 go build -ldflags "-s -w" -o dist/iconize-freebsd-amd64 .
12+
13+
tidy:
14+
go mod tidy
15+
516
icon:
617
go install github.com/akavel/rsrc@latest
718
rsrc -ico go.ico
819

9-
run:
10-
go mod tidy
11-
go run .
12-
13-
windows: icon build
20+
loc:
21+
go install github.com/boyter/scc/v3@latest
22+
scc --exclude-dir vendor --exclude-dir bin .

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.19
55
require (
66
github.com/spf13/pflag v1.0.5
77
github.com/typomedia/ico v1.0.0-beta.4
8-
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
8+
golang.org/x/image v0.1.0
99
)

main.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,49 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/spf13/pflag"
7+
"github.com/typomedia/ico"
8+
"golang.org/x/image/draw"
69
"image"
710
"image/color"
11+
"io"
812
"log"
913
"os"
1014
"path/filepath"
11-
"time"
12-
13-
"github.com/spf13/pflag"
14-
"github.com/typomedia/ico"
15-
"golang.org/x/image/draw"
1615
)
1716

1817
func main() {
1918
var (
20-
input string
21-
output string
19+
input string
20+
data []byte
21+
err error
2222
)
2323

24-
pflag.StringVarP(&output, "out", "o", "", "output ico file")
24+
outfile := pflag.StringP("out", "o", "", "outfile ico file")
25+
version := pflag.BoolP("version", "V", false, "show version")
2526
pflag.Parse()
2627

27-
if len(pflag.Args()) < 1 {
28-
os.Exit(1)
28+
if *version {
29+
fmt.Println("iconize 0.2.0 <[email protected]>")
30+
os.Exit(0)
2931
}
3032

3133
input = pflag.Arg(0)
32-
fmt.Println("Input:", input)
33-
34-
start := time.Now()
35-
36-
file, err := os.ReadFile(input)
34+
stat, _ := os.Stdin.Stat()
35+
36+
switch true {
37+
case input != "":
38+
data, err = os.ReadFile(input)
39+
case (stat.Mode() & os.ModeCharDevice) == 0:
40+
data, err = io.ReadAll(os.Stdin)
41+
default:
42+
log.Fatal("no input data given")
43+
}
3744
if err != nil {
3845
log.Fatal(err)
3946
}
4047

41-
img, _, err := image.Decode(bytes.NewReader(file))
48+
img, _, err := image.Decode(bytes.NewReader(data))
4249
if err != nil {
4350
log.Fatal(err)
4451
}
@@ -48,7 +55,7 @@ func main() {
4855

4956
alpha := image.NewRGBA(image.Rect(0, 0, size, size))
5057

51-
rgba := color.RGBA{0, 0, 0, 0}
58+
rgba := color.RGBA{}
5259
draw.Draw(alpha, alpha.Bounds(), &image.Uniform{C: rgba}, image.Point{}, draw.Src)
5360

5461
draw.Draw(alpha, img.Bounds().Add(
@@ -57,18 +64,6 @@ func main() {
5764
Y: (size - img.Bounds().Dy()) / 2,
5865
}), img, image.Point{}, draw.Over)
5966

60-
basename := name(input) + ".ico"
61-
if output != "" {
62-
basename = output
63-
}
64-
fmt.Println("Output:", basename)
65-
66-
icoFile, err := os.Create(basename)
67-
if err != nil {
68-
log.Fatal(err)
69-
}
70-
defer icoFile.Close()
71-
7267
icon := ico.NewIcon()
7368
// https://learn.microsoft.com/en-us/windows/win32/uxguide/vis-icons
7469
sizes := []int{256, 128, 64, 48, 32, 24, 16}
@@ -77,15 +72,24 @@ func main() {
7772
icon.AddPng(resizedImg)
7873
}
7974

80-
t := time.Now()
81-
elapsed := t.Sub(start)
82-
fmt.Println("Elapsed:", elapsed)
83-
8475
enc, err := icon.Encode()
8576
if err != nil {
8677
log.Fatal(err)
8778
}
8879

80+
// write to stdout
81+
if *outfile == "" {
82+
fmt.Println(string(enc))
83+
os.Exit(0)
84+
}
85+
86+
// write to file
87+
icoFile, err := os.Create(*outfile)
88+
if err != nil {
89+
log.Fatal(err)
90+
}
91+
defer icoFile.Close()
92+
8993
icoFile.Write(enc)
9094
os.Exit(0)
9195

0 commit comments

Comments
 (0)