-
Notifications
You must be signed in to change notification settings - Fork 4
/
shared.go
106 lines (91 loc) · 2.42 KB
/
shared.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
// Ultimate Provisioner: UP cmd
// Copyright (c) 2019 Stephen Cheng and contributors
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package impl
import (
"bufio"
"github.com/fatih/color"
"github.com/upcmd/up/model/core"
u "github.com/upcmd/up/utils"
ee "github.com/upcmd/up/utils/error"
"os"
)
const (
FUNC_SHELL = "shell"
FUNC_CALL = "call"
FUNC_BLOCK = "block"
FUNC_CMD = "cmd"
)
type MustConditionToContinueFunc func() bool
func DryRunOrExit(mark string, mustCondition MustConditionToContinueFunc, conditionDesc string) {
ok := mustCondition()
if TaskerRuntime().Tasker.Dryrun {
color.Green(" %s -> %s", mark, "in dryrun, try to ignore")
if !ok {
color.Red(" %s -> %s", mark, "can not continue further due to critical condition not satisfied")
color.Red(" %s -> %s", mark, conditionDesc)
os.Exit(-1)
}
} else {
os.Exit(-1)
}
}
type ContinueFunc func()
//if there is NoFault, then continue
//or if there is a fault in the allowed list, then skip rest, do not run continueFunc
//else the fault is not ignorable, then if use DryRunOrExitFunc
func DryRunAndSkip(mark string, allowedErrors []string, continueFunc ContinueFunc, mustCondition MustConditionToContinueFunc) {
if mark == ee.NOFAULT {
continueFunc()
} else if u.Contains(allowedErrors, mark) {
//do nothing
if TaskerRuntime().Tasker.Dryrun {
u.Pdryrun("in dry run and skip further")
}
} else {
if mustCondition != nil {
DryRunOrExit("mark", mustCondition, "trying to continue")
}
}
}
func pause(execvars *core.Cache) {
hint := `
enter: continue
q: quit
i: inspect
`
u.Ppromptvvvvv("pause action to continue", hint)
reader := bufio.NewReader(os.Stdin)
keyinput, _ := reader.ReadString('\n')
switch keyinput {
case "q\n":
u.GraceExit("puase action", "client choose to stop continuing the execution")
case "i\n":
u.Ppfmsg("runtime exec vars:", *execvars)
pause(execvars)
default:
//continue
}
}
func IsCalledTask() (called bool) {
if TaskerStack.GetLen() > 1 {
called = true
} else {
if TaskerRuntime().Tasker.TaskStack.GetLen() > 1 {
called = true
} else {
called = false
}
}
return
}
func IsAtRootTaskLevel() (called bool) {
if TaskerRuntime().Tasker.TaskStack.GetLen() == 0 {
called = true
} else {
called = false
}
return
}