github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/biz/impl/shared.go (about) 1 // Ultimate Provisioner: UP cmd 2 // Copyright (c) 2019 Stephen Cheng and contributors 3 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 7 8 package impl 9 10 import ( 11 "bufio" 12 "github.com/fatih/color" 13 "github.com/upcmd/up/model/core" 14 u "github.com/upcmd/up/utils" 15 ee "github.com/upcmd/up/utils/error" 16 "os" 17 ) 18 19 const ( 20 FUNC_SHELL = "shell" 21 FUNC_CALL = "call" 22 FUNC_BLOCK = "block" 23 FUNC_CMD = "cmd" 24 25 NONE_VALUE = "None" 26 LAST_RESULT = "last_result" 27 FLAG_SILENT = "silent" 28 29 UP_ENTRY_TASK_NAME = "UP_ENTRY_TASK" 30 ) 31 32 type MustConditionToContinueFunc func() bool 33 34 func DryRunOrExit(mark string, mustCondition MustConditionToContinueFunc, conditionDesc string) { 35 36 ok := mustCondition() 37 38 if TaskerRuntime().Tasker.Dryrun { 39 color.Green(" %s -> %s", mark, "in dryrun, try to ignore") 40 if !ok { 41 color.Red(" %s -> %s", mark, "can not continue further due to critical condition not satisfied") 42 color.Red(" %s -> %s", mark, conditionDesc) 43 os.Exit(-1) 44 } 45 } else { 46 os.Exit(-1) 47 } 48 49 } 50 51 type ContinueFunc func() 52 53 //if there is NoFault, then continue 54 //or if there is a fault in the allowed list, then skip rest, do not run continueFunc 55 //else the fault is not ignorable, then if use DryRunOrExitFunc 56 func DryRunAndSkip(mark string, allowedErrors []string, continueFunc ContinueFunc, mustCondition MustConditionToContinueFunc) { 57 if mark == ee.NOFAULT { 58 continueFunc() 59 } else if u.Contains(allowedErrors, mark) { 60 //do nothing 61 if TaskerRuntime().Tasker.Dryrun { 62 u.Pdryrun("in dry run and skip further") 63 } 64 } else { 65 if mustCondition != nil { 66 DryRunOrExit("mark", mustCondition, "trying to continue") 67 } 68 } 69 } 70 71 func pause(execvars *core.Cache) { 72 hint := ` 73 enter: continue 74 q: quit 75 i: inspect 76 ` 77 u.Pprompt("pause action to continue", hint) 78 reader := bufio.NewReader(os.Stdin) 79 keyinput, _ := reader.ReadString('\n') 80 81 switch keyinput { 82 case "q\n": 83 u.GraceExit("puase action", "client choose to stop continuing the execution") 84 case "i\n": 85 u.Ppfmsg("runtime exec vars:", *execvars) 86 pause(execvars) 87 default: 88 //continue 89 } 90 } 91 92 func IsCalledTask() (called bool) { 93 if TaskerStack.GetLen() > 1 { 94 called = true 95 } else { 96 if TaskerRuntime().Tasker.TaskStack.GetLen() > 1 { 97 called = true 98 } else { 99 called = false 100 } 101 } 102 return 103 } 104 105 func IsAtRootTaskLevel() (called bool) { 106 if TaskerRuntime().Tasker.TaskStack.GetLen() == 0 { 107 called = true 108 } else { 109 called = false 110 } 111 return 112 }