Skip to content

Commit 5c7a179

Browse files
committed
fix 'golint' and 'go vet' warnings
1 parent e22482a commit 5c7a179

File tree

13 files changed

+120
-72
lines changed

13 files changed

+120
-72
lines changed

bindata.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

diff.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
67
"github.com/sergi/go-diff/diffmatchpatch"
78
)
89

10+
// DeltaType classifies a patch action like 'delete', 'equal' or 'insert'
911
type DeltaType int
1012

1113
const (
14+
// Del represents a deleted patch chunk
1215
Del DeltaType = (iota - 1)
16+
// Eq represents a not changed patch chunk
1317
Eq
18+
// Ins represents a added patch chunk
1419
Ins
1520
)
1621

22+
// Delta represents a patch chunk
1723
type Delta struct {
1824
Type DeltaType
1925
LineNrFrom int
@@ -38,6 +44,7 @@ func (d *Delta) String() string {
3844
return fmt.Sprintf("{%s:%d,%d:%d,%d:%s}", t, d.LineNrFrom, d.LineNrTarget, d.StartPosFrom, d.StartPosTarget, d.Text)
3945
}
4046

47+
// Deltas represents all patch chunks of a file
4148
type Deltas []Delta
4249

4350
func (deltas Deltas) String() string {
@@ -51,6 +58,7 @@ func (deltas Deltas) String() string {
5158
return buffer.String()
5259
}
5360

61+
// Diff compares two strings and returns the DiffResult
5462
func Diff(from, target string, contextSize int) DiffResult {
5563

5664
// init diff-match-patch and create the (diff-match-patch) diff
@@ -92,7 +100,7 @@ func createDeltasFromDiffs(diffs []diffmatchpatch.Diff, contextSize int) Deltas
92100
nextDiffIfTypeIs := func(diffType diffmatchpatch.Operation) (diffmatchpatch.Diff, bool) {
93101
if idx < len(diffs) && diffs[idx].Type == diffType {
94102
next := diffs[idx]
95-
idx += 1
103+
idx++
96104
return next, true
97105
}
98106
return diffmatchpatch.Diff{}, false

diff_result.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
)
99

10+
// DiffResult represents the result from a diff
1011
type DiffResult struct {
1112
LineBasedDeltas Deltas
1213
CharBasedDeltas Deltas
@@ -35,10 +36,12 @@ func splitDeltasByContext(deltas Deltas) []Deltas {
3536
return splitted
3637
}
3738

39+
// DeltasByContext splits the Delta at their context bounds
3840
func (dr *DiffResult) DeltasByContext() []Deltas {
3941
return splitDeltasByContext(dr.LineBasedDeltas)
4042
}
4143

44+
// AsSideBySideHTML creates a SideBySide Diff
4245
func (dr *DiffResult) AsSideBySideHTML() []string {
4346
var htmlBlocks []string
4447

@@ -63,7 +66,7 @@ func (dr *DiffResult) AsSideBySideHTML() []string {
6366
nextDeltaIfTypeIs := func(deltaType DeltaType) (Delta, bool) {
6467
if deltaIdx < len(deltas) && deltas[deltaIdx].Type == deltaType {
6568
next := deltas[deltaIdx]
66-
deltaIdx += 1
69+
deltaIdx++
6770
return next, true
6871
}
6972
return Delta{}, false
@@ -125,6 +128,7 @@ func (dr *DiffResult) AsSideBySideHTML() []string {
125128
return htmlBlocks
126129
}
127130

131+
// AsIntextHTML creates an Inline Diff
128132
func (dr *DiffResult) AsIntextHTML() []string {
129133
var htmlBlocks []string
130134

file_handle.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ func NewFileHandleInSnapshot(path, snapName string) (*FileHandle, error) {
3333
return newFileHandle(ds.ConvertToSnapPath(path, snapName))
3434
}
3535

36+
// NewFileHandleInSnapshotPart is the flipped, partially applied version of 'NewFileHandleInSnapshot'
37+
func NewFileHandleInSnapshotPart(snapName string) func(string) (*FileHandle, error) {
38+
return func(path string) (*FileHandle, error) {
39+
return NewFileHandleInSnapshot(path, snapName)
40+
}
41+
}
42+
3643
func newFileHandle(path string) (*FileHandle, error) {
3744
fi, err := os.Stat(path)
3845
if err != nil {
@@ -161,13 +168,12 @@ func (fh *FileHandle) CopyAs(path string) (err error) {
161168
// * deleted entries are inserted
162169
// * inserted entries are removed
163170
func (fh *FileHandle) Patch(deltas Deltas) error {
164-
var err error
165171

166172
// verify the equal parts from the deltas are the same as in the given file
167173
// returns a error if not
168174
verifyDeltasAreApplicable := func() error {
169-
var f *os.File
170-
if f, err = os.Open(fh.Path); err != nil {
175+
f, err := os.Open(fh.Path)
176+
if err != nil {
171177
return fmt.Errorf("open file: '%s' - %s", fh.Name, err.Error())
172178
}
173179
defer f.Close()
@@ -190,14 +196,15 @@ func (fh *FileHandle) Patch(deltas Deltas) error {
190196

191197
// apply the deltas to a given file
192198
applyDeltasTo := func(dstPath string) error {
193-
var src, dst *os.File
194199
// open src / dst
195-
if src, err = os.Open(fh.Path); err != nil {
200+
src, err := os.Open(fh.Path)
201+
if err != nil {
196202
return fmt.Errorf("unable to open src-file: '%s' - %s", fh.Path, err.Error())
197203
}
198204
defer src.Close()
199205

200-
if dst, err = os.Create(dstPath); err != nil {
206+
dst, err := os.Create(dstPath)
207+
if err != nil {
201208
return fmt.Errorf("unable to open dst-file: '%s' - %s", dstPath, err.Error())
202209
}
203210
defer func() {
@@ -267,12 +274,13 @@ func (fh *FileHandle) Patch(deltas Deltas) error {
267274
}
268275

269276
if err := os.Rename(patchWorkFilePath, fh.Path); err != nil {
270-
return fmt.Errorf("unable to rename patch file to orginal file - %s", err.Error())
277+
return fmt.Errorf("unable to rename patch file to original file - %s", err.Error())
271278
}
272279

273280
return nil
274281
}
275282

283+
// MoveToBackup moves the file in the backup location
276284
func (fh *FileHandle) MoveToBackup() error {
277285
backupDir := fmt.Sprintf("%s/.zsd", filepath.Dir(fh.Path))
278286

@@ -296,7 +304,7 @@ func (fh *FileHandle) MoveToBackup() error {
296304
return os.Rename(fh.Path, backupFilePath)
297305
}
298306

299-
// FileHasChangedFunGen to create a FileHasChangedFunc
307+
// FileHasChangedFuncGen to create a FileHasChangedFunc
300308
type FileHasChangedFuncGen func(*FileHandle) FileHasChangedFunc
301309

302310
// FileHasChangedFunc to detect if a file has changed
@@ -329,7 +337,7 @@ func NewFileHasChangedFuncGenByName(method string) (FileHasChangedFuncGen, error
329337
return CompareFileBySizeAndModTime(actual)
330338
}, nil
331339
default:
332-
return nil, fmt.Errorf("no such compare method: '%s' - avaliable: 'size+modTime', 'size', 'md5'", method)
340+
return nil, fmt.Errorf("no such compare method: '%s' - available: 'size+modTime', 'size', 'md5'", method)
333341
}
334342
}
335343

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func main() {
6868

6969
// abort if zfs name is missing
7070
if len(zfsName) == 0 {
71-
fmt.Println("parameter <ZFS_NAME> missing\n")
71+
fmt.Println("parameter <ZFS_NAME> missing")
72+
fmt.Println()
7273
flag.Usage()
7374
os.Exit(1)
7475
}

params.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type params map[string]interface{}
1616

1717
// parse and validate request parameters
1818
// * rules format: [?]<NAME>:<TYPE>
19-
// - seperated per ,
19+
// - separated per ,
2020
// - starting with a question mark means optional parameter
2121
// - currently supported types: 'string' and 'int'
2222
// - example: 'path:string,?limit:int'
@@ -47,15 +47,15 @@ func parseParams(w http.ResponseWriter, r *http.Request, rules string) (p params
4747
if valueFound {
4848
switch pType {
4949
case "int":
50-
if i, err := strconv.Atoi(value.(string)); err != nil {
50+
i, err := strconv.Atoi(value.(string))
51+
if err != nil {
5152
msg := fmt.Sprintf("unable to convert parameter '%s' to int: %s", pName, err.Error())
5253
logError.Println(msg)
5354
http.Error(w, msg, http.StatusBadRequest)
5455
paramsValid = false
5556
return
56-
} else {
57-
p[pName] = i
5857
}
58+
p[pName] = i
5959
case "string":
6060
// nothing to do
6161
default:

params_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func TestParseParamsInvalidParams(t *testing.T) {
9494
}
9595

9696
func newDummyReq(queryParam string) *http.Request {
97-
testUrl, _ := url.Parse(fmt.Sprintf("http://example.com?%s", queryParam))
97+
testURL, _ := url.Parse(fmt.Sprintf("http://example.com?%s", queryParam))
9898
return &http.Request{
9999
Method: "GET",
100-
URL: testUrl,
100+
URL: testURL,
101101
}
102102
}

utils.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ import (
66
"strings"
77
)
88

9+
// read the file content from the file handle
10+
func readTextFrom(getFh func(string) (*FileHandle, error), name string) (string, error) {
11+
12+
// get the file handle
13+
fh, err := getFh(name)
14+
if err != nil {
15+
logError.Println("unable to get file-handle: ", err.Error())
16+
return "", err
17+
}
18+
19+
// read the file content
20+
content, err := fh.ReadText()
21+
if err != nil {
22+
logError.Println("unable to read the file: ", err.Error())
23+
return "", err
24+
}
25+
26+
return content, err
27+
}
28+
929
// lastElement splits a string by sep and returns the last element
1030
func lastElement(str, sep string) string {
1131
fields := strings.Split(str, sep)
@@ -81,7 +101,7 @@ func countNewLines(text string) int {
81101
count := 0
82102
for _, char := range text {
83103
if char == '\n' {
84-
count += 1
104+
count++
85105
}
86106
}
87107
return count

0 commit comments

Comments
 (0)