-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
64 lines (59 loc) · 1.58 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
package ifchanged
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"os/exec"
)
func GetFileSHA256(fileName string) (string, error) {
hasher := sha256.New()
s, err := ioutil.ReadFile(fileName)
hasher.Write(s)
if err != nil {
return "", fmt.Errorf("error finding sha256: %w", err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func ReadFileAsString(fileName string) (string, error) {
s, err := ioutil.ReadFile(fileName)
if err != nil {
return "", fmt.Errorf("error reading file: %w", err)
}
return string(s), nil
}
func SaveSHA256(sha256 string, fileName string) error {
file, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("create sha256 error: %w", err)
}
defer file.Close()
w := bufio.NewWriter(file)
_, err = fmt.Fprintf(w, `%s`, sha256)
if err != nil {
return fmt.Errorf("save sha256 error: %w", err)
}
err = w.Flush()
if err != nil {
return fmt.Errorf("save sha256 error: %w", err)
}
return nil
}
// ExecuteCommand is a helper function to run `exe` file with the `arg`s and
// in case of error, returns a fully formatted error containing both `stderr` and `stdout`
func ExecuteCommand(exe string, arg ...string) error {
cmd := exec.Command(exe, arg...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run() // Block the thread until it finishes
if err != nil {
return fmt.Errorf("execute command error: stderr: \"%s\", stdout: \"%s\" due to %w", stderr.String(), stdout.String(), err)
}
return nil
// cmd.Wait() // Wait is only needed if we cmd.Start()
}