-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmsdo-helpers.ts
More file actions
102 lines (93 loc) · 3.05 KB
/
msdo-helpers.ts
File metadata and controls
102 lines (93 loc) · 3.05 KB
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
import path from "path";
import fs from "fs";
import { IExecOptions } from "azure-pipelines-task-lib/toolrunner";
import tl = require('azure-pipelines-task-lib/task');
import os from 'os';
import { Writable } from "stream";
/**
* Enum for the possible inputs for the task (specified in task.json)
*/
export enum Inputs {
CommandType = 'command'
}
/*
* Enum for the possible values for the Inputs.CommandType (specified in task.json)
*/
export enum CommandType {
PreJob = 'pre-job',
PostJob = 'post-job',
Run = 'run'
}
/**
* Enum for defining constants used in the task.
*/
export enum Constants {
Unknown = "unknown",
PreJobStartTime = "PREJOBSTARTTIME"
}
/**
* Wrapper over the Task.execSync, Execute a command and return its stdout.
* Throw an error if the command fails.
*
* @param cmd
* @param args
* @param options
* @returns stdout of the command
*/
export function execTaskCmdSync(cmd: string, args: string[], options?: IExecOptions): string {
var cmdExecute = tl.execSync(cmd, args, options);
if (cmdExecute.code != 0) {
throw new Error(`Failed to execute command: ${cmd} ${args}.
Exit Code: ${cmdExecute.code}.
Stdout: ${cmdExecute.stdout}.
Stderr: ${cmdExecute.stderr}`);
}
var stdOut = cmdExecute.stdout || "";
return stdOut.trim();
}
/**
* Encodes a string to base64.
*
* @param str - The string to encode.
* @returns The base64 encoded string.
*/
export const encode = (str: string):string => Buffer.from(str, 'binary').toString('base64');
/**
* Returns the encoded content of the Docker version, Docker events, and Docker images in the pre-defined format -
* DockerVersion
* Version: TaskVersion
* <Delim>Events:
* DockerEvents
* <Delim>Images:
* DockerImages
*
* @param dockerVersion - The version of Docker.
* @param dockerEvents - The Docker events.
* @param dockerImages - The Docker images.
* @param taskVersion - Optional version of the task. Defaults to the version in the task.json file.
* @param sectionDelim - Optional delimiter to separate sections in the encoded content. Defaults to ":::".
* @returns The encoded content of the Docker version, Docker events, and Docker images.
*/
export function getEncodedContent(
dockerVersion: string,
dockerEvents: string,
dockerImages: string
): string {
let data : string[] = [];
data.push("DockerVersion: " + dockerVersion);
data.push("DockerEvents:");
data.push(dockerEvents);
data.push("DockerImages:");
data.push(dockerImages);
return encode(data.join(os.EOL));
}
/**
* Writes the specified data to the specified output stream, followed by the platform-specific end-of-line character.
* If no output stream is specified, the data is written to the standard output stream.
*
* @param data - The data to write to the output stream.
* @param outStream - Optional. The output stream to write the data to. Defaults to the standard output stream.
*/
export function writeToOutStream(data: string, outStream: Writable = process.stdout): void {
outStream.write(data.trim() + os.EOL);
}