/
util.go
73 lines (59 loc) · 1.95 KB
/
util.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/fatih/color"
)
var (
bold = color.New(color.Bold).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
magenta = color.New(color.FgHiMagenta).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
)
func doRequest(c *cli.Context, file string, remote string, url string, body io.Reader) *http.Response {
if !dryRun {
req, _ := http.NewRequest("GET", fmt.Sprintf("https://%s%s", remote, url), body)
resp, _ := http.DefaultClient.Do(req)
return resp
}
return nil
}
func fileChecksum(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
hasher := sha256.New()
io.Copy(hasher, file)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
type logFunc func(remote, path string)
func logAdd(remote, path string) {
logrus.Infof("%s: %s %s %s added", time.Now().Format("2006/02/01 15:04:05 -0700"), green("[+]"), blue(remote), bold(path))
}
func logUpdate(remote, path string) {
logrus.Infof("%s: %s %s %s updated", time.Now().Format("2006/02/01 15:04:05 -0700"), magenta("[M]"), blue(remote), bold(path))
}
func logDelete(remote, path string) {
logrus.Infof("%s: %s %s %s deleted", time.Now().Format("2006/02/01 15:04:05 -0700"), yellow("[-]"), blue(remote), bold(path))
}
func logWarning(remote, path, cause string, tokens ...interface{}) {
logrus.Warnf("%s: %s %s %s skipped: %s", time.Now().Format("2006/02/01 15:04:05 -0700"), red("[!]"), blue(remote), bold(path), fmt.Sprintf(cause, tokens...))
}
func handleResponse(remote, path string, resp *http.Response, log logFunc) {
if resp == nil || resp.StatusCode == http.StatusAccepted {
log(remote, path)
} else {
logWarning(remote, path, "remote could not process file")
}
}