This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
129 lines (109 loc) · 3.04 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package utils
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
const (
defaultFilePerm os.FileMode = 0666
defaultPathPerm os.FileMode = 0777
)
// nopWriteCloser wraps an io.Writer and provides a no-op Close method to
// satisfy the io.WriteCloser interface.
type nopWriteCloser struct {
io.Writer
}
func (wc *nopWriteCloser) Write(p []byte) (int, error) { return wc.Writer.Write(p) }
func (wc *nopWriteCloser) Close() error { return nil }
// IsDir return true if the path is a directory
func IsDir(path string) bool {
if info, err := os.Stat(path); err == nil && info.IsDir() {
return true
}
return false
}
// FileWalk type channel that represent interation trough a path
type FileWalk chan string
// Walk do the walk
func (f FileWalk) Walk(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
f <- path
}
return nil
}
// IterateUpload will walk the files in the walker and call callback to upload it one by one
func (f FileWalk) IterateUpload(localSource string, callback func(path, rel string) error) {
// walk the files
walker := make(FileWalk)
go func() {
// Gather the files to upload by walking the path recursively.
if err := filepath.Walk(localSource, walker.Walk); err != nil {
log.Fatalln("Error: ", fmt.Sprintf("%v", err))
}
close(walker)
}()
// For each file found on the recursive
for path := range walker {
rel, err := filepath.Rel(localSource, path)
if err != nil {
log.Fatalln("Unable to get relative path:", path, err)
}
callback(path, rel)
}
}
// StringInSlice Return true if a is in the list slice
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// WriteReaderToFile will write an io.Reader to file dest
func WriteReaderToFile(r io.Reader, dest string) error {
mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC // overwrite if exists
f, err := os.OpenFile(dest, mode, defaultFilePerm)
if err != nil {
return fmt.Errorf("open file: %s", err)
}
wc := io.WriteCloser(&nopWriteCloser{f})
if _, err := io.Copy(wc, r); err != nil {
f.Close() // error deliberately ignored
return fmt.Errorf("i/o copy: %s", err)
}
if err := wc.Close(); err != nil {
f.Close() // error deliberately ignored
return fmt.Errorf("compression close: %s", err)
}
if err := f.Sync(); err != nil {
f.Close() // error deliberately ignored
return fmt.Errorf("file sync: %s", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("file close: %s", err)
}
return nil
}
// GetByteHash will return hex sha264 of specified byte sequence
func GetByteHash(bt []byte) string {
h := sha256.New()
h.Write(bt)
return fmt.Sprintf("%x", h.Sum(nil))
}
// GetStringHash will return string hash of specified string
func GetStringHash(plainString string) string {
return GetByteHash([]byte(plainString))
}
// GetFileHash will return string hash of specified file
func GetFileHash(path string) string {
slurp, _ := ioutil.ReadFile(path)
return GetByteHash(slurp)
}