-
Notifications
You must be signed in to change notification settings - Fork 12
/
omxplayer_helpers.go
66 lines (60 loc) · 1.76 KB
/
omxplayer_helpers.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
package omxplayer
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// removeFile removes the specified file. Errors are ignored.
func removeFile(path string) {
log.WithFields(log.Fields{
"path": path,
}).Debug("omxplayer: removing file")
os.Remove(path)
}
// waitForFile waits for the specified file to exist before returning. If the an
// error, other than the file not existing, occurs, the error is returned. If,
// after 100 attempts, the file does not exist, an error is returned.
func waitForFile(path string) error {
log.WithFields(log.Fields{
"file": path,
}).Debug("omxplayer: waiting for file")
for i := 0; i < 100; i++ {
_, err := os.Stat(path)
if err == nil || !os.IsNotExist(err) {
return err
}
time.Sleep(50 * time.Millisecond)
}
return fmt.Errorf("omxplayer: file does not exist: %s", path)
}
// readFile waits for the specified file to contain contents, and then returns
// those contents as a string. If an error occurs while reading the file, the
// error is returned. If the file has no content after 100 attempts, an error is
// returned.
func readFile(path string) (string, error) {
log.WithFields(log.Fields{
"file": path,
}).Debug("omxplayer: reading file")
for i := 0; i < 100; i++ {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
if len(bytes) > 0 {
return strings.TrimSpace(string(bytes)), err
}
time.Sleep(50 * time.Millisecond)
}
return "", fmt.Errorf("omxplayer: file is empty: %s", path)
}
// setEnv sets the specified environment variable to the specified value.
func setEnv(variable, value string) {
log.WithFields(log.Fields{
"variable": variable,
"value": value,
}).Debug("omxplayer: setting environment variable")
os.Setenv(variable, value)
}