cli: login

This commit is contained in:
Koushik Dutta
2021-09-23 14:31:47 -07:00
parent e546ebb185
commit 9ef615431c
3 changed files with 54 additions and 36 deletions

4
cli/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "scrypted",
"version": "1.0.14",
"version": "1.0.17",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "scrypted",
"version": "1.0.14",
"version": "1.0.17",
"license": "ISC",
"dependencies": {
"@scrypted/sdk": "^0.0.79",

View File

@@ -1,6 +1,6 @@
{
"name": "scrypted",
"version": "1.0.14",
"version": "1.0.17",
"description": "",
"main": "dist/main.js",
"bin": {

View File

@@ -15,6 +15,42 @@ function getUserHome() {
return ret;
}
const scryptedHome = path.join(getUserHome(), '.scrypted');
const loginPath = path.join(scryptedHome, 'login.json');
async function doLogin(ip: string) {
const username = readline.question('username: ');
const password = readline.question('password: ', {
hideEchoBack: true,
});
const url = `https://${ip}:9443/login`;
const response = await axios(Object.assign({
method: 'GET',
auth: {
username,
password,
},
url,
}, axiosConfig));
mkdirp.sync(scryptedHome);
let login: any;
try {
login = JSON.parse(fs.readFileSync(loginPath).toString());
}
catch (e) {
login = {};
}
if (typeof login !== 'object')
login = {};
login = login || {};
login[ip] = response.data;
fs.writeFileSync(loginPath, JSON.stringify(login));
return login;
}
const axiosConfig: AxiosRequestConfig = {
httpsAgent: new https.Agent({
rejectUnauthorized: false
@@ -22,7 +58,12 @@ const axiosConfig: AxiosRequestConfig = {
}
async function main() {
if (process.argv[2] === 'install') {
if (process.argv[2] === 'login') {
const ip = process.argv[4] || '127.0.0.1';
await doLogin(ip);
console.log('login successful.')
}
else if (process.argv[2] === 'install') {
const ip = process.argv[4] || '127.0.0.1';
const pkg = process.argv[3];
@@ -31,47 +72,22 @@ async function main() {
process.exit(1);
}
const scryptedHome = path.join(getUserHome(), '.scrypted');
const loginPath = path.join(scryptedHome, 'login.json');
let username: string;
let password: string;
let login: any;
try {
login = JSON.parse(fs.readFileSync(loginPath).toString());
if (typeof login !== 'object')
login = {};
if (!login[ip].username || !login[ip].token)
throw new Error();
username = login[ip].username;
password = login[ip].token;
}
catch (e) {
username = readline.question('username: ');
password = readline.question('password: ', {
hideEchoBack: true,
});
const url = `https://${ip}:9443/login`;
const authedConfig = Object.assign({
method: 'GET',
auth: {
username,
password,
},
url,
}, axiosConfig);
const response = await axios(authedConfig);
mkdirp.sync(scryptedHome);
login = login || {};
login[ip] = response.data;
fs.writeFileSync(loginPath, JSON.stringify(login));
login = await doLogin(ip);
}
const url = `https://${ip}:9443/web/component/script/install/${pkg}`;
const response = await axios.post(url, undefined, Object.assign({
method: 'GET',
const response = await axios(Object.assign({
method: 'POST',
auth: {
username: login[ip].username,
password: login[ip].token,
@@ -79,10 +95,12 @@ async function main() {
url,
}, axiosConfig));
console.log('install successful. id:',response.data.id);
}
console.log('install successful. id:', response.data.id);
}
else {
console.log('usage: npx scrypted install npm-package-name');
console.log('usage:');
console.log(' npx scrypted install npm-package-name [ip]');
console.log(' npx scrypted login [ip]');
process.exit(1);
}
}