forked from TritonDataCenter/node-docker-registry-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainline.ts
executable file
·125 lines (110 loc) · 3.13 KB
/
mainline.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
/*
* 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 http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2015, Joyent, Inc.
*/
/*
* Shared code for some of the examples in this dir to get CLI options.
*/
// var assert = require('assert-plus');
// var bunyan = require('bunyan');
// var dashdash = require('dashdash');
// var format = require('util').format;
// var read = require('read');
import { parse } from "https://deno.land/std@0.130.0/flags/mod.ts";
export interface CliOption {
names: string[];
type: 'bool' | 'number' | 'string';
help?: string;
}
var optionsNoAuth: CliOption[] = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Print this help and exit.'
},
{
names: ['verbose', 'v'],
type: 'bool',
help: 'Verbose logging.'
},
{
names: ['insecure', 'k'],
type: 'bool',
help: 'Allow insecure SSL connections (i.e. do not enforce SSL certs)'
},
{
names: ['schema', 's'],
type: 'number',
help: 'Maximum schema version to request (1 or 2, defaults to 1)'
}
];
var options: CliOption[] = [
...optionsNoAuth,
{
names: ['username', 'u'],
type: 'string',
help: 'Basic auth username'
},
{
names: ['password', 'p'],
type: 'string',
help: 'Basic auth password'
},
];
export function fail(cmd: string, err: Error, opts: {
verbose?: boolean;
} = {}) {
var errToShow = opts.verbose ? err.stack || err : err.message || err;
console.error('%s: error: %s', cmd, errToShow);
Deno.exit(2);
}
export function mainline(config: {
cmd: string;
excludeAuth?: boolean;
options?: CliOption[];
}) {
var dashOpts = (config.excludeAuth ? optionsNoAuth : options);
if (config.options) {
// Add to existing options.
dashOpts = dashOpts.concat(config.options);
}
const parseArgs = {
string: new Array<string>(),
boolean: new Array<string>(),
alias: Object.create(null) as Record<string, string[]>,
};
for (const opt of dashOpts) {
if (opt.names.length > 1) {
parseArgs.alias[opt.names[0]] = opt.names.slice(1);
}
if (opt.type === 'string') {
parseArgs.string.push(opt.names[0]);
}
if (opt.type === 'bool') {
parseArgs.boolean.push(opt.names[0]);
}
}
const opts = parse(Deno.args, parseArgs);
// var logLevel = 'warn';
// if (opts.verbose) {
// logLevel = 'trace';
// }
// var log = bunyan.createLogger({
// name: config.cmd,
// level: logLevel
// });
// Handle password prompt, if necessary.
if (opts.username && !opts.password) {
// var readOpts = {
// prompt: format('Password for %s: ', opts.username),
// silent: true
// };
opts.password = prompt(`Password for ${opts.username}`)?.trim();
}
// cb(log, parser, opts, opts._args);
return {opts, args: opts._.map(x => x.toString())};
}