Skip to content

Commit 42f6148

Browse files
committed
fix PNG.Check() logic
1 parent acdc7f1 commit 42f6148

6 files changed

Lines changed: 25 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
- v2.0.4
3535
- update go-helper/v2
3636
- update root.go
37+
- v2.0.5
38+
- fix PNG.Check() logic

cmd/root.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,24 @@ var rootCmd = &cobra.Command{
5151
if len(args) < 2 {
5252
errs.Queue("", errors.New("Input/Output file missing"))
5353
}
54-
55-
// --- Pre-checking
56-
57-
var icoFile string
58-
if errs.IsEmpty() {
59-
// Make sure the last file in arg list is *NOT* PNG, to prevent accidental overwriting a PNG file
54+
},
55+
Run: func(cmd *cobra.Command, args []string) {
56+
var (
57+
ico *p2i.ICO
6058
icoFile = args[len(args)-1]
61-
png := new(p2i.PNG).New().Read(icoFile)
59+
png *p2i.PNG
60+
pngNum = len(args) - 1
61+
)
62+
if errs.IsEmpty() {
63+
// check if output file is PNG
64+
png = new(p2i.PNG).New().Read(icoFile)
6265
if png.Err == nil && png.IsPNG() {
6366
errs.Queue("", errors.New(png.File+": is PNG"))
6467
} else {
6568
// Clear the errs queue, as the icoFile may not exist yet and generated a read error
6669
errs.Clear()
6770
}
6871
}
69-
70-
},
71-
Run: func(cmd *cobra.Command, args []string) {
72-
var (
73-
ico *p2i.ICO
74-
icoFile = args[len(args)-1]
75-
pngNum = len(args) - 1
76-
)
7772
if errs.IsEmpty() {
7873
ico = new(p2i.ICO).New(icoFile)
7974
for i := range pngNum {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module github.com/J-Siu/go-png2ico/v2
22

3-
go 1.25.1
3+
go 1.25.5
44

55
require (
6-
github.com/J-Siu/go-helper/v2 v2.6.5
6+
github.com/J-Siu/go-helper/v2 v2.7.1
77
github.com/spf13/cobra v1.10.2
88
)
99

1010
require (
1111
github.com/charlievieth/strcase v0.0.5 // indirect
1212
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1313
github.com/spf13/pflag v1.0.10 // indirect
14-
golang.org/x/sys v0.39.0 // indirect
14+
golang.org/x/sys v0.40.0 // indirect
1515
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/J-Siu/go-helper/v2 v2.6.5 h1:EVTjsBacgeT0CUvsCXGSxWgZGLo+5kvJz771HiGG7MM=
2-
github.com/J-Siu/go-helper/v2 v2.6.5/go.mod h1:xMmpBESfx/Z5eab1bTNgu6YpEOvBZUorjNfLdGu4fN4=
1+
github.com/J-Siu/go-helper/v2 v2.7.1 h1:fq6e/5xfUGxh/Y2gbnzwWUGzZjl5x7YsdgcQmrx4nwc=
2+
github.com/J-Siu/go-helper/v2 v2.7.1/go.mod h1:8cC+sNocdV2FB7gJJ5pGiwG+tnlkoepFEsu4wyp6Its=
33
github.com/charlievieth/strcase v0.0.5 h1:gV4iXVyD6eI5KdfOV+/vIVCKXZwtCWOmDMcu7Uy00Rs=
44
github.com/charlievieth/strcase v0.0.5/go.mod h1:FIOYY1aDBMSIOFqmVomHBpoK+bteGlESRsgsdWjrhx8=
55
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
@@ -12,6 +12,6 @@ github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
1212
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
1313
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1414
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
15-
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
16-
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
15+
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
16+
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
1717
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

p2i/png.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,14 @@ func (t *PNG) Check() *PNG {
8080

8181
// CHECK 1: 8byte header[0:8] - magic number
8282
magic := []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}
83-
if bytes.Equal(magic[:], t.Buf[:8]) {
83+
t.isPNG = bytes.Equal(magic[:], t.Buf[:8])
84+
if t.isPNG {
8485
ezlog.Debug().N(prefix).M("Found PNG magic").Out()
85-
t.isPNG = true
86-
}
87-
88-
// CHECK 2: 4byte header[12:16] - chunk type IHDR
89-
if t.isPNG && bytes.Equal([]byte("IHDR"), t.Buf[12:16]) {
90-
ezlog.Debug().N(prefix).M("Found IHDR chunk").Out()
91-
t.isPNG = true
86+
// CHECK 2: 4byte header[12:16] - chunk type IHDR
87+
t.isPNG = t.isPNG && bytes.Equal([]byte("IHDR"), t.Buf[12:16])
9288
}
93-
9489
if t.isPNG {
90+
ezlog.Debug().N(prefix).M("Found IHDR chunk").Out()
9591
t.info()
9692
}
9793

p2i/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ THE SOFTWARE.
2323
package p2i
2424

2525
const (
26-
Version = "v2.0.4"
26+
Version = "v2.0.5"
2727
)

0 commit comments

Comments
 (0)