Skip to content

Commit 883a680

Browse files
committed
Update go-helper/v2
Update `processFile` flow
1 parent c83f352 commit 883a680

6 files changed

Lines changed: 84 additions & 98 deletions

File tree

CHANGELOG.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
- v1.0.7
2+
- Fix Github workflows
3+
- v1.0.8
4+
- Upgrade go-helper to v1.1.8
5+
- Fix config logic
6+
- Fix debug logic
7+
- Fix receiver name
8+
- v1.0.9
9+
- Upgrade go-helper to v1.1.10
10+
- Add command line version
11+
- Move -dryRun from base to "upgrade" command
12+
- Move default config file `~/.go-dotfile.json` -> `~/.config/go-dotfile.json`
13+
- v1.0.10
14+
- Fix TypeConf.setDefault overwrite command line config file option
15+
- conf.go
16+
- check DirDest exist
17+
- dotfile.go
18+
- Init() - Chdir() error check
19+
- Process() - queue error
20+
- Change some function to local
21+
- root.go
22+
- Print error queue
23+
- v1.0.11
24+
- Fix version
25+
- v1.1.0
26+
- Skip copy if source and destination files have same modification time and size
27+
- Copy source file modification time to destination file
28+
- v1.1.1
29+
- TypeDotfile
30+
- fix MyType mismatch
31+
- fileChanged
32+
- fix logical err: should ignore destination file stat() err
33+
- v1.1.2
34+
- Update to `go-helper/v2`
35+
- v1.1.3
36+
- Update `go-helper/v2`
37+
- v1.1.4
38+
- Move to `go-helper/basestruct`
39+
- v1.1.5
40+
- Update `go-helper/v2`
41+
- use property for `New()`
42+
- `Dirs` and `Files` calculation moved to `Run()`
43+
- v1.1.6
44+
- Update `go-helper/v2`
45+
- Update `processFile` flow

README.md

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Simple dotfile management command line.
77
- [Build](#build)
88
- [Configuration](#configuration)
99
- [Testing](#testing)
10-
- [Change Log](#change-log)
1110
- [License](#license)
1211

1312
<!--more-->
@@ -91,51 +90,6 @@ go-dotfile -c examples/go-dotfile.sample.json
9190
ls -a $HOME/tmp
9291
```
9392

94-
### Change Log
95-
96-
- v1.0.7
97-
- Fix Github workflows
98-
- v1.0.8
99-
- Upgrade go-helper to v1.1.8
100-
- Fix config logic
101-
- Fix debug logic
102-
- Fix receiver name
103-
- v1.0.9
104-
- Upgrade go-helper to v1.1.10
105-
- Add command line version
106-
- Move -dryRun from base to "upgrade" command
107-
- Move default config file `~/.go-dotfile.json` -> `~/.config/go-dotfile.json`
108-
- v1.0.10
109-
- Fix TypeConf.setDefault overwrite command line config file option
110-
- conf.go
111-
- check DirDest exist
112-
- dotfile.go
113-
- Init() - Chdir() error check
114-
- Process() - queue error
115-
- Change some function to local
116-
- root.go
117-
- Print error queue
118-
- v1.0.11
119-
- Fix version
120-
- v1.1.0
121-
- Skip copy if source and destination files have same modification time and size
122-
- Copy source file modification time to destination file
123-
- v1.1.1
124-
- TypeDotfile
125-
- fix MyType mismatch
126-
- fileChanged
127-
- fix logical err: should ignore destination file stat() err
128-
- v1.1.2
129-
- Update to `go-helper/v2`
130-
- v1.1.3
131-
- Update `go-helper/v2`
132-
- v1.1.4
133-
- Move to `go-helper/basestruct`
134-
- v1.1.5
135-
- Update `go-helper/v2`
136-
- use property for `New()`
137-
- `Dirs` and `Files` calculation moved to `Run()`
138-
13993
### License
14094

14195
The MIT License (MIT)

global/version.go

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

2525
const (
26-
Version = "v1.1.5"
26+
Version = "v1.1.6"
2727
)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/J-Siu/go-dotfile
33
go 1.25.1
44

55
require (
6-
github.com/J-Siu/go-helper/v2 v2.5.2
6+
github.com/J-Siu/go-helper/v2 v2.5.4
77
github.com/edwardrf/symwalk v0.1.0
88
github.com/spf13/cobra v1.10.1
99
github.com/spf13/viper v1.21.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/J-Siu/go-helper/v2 v2.5.2 h1:+YapLpQMLOd4KpftcvSrHYUB096c34FT/biY6BiKG5I=
2-
github.com/J-Siu/go-helper/v2 v2.5.2/go.mod h1:yjoN0uaZ75FX8xKGMZ2WpY+OwQ074cTdIolarsBH9co=
1+
github.com/J-Siu/go-helper/v2 v2.5.4 h1:r03CZ6b5CPPiPiUXW4mr6NYSMqKL+N1EDfELD7it2oI=
2+
github.com/J-Siu/go-helper/v2 v2.5.4/go.mod h1:yjoN0uaZ75FX8xKGMZ2WpY+OwQ074cTdIolarsBH9co=
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=

lib/dotfile.go

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"os"
2828
"path"
2929
"strings"
30+
"time"
3031

3132
"github.com/J-Siu/go-helper/v2/basestruct"
3233
"github.com/J-Siu/go-helper/v2/errs"
@@ -39,8 +40,8 @@ import (
3940
type FileProcMode int8
4041

4142
const (
42-
APPEND FileProcMode = iota // Processing mode for TypeDotfile.Mode
43-
COPY // Processing mode for TypeDotfile.Mode
43+
APPEND FileProcMode = iota
44+
COPY
4445
SKIP
4546
)
4647

@@ -106,72 +107,58 @@ func (t *TypeDotfile) Run() {
106107
//
107108
// Not using TypeDotfile.Err
108109
func (t *TypeDotfile) processFile(src, dest string) (err error) {
109-
prefix := t.MyType + ".processFile"
110+
// prefix := t.MyType + ".processFile"
110111

111-
fileProcMode := t.Mode
112-
filePermStr := ".........."
112+
var (
113+
data []byte
114+
filePermStr = ".........."
115+
fileProcMode = t.Mode
116+
srcInfo os.FileInfo
117+
srcModTime time.Time
118+
srcPermission os.FileMode
119+
)
113120

114-
// Destination FileMode
115-
destFlag := os.O_CREATE | os.O_WRONLY
116-
if fileProcMode == APPEND {
117-
destFlag |= os.O_APPEND
118-
}
119-
if fileProcMode == COPY {
120-
destFlag |= os.O_TRUNC
121-
same := file.FileSame(src, dest)
122-
if same {
123-
fileProcMode = SKIP
124-
}
121+
if fileProcMode == COPY && file.FileSame(src, dest) {
122+
fileProcMode = SKIP
125123
}
126124

127125
if fileProcMode != SKIP {
128-
srcInfo, err := os.Stat(src)
129-
if err != nil {
130-
return err
126+
srcInfo, err = os.Stat(src)
127+
if err == nil {
128+
srcPermission = srcInfo.Mode()
129+
srcModTime = srcInfo.ModTime()
131130
}
132-
srcPermission := srcInfo.Mode()
133-
srcModTime := srcInfo.ModTime()
134131

135132
// Read source file
136-
data, err := os.ReadFile(src)
137-
if err != nil {
138-
return err
139-
}
140-
141-
// Open destination file
142-
f, err := os.OpenFile(dest, destFlag, srcPermission)
143-
if err != nil {
144-
return err
133+
if err == nil {
134+
data, err = os.ReadFile(src)
145135
}
146136

147137
// Append: add newline to destination file
148-
if fileProcMode == APPEND {
149-
_, err = f.Write([]byte("\n"))
150-
if err != nil {
151-
return err
138+
if err == nil {
139+
if fileProcMode == APPEND {
140+
b := []byte("\n")
141+
err = file.AppendByte(dest, &b)
142+
if err == nil {
143+
err = file.AppendByte(dest, &data)
144+
}
145+
} else {
146+
err = file.WriteByte(dest, &data, srcPermission)
152147
}
153148
}
154149

155-
// Append source content to destination file
156-
_, err = f.Write(data)
157-
if err != nil {
158-
return err
159-
}
160-
161-
f.Close()
162-
163150
// Set dest permission
164-
if fileProcMode == COPY {
151+
if err == nil && fileProcMode == COPY {
165152
os.Chtimes(dest, srcModTime, srcModTime)
166153
filePermStr = srcPermission.String()
167154
}
168155
}
169156

170-
str := fmt.Sprintf("%-6s %s %s -> %s", fileProcMode.String(), filePermStr, src, dest)
171-
if ezlog.GetLogLevel() >= ezlog.DEBUG {
172-
ezlog.Debug().N(prefix).M(str).Out()
173-
} else if t.Verbose {
174-
ezlog.Log().M(str).Out()
157+
if err == nil {
158+
if ezlog.GetLogLevel() >= ezlog.DEBUG || t.Verbose {
159+
str := fmt.Sprintf("%-6s %s %s -> %s", fileProcMode.String(), filePermStr, src, dest)
160+
ezlog.Log().M(str).Out()
161+
}
175162
}
176163

177164
return err

0 commit comments

Comments
 (0)