-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
151 lines (122 loc) · 2.47 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/NonLogicalDev/shell.async-goprompt/pkg/shellout"
ps "github.com/mitchellh/go-ps"
)
// ----------------------------------------------------------------------------
func timeFMT(ts time.Time) string {
return ts.Format("15:04:05 01/02/06")
}
// ----------------------------------------------------------------------------
type shellKV struct {
name string
value interface{}
}
func (kv shellKV) String() string {
return fmt.Sprintf("%s\t%v", kv.name, kv.value)
}
// ----------------------------------------------------------------------------
func shellKVStaggeredPrinter(
printCH <-chan shellKV,
dFirst time.Duration,
d time.Duration,
) {
var parts []shellKV
printParts := func(parts []shellKV) {
if len(parts) == 0 {
return
}
for _, p := range parts {
fmt.Println(p.String())
}
if len(parts) > 0 {
fmt.Println()
}
os.Stdout.Sync()
}
timer := time.NewTimer(dFirst)
printLoop:
for {
select {
case rx, ok := <-printCH:
if !ok {
break printLoop
}
parts = append(parts, rx)
case <-timer.C:
printParts(parts)
parts = nil
timer.Reset(d)
}
}
printParts(parts)
parts = nil
}
// ----------------------------------------------------------------------------
func stringExec(path string, args ...string) (string, error) {
ctx, ctxCancel := context.WithTimeout(bgctx, 10*time.Second)
defer ctxCancel()
out, err := shellout.New(ctx,
shellout.Args(path, args...),
shellout.EnvInherit(),
shellout.EnvSet(map[string]string{
"GIT_OPTIONAL_LOCKS": "0",
}),
).RunString()
return trim(out), err
}
func moduleFindProcessChain() ([]ps.Process, error) {
psPTR := os.Getpid()
var pidChain []ps.Process
for {
if psPTR == 0 {
break
}
psInfo, err := ps.FindProcess(psPTR)
if err != nil {
return nil, err
}
pidChain = append(pidChain, psInfo)
psPTR = psInfo.PPid()
}
return pidChain, nil
}
func trimPath(s string) string {
var out []string
parts := strings.Split(s, "/")
for i, part := range parts {
if i == len(parts)-1 {
out = append(out, part)
} else {
out = append(out, part[0:intMin(len(part), 1)])
}
}
return strings.Join(out, "/")
}
func intMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func intMin(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func trim(s string) string {
return strings.Trim(s, "\n")
}
func strInt(s string) int {
r, _ := strconv.Atoi(s)
return r
}