Skip to content

Commit

Permalink
Moved logs and run to commands.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
grant committed May 30, 2018
1 parent 993bd79 commit de71328
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 78 deletions.
78 changes: 2 additions & 76 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ import 'connect';
const open = require('open');
const path = require('path');
const commander = require('commander');
const chalk = require('chalk');
import * as pluralize from 'pluralize';
import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME,
ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL,
getProjectSettings, getAPIFileType, checkIfOnline,
saveProjectId, manifestExists } from './src/utils';
import { drive, script, logger, getAPICredentials, login } from './src/auth';
import { LOG, help, defaultCmd,
logout, create, clone} from './src/commands';
import { LOG, help, defaultCmd, logs, run,
logout, create, clone} from './src/commands';
import {getProjectFiles, fetchProject, getFileType, hasProject} from './src/files';

// Functions (not yet moved out of this file)
Expand Down Expand Up @@ -298,79 +297,6 @@ const redeploy = async (deploymentId: string, version: string, description: stri
});
});
};
const logs = async (cmd: {
json: boolean,
open: boolean,
}) => {
await checkIfOnline();
function printLogs(entries: any[]) {
for (let i = 0; i < 50 && i < entries.length; ++i) {
const { severity, timestamp, resource, textPayload, protoPayload, jsonPayload } = entries[i];
let functionName = resource.labels.function_name;
functionName = functionName ? functionName.padEnd(15) : ERROR.NO_FUNCTION_NAME;
let payloadData: any = '';
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
} else {
const data: any = {
textPayload,
jsonPayload: jsonPayload ? jsonPayload.fields.message.stringValue : '',
protoPayload,
};
payloadData = data.textPayload || data.jsonPayload || data.protoPayload || ERROR.PAYLOAD_UNKNOWN;
if (payloadData && typeof(payloadData) === 'string') {
payloadData = payloadData.padEnd(20);
}
}
const coloredStringMap: any = {
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity),
};
let coloredSeverity:string = coloredStringMap[severity] || severity;
coloredSeverity = String(coloredSeverity).padEnd(20);
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`);
}
}
const { projectId } = await getProjectSettings();
if (!projectId) {
console.error(ERROR.NO_GCLOUD_PROJECT);
process.exit(-1);
}
if (cmd.open) {
const url = 'https://console.cloud.google.com/logs/viewer?project=' +
`${projectId}&resource=app_script_function`;
console.log(`Opening logs: ${url}`);
open(url);
process.exit(0);
}
getAPICredentials(async () => {
const { data } = await logger.entries.list({
resourceNames: [
`projects/${projectId}`,
],
});
printLogs(data.entries);
});
};
const run = (functionName:string) => {
getAPICredentials(async () => {
await checkIfOnline();
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
const params = {
scriptId,
function: functionName,
devMode: false,
};
script.scripts.run(params).then(response => {
console.log(response.data);
}).catch(e => {
console.log(e);
});
});
});
};

// CLI

Expand Down
79 changes: 77 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DOT, PROJECT_NAME, getScriptURL,
logError, ClaspSettings, DOTFILE, ERROR,
checkIfOnline, spinner, saveProjectId, manifestExists,
getProjectSettings } from './utils';
getProjectSettings, ProjectSettings } from './utils';
const open = require('open');
import {hasProject, fetchProject} from './files';
import { authorize, getAPICredentials, drive, script} from './auth';
import { authorize, getAPICredentials, drive, script, logger} from './auth';
import * as pluralize from 'pluralize';
const commander = require('commander');
import * as del from 'del';
const chalk = require('chalk');
const { prompt } = require('inquirer');

// Log messages (some logs take required params)
Expand Down Expand Up @@ -143,4 +145,77 @@ export const clone = async (scriptId: string, versionNumber?: number) => {
export const logout = () => {
del(DOT.RC.ABSOLUTE_PATH, { force: true }); // del doesn't work with a relative path (~)
del(DOT.RC.ABSOLUTE_LOCAL_PATH, { force: true });
};
export const logs = async (cmd: {
json: boolean,
open: boolean,
}) => {
await checkIfOnline();
function printLogs(entries: any[]) {
for (let i = 0; i < 50 && i < entries.length; ++i) {
const { severity, timestamp, resource, textPayload, protoPayload, jsonPayload } = entries[i];
let functionName = resource.labels.function_name;
functionName = functionName ? functionName.padEnd(15) : ERROR.NO_FUNCTION_NAME;
let payloadData: any = '';
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
} else {
const data: any = {
textPayload,
jsonPayload: jsonPayload ? jsonPayload.fields.message.stringValue : '',
protoPayload,
};
payloadData = data.textPayload || data.jsonPayload || data.protoPayload || ERROR.PAYLOAD_UNKNOWN;
if (payloadData && typeof(payloadData) === 'string') {
payloadData = payloadData.padEnd(20);
}
}
const coloredStringMap: any = {
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity),
};
let coloredSeverity:string = coloredStringMap[severity] || severity;
coloredSeverity = String(coloredSeverity).padEnd(20);
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`);
}
}
const { projectId } = await getProjectSettings();
if (!projectId) {
console.error(ERROR.NO_GCLOUD_PROJECT);
process.exit(-1);
}
if (cmd.open) {
const url = 'https://console.cloud.google.com/logs/viewer?project=' +
`${projectId}&resource=app_script_function`;
console.log(`Opening logs: ${url}`);
open(url);
process.exit(0);
}
getAPICredentials(async () => {
const { data } = await logger.entries.list({
resourceNames: [
`projects/${projectId}`,
],
});
printLogs(data.entries);
});
};
export const run = (functionName:string) => {
getAPICredentials(async () => {
await checkIfOnline();
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
const params = {
scriptId,
function: functionName,
devMode: false,
};
script.scripts.run(params).then(response => {
console.log(response.data);
}).catch(e => {
console.log(e);
});
});
});
};

0 comments on commit de71328

Please sign in to comment.