-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLineUtils.scala
171 lines (159 loc) · 4.68 KB
/
CommandLineUtils.scala
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package org.encalmo.utils
import scala.io.AnsiColor.*
object CommandLineUtils {
def optionalScriptParameter(shortName: Char, longName: String)(
args: Array[String]
): Option[String] =
args.zipWithIndex
.find((a, i) =>
a == s"-$shortName"
|| a.startsWith(s"-$shortName=")
|| a == s"--$longName"
|| a.startsWith(s"--${longName}=")
)
.flatMap((a, i) =>
if (a.startsWith(s"--${longName}="))
then Some(a.drop(s"--${longName}=".size))
else if (a.startsWith(s"-$shortName="))
then Some(a.drop(s"-$shortName=".size))
else if (args.length > i + 1) then
val value = args(i + 1)
if (value.startsWith("-")) None
else Some(value)
else None
)
.orElse {
val sysValue = System.getProperty(longName)
if (sysValue != null) then Some(sysValue)
else
val envValue = System.getenv(longName.toUpperCase())
if (envValue != null) then Some(envValue)
else None
}
def optionalScriptParameter(longName: String)(
args: Array[String]
): Option[String] =
args.zipWithIndex
.find((a, i) => a == s"--$longName" || a.startsWith(s"--${longName}="))
.flatMap((a, i) =>
if (a.startsWith(s"--${longName}="))
then Some(a.drop(s"--${longName}=".size))
else if (args.length > i + 1) then
val value = args(i + 1)
if (value.startsWith("-")) None
else Some(value)
else None
)
.orElse {
val sysValue = System.getProperty(longName)
if (sysValue != null) then Some(sysValue)
else
val envValue = System.getenv(longName.toUpperCase())
if (envValue != null) then Some(envValue)
else None
}
def requiredScriptParameter(shortName: Char, longName: String)(
args: Array[String]
): String =
optionalScriptParameter(shortName, longName)(args)
.getOrElse {
System.err.println(
s"${RED}Required ${BOLD}-$shortName${RESET}${RED} or ${BOLD}--$longName${RESET}${RED} parameter is missing.${RESET}"
)
System.exit(1)
""
}
def requiredScriptParameter(longName: String)(
args: Array[String]
): String =
optionalScriptParameter(longName)(args)
.getOrElse {
System.err.println(
s"${RED}Required ${BOLD}--$longName${RESET}${RED} parameter is missing.${RESET}"
)
System.exit(1)
""
}
inline def optionalScriptFlag(shortName: Char, longName: String)(
args: Array[String]
): Boolean =
args.exists(a =>
a == s"-$shortName"
|| a.startsWith(s"-$shortName=")
|| a == s"--$longName"
|| a.startsWith(s"--$longName=")
)
inline def optionalScriptFlag(longName: String)(
args: Array[String]
): Boolean = {
args.exists(a => a == s"--$longName" || a.startsWith(s"--$longName="))
}
def execute(
command: String,
cwd: os.Path = os.pwd,
showOutput: Boolean = true
): Either[Iterable[String], Iterable[String]] = {
executeCommandArray(
command
.replace(" \\", " ")
.replace("\n", "")
.replace("\r", "")
.replace("\t", " ")
.split(" ")
.filterNot(_.isBlank()),
cwd,
showOutput
)
}
def executeCommandArray(
commandArray: Array[String],
cwd: os.Path = os.pwd,
showOutput: Boolean = true
): Either[Iterable[String], Iterable[String]] = {
if (commandArray.length > 0)
then {
println(s"${BLUE}${BOLD}${commandArray.mkString(" ")}${RESET}")
val out = collection.mutable.Buffer[String]()
val commandResult = os
.proc(commandArray)
.call(
check = false,
cwd = cwd,
stdout = os.ProcessOutput.Readlines { line =>
if (showOutput) then {
print(BLUE)
print(line)
println(RESET)
}
out.append(line)
},
stderr = os.ProcessOutput.Readlines { line =>
if (showOutput) then {
print(RED)
print(line)
println(RESET)
}
out.append(line)
}
)
if (commandResult.exitCode != 0)
then {
println(
s"${WHITE}${RED_B}[FATAL] script's command ${YELLOW}${commandArray(
0
)}${WHITE} returned ${commandResult.exitCode} ${RESET}"
)
Left(out)
} else {
Right(out)
}
} else Left(Seq.empty)
}
extension [L, R](e: Either[L, R]) {
def exitOfFailure: R =
e.fold(
_ => System.exit(2).asInstanceOf[R],
identity
)
}
}