-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
52 lines (40 loc) · 927 Bytes
/
command.go
File metadata and controls
52 lines (40 loc) · 927 Bytes
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
package intelpower
import (
"bytes"
"errors"
"fmt"
"os/exec"
"path"
"strings"
)
func (pwr *IntelPower) command(cmd string) (string, error) {
var bufError bytes.Buffer
var bufOut bytes.Buffer
c := exec.Command(pwr.shell[0], pwr.shell[1], cmd)
c.Stderr = &bufError
c.Stdout = &bufOut
err := c.Run()
if err != nil {
return "", NewCommandError(err)
}
if bufError.Len() > 0 {
return "", NewCommandError(errors.New(bufError.String()))
}
return strings.TrimSpace(bufOut.String()), nil
}
func (pwr *IntelPower) cmdRead(filepath ...string) (string, error) {
file := path.Join(filepath...)
resp, err := pwr.command("cat " + file)
if err != nil {
return "", err
}
return resp, nil
}
func (pwr *IntelPower) cmdWrite(data string, filepath ...string) error {
file := path.Join(filepath...)
_, err := pwr.command(fmt.Sprintf("echo %s > %s", data, file))
if err != nil {
return err
}
return nil
}