/
options.ts
281 lines (265 loc) · 8.52 KB
/
options.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import path from 'path'
import os from 'os'
import { exit } from 'process'
import { existsSync } from 'fs'
import * as core from '@actions/core'
import * as z from 'zod'
import untildify from 'untildify'
type Inputs = Readonly<{
pixiVersion?: string
pixiUrl?: string
logLevel?: LogLevel
manifestPath?: string
runInstall?: boolean
environments?: string[]
frozen?: boolean
locked?: boolean
cache?: boolean
cacheKey?: string
cacheWrite?: boolean
pixiBinPath?: string
authHost?: string
authToken?: string
authUsername?: string
authPassword?: string
authCondaToken?: string
postCleanup?: boolean
}>
export type PixiSource =
| {
version: string
}
| {
url: string
}
type Auth = {
host: string
} & (
| {
token: string
}
| {
username: string
password: string
}
| {
condaToken: string
}
)
type Cache = {
cacheKeyPrefix: string
cacheWrite: boolean
}
export type Options = Readonly<{
pixiSource: PixiSource
logLevel: LogLevel
manifestPath: string
pixiLockFile: string
runInstall: boolean
environments?: string[]
frozen: boolean
locked: boolean
cache?: Cache
pixiBinPath: string
auth?: Auth
postCleanup: boolean
}>
const logLevelSchema = z.enum(['q', 'default', 'v', 'vv', 'vvv'])
export type LogLevel = z.infer<typeof logLevelSchema>
export const PATHS = {
pixiBin: path.join(os.homedir(), '.pixi', 'bin', `pixi${os.platform() === 'win32' ? '.exe' : ''}`)
}
const parseOrUndefined = <T>(key: string, schema: z.ZodSchema<T>, errorMessage?: string): T | undefined => {
const input = core.getInput(key)
// GitHub actions sets empty inputs to the empty string, but we want undefined
if (input === '') {
return undefined
}
const maybeResult = schema.safeParse(input)
if (!maybeResult.success) {
if (!errorMessage) {
throw new Error(`${key} is not valid: ${maybeResult.error.message}`)
}
throw new Error(errorMessage)
}
return maybeResult.data
}
const parseOrUndefinedJSON = <T>(key: string, schema: z.ZodSchema<T>): T | undefined => {
const input = core.getInput(key)
// GitHub actions sets empty inputs to the empty string, but we want undefined
if (input === '') {
return undefined
}
return schema.parse(JSON.parse(input))
}
const parseOrUndefinedList = <T>(key: string, schema: z.ZodSchema<T>): T[] | undefined => {
const input = core.getInput(key)
// GitHub actions sets empty inputs to the empty string, but we want undefined
if (input === '') {
return undefined
}
return input
.split(' ')
.map((s) => schema.parse(s))
.filter((s) => s !== '')
}
const validateInputs = (inputs: Inputs): void => {
if (inputs.pixiVersion && inputs.pixiUrl) {
throw new Error('You need to specify either pixi-version or pixi-url')
}
if (inputs.cacheKey !== undefined && inputs.cache === false) {
throw new Error('Cannot specify cache key without caching')
}
if (inputs.runInstall === false && inputs.cache === true) {
throw new Error('Cannot cache without running install')
}
if (inputs.runInstall === false && inputs.frozen === true) {
throw new Error('Cannot use `frozen: true` when not running install')
}
if (inputs.runInstall === false && inputs.locked === true) {
throw new Error('Cannot use `locked: true` when not running install')
}
if (inputs.locked === true && inputs.frozen === true) {
throw new Error('Cannot use `locked: true` and `frozen: true` at the same time')
}
if ((inputs.authUsername && !inputs.authPassword) || (!inputs.authUsername && inputs.authPassword)) {
throw new Error('You need to specify both auth-username and auth-password')
}
// now we can assume that authUsername is defined iff authPassword is defined
if (inputs.authHost) {
if (!inputs.authToken && !inputs.authUsername && !inputs.authCondaToken) {
throw new Error('You need to specify either auth-token or auth-username and auth-password or auth-conda-token')
}
if (
(inputs.authToken && (inputs.authUsername || inputs.authCondaToken)) ||
(inputs.authUsername && inputs.authCondaToken)
) {
throw new Error('You cannot specify two auth methods')
}
}
if (!inputs.authHost) {
if (inputs.authToken || inputs.authUsername || inputs.authCondaToken) {
throw new Error('You need to specify auth-host')
}
}
if (inputs.runInstall === false && inputs.environments) {
throw new Error('Cannot specify environments without running install')
}
}
const inferOptions = (inputs: Inputs): Options => {
const runInstall = inputs.runInstall ?? true
const pixiSource = inputs.pixiVersion
? { version: inputs.pixiVersion }
: inputs.pixiUrl
? { url: inputs.pixiUrl }
: { version: 'latest' }
const logLevel = inputs.logLevel ?? (core.isDebug() ? 'vv' : 'default')
const manifestPath = inputs.manifestPath ? path.resolve(untildify(inputs.manifestPath)) : 'pixi.toml'
const pixiLockFile = path.join(path.dirname(manifestPath), 'pixi.lock')
const lockFileAvailable = existsSync(pixiLockFile)
core.debug(`lockFileAvailable: ${lockFileAvailable}`)
if (!lockFileAvailable && inputs.cacheWrite === true) {
throw new Error('You cannot specify cache-write = true without a lock file present')
}
const cache = inputs.cacheKey
? { cacheKeyPrefix: inputs.cacheKey, cacheWrite: inputs.cacheWrite ?? true }
: inputs.cache === true || (lockFileAvailable && inputs.cache !== false)
? { cacheKeyPrefix: 'pixi-', cacheWrite: inputs.cacheWrite ?? true }
: undefined
const pixiBinPath = inputs.pixiBinPath ? path.resolve(untildify(inputs.pixiBinPath)) : PATHS.pixiBin
const frozen = inputs.frozen ?? false
const locked = inputs.locked ?? (lockFileAvailable && !frozen)
const auth = !inputs.authHost
? undefined
: ((inputs.authToken
? {
host: inputs.authHost,
token: inputs.authToken
}
: inputs.authCondaToken
? {
host: inputs.authHost,
condaToken: inputs.authCondaToken
}
: {
host: inputs.authHost,
username: inputs.authUsername!,
password: inputs.authPassword!
}) as Auth)
const postCleanup = inputs.postCleanup ?? true
return {
pixiSource,
logLevel,
manifestPath,
pixiLockFile,
runInstall,
environments: inputs.environments,
frozen,
locked,
cache,
pixiBinPath,
auth,
postCleanup
}
}
const assertOptions = (_options: Options) => {
// const assert = (condition: boolean, message?: string) => {
// if (!condition) {
// throw new Error(message)
// }
// }
// TODO
}
const getOptions = () => {
const inputs: Inputs = {
pixiVersion: parseOrUndefined(
'pixi-version',
z.union([z.literal('latest'), z.string().regex(/^v\d+\.\d+\.\d+$/)]),
'pixi-version must either be `latest` or a version string matching `vX.Y.Z`.'
),
pixiUrl: parseOrUndefined('pixi-url', z.string().url()),
logLevel: parseOrUndefined(
'log-level',
logLevelSchema,
'log-level must be one of `q`, `default`, `v`, `vv`, `vvv`.'
),
manifestPath: parseOrUndefined('manifest-path', z.string()),
runInstall: parseOrUndefinedJSON('run-install', z.boolean()),
environments: parseOrUndefinedList('environments', z.string()),
locked: parseOrUndefinedJSON('locked', z.boolean()),
frozen: parseOrUndefinedJSON('frozen', z.boolean()),
cache: parseOrUndefinedJSON('cache', z.boolean()),
cacheKey: parseOrUndefined('cache-key', z.string()),
cacheWrite: parseOrUndefinedJSON('cache-write', z.boolean()),
pixiBinPath: parseOrUndefined('pixi-bin-path', z.string()),
authHost: parseOrUndefined('auth-host', z.string()),
authToken: parseOrUndefined('auth-token', z.string()),
authUsername: parseOrUndefined('auth-username', z.string()),
authPassword: parseOrUndefined('auth-password', z.string()),
authCondaToken: parseOrUndefined('auth-conda-token', z.string()),
postCleanup: parseOrUndefinedJSON('post-cleanup', z.boolean())
}
core.debug(`Inputs: ${JSON.stringify(inputs)}`)
validateInputs(inputs)
const options = inferOptions(inputs)
core.debug(`Inferred options: ${JSON.stringify(options)}`)
assertOptions(options)
return options
}
let _options: Options
try {
_options = getOptions()
} catch (error) {
if (core.isDebug()) {
throw error
}
if (error instanceof Error) {
core.setFailed(error.message)
exit(1)
} else if (typeof error === 'string') {
core.setFailed(error)
exit(1)
}
throw error
}
export const options = _options