-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_unix.go
More file actions
86 lines (71 loc) · 2.42 KB
/
Copy pathrunner_unix.go
File metadata and controls
86 lines (71 loc) · 2.42 KB
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
//go:build unix
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2025 Dídimo Grimaldo T.
* go-carousel
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Unix-specific code for Runner.
*-----------------------------------------------------------------*/
package carousel
import (
"fmt"
"os"
"strings"
"github.com/lordofscripts/goapp/app"
"github.com/lordofscripts/goapp/app/logx"
)
/* ----------------------------------------------------------------
* G l o b a l s
*-----------------------------------------------------------------*/
const (
// Unix specific
EXT_LSUSB = "/usr/bin/lsusb" // @note from JSON config
EXT_LSBLK = "/usr/bin/lsblk" // @note idem
EXT_LOGINCTL = "/usr/bin/loginctl" // @note idem
)
/* ----------------------------------------------------------------
* I n i t i a l i z e r
*-----------------------------------------------------------------*/
func init() {
app.AssertOrDie(!FileExists(EXT_LSUSB), "Missing lsusb", 12)
app.AssertOrDie(!FileExists(EXT_LSBLK), "Missing lsblk", 13)
}
/* ----------------------------------------------------------------
* F u n c t i o n s
*-----------------------------------------------------------------*/
func GetMountPoint(volumeLabel string) (string, error) {
outStr, err := ExecuteProgram(EXT_LSBLK, "-P", "-o", "name,label,mountpoint") // or use -P instead of -l
if err != nil {
logx.Println("error IsDeviceOnline", err)
return "", err
}
// with -P: NAME="sda7" LABEL="" MOUNTPOINT="/home"
// with -l: sda7 /home
lines := strings.Split(outStr, "\n")
for _, line := range lines {
if strings.Contains(line, volumeLabel) { // LABEL @todo improve
mpoint := line[strings.Index(line, "MOUNTPOINT=")+12 : len(line)-1]
return mpoint, nil
}
}
return "", fmt.Errorf("mountpoint of '%s' not found", volumeLabel)
}
/**
* Verify that the USB device by VendorID:ProductID is connected
*/
func IsDeviceOnline(vendorId, productId string) (bool, error) {
devId := vendorId + ":" + productId
outStr, err := ExecuteProgram(EXT_LSUSB, "-d", devId)
if err != nil {
logx.Println("error IsDeviceOnline", err)
return false, err
}
if outStr == "" {
return false, nil
}
return true, nil
}
// @audit need a more reliable version but so far it works well.
func IsCronJob() (bool, error) {
return os.Getenv("HOME") == "", nil
}