Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add architect push cmd #18

Merged
merged 6 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
.idea
architect_services
oclif.manifest.json
*.env
104 changes: 96 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
"@oclif/errors": "^1.2.2",
"@oclif/plugin-help": "^2.1.2",
"@types/auth0": "^2.9.11",
"@types/dotenv": "^6.1.1",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^7.0.0",
"@types/joi": "^14.3.3",
"@types/keytar": "^4.4.0",
"@types/listr": "^0.14.0",
"@types/request": "^2.48.1",
"auth0": "^2.17.0",
"chalk": "^2.4.1",
"dotenv": "^8.0.0",
"execa": "^1.0.0",
"fs-extra": "^8.0.1",
"inquirer": "^6.2.0",
"joi": "^14.3.1",
"keytar": "^4.6.0",
"listr": "^0.14.3",
"oclif": "^1.12.5",
Expand Down
52 changes: 52 additions & 0 deletions src/app-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as dotenv from 'dotenv';
import * as Joi from 'joi';

const CONFIG_SCHEMA: Joi.ObjectSchema = Joi.object({
OAUTH_DOMAIN: Joi
.string()
.hostname()
.default('architect.auth0.com'),
OAUTH_CLIENT_ID: Joi
.string()
.default('X9U08B2hg6QEmRUIFKZoCSqgNBmAM6aU'),
DEFAULT_REGISTRY_HOST: Joi
.string()
.uri()
.default('registry.architect.io'),
API_HOST: Joi
.string()
.uri()
.default('https://api.architect.io')
});

const validate_config = (
input_config: { [key: string]: any },
): { [key: string]: string } => {
const { error, value: validatedEnvConfig } = Joi.validate(
input_config,
CONFIG_SCHEMA,
{ stripUnknown: true },
);
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
return validatedEnvConfig;
};

export class AppConfig {
readonly oauth_domain: string;
readonly oauth_client_id: string;
readonly default_registry_host: string;
readonly api_host: string;

constructor() {
// Load environment params from a .env file if found
dotenv.config();

const app_env = validate_config(process.env);
this.oauth_domain = app_env.OAUTH_DOMAIN;
this.oauth_client_id = app_env.OAUTH_CLIENT_ID;
this.default_registry_host = app_env.DEFAULT_REGISTRY_HOST;
this.api_host = app_env.API_HOST;
}
}
72 changes: 68 additions & 4 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import Command from '@oclif/command';
import * as Config from '@oclif/config';
import * as keytar from 'keytar';
import * as Listr from 'listr';
import * as request from 'request';
import * as url from 'url';

export default abstract class extends Command {
import { AppConfig } from './app-config';

export default abstract class ArchitectCommand extends Command {
static async tasks(this: any, argv?: string[], opts?: Config.LoadOptions): Promise<Listr.ListrTask[]> {
if (!argv) argv = process.argv.slice(2);
const config = await Config.load(opts || module.parent && module.parent.parent && module.parent.parent.filename || __dirname);
let cmd = new this(argv, config);
return cmd._tasks(argv);
}
protected static app_config: AppConfig;
protected static architect: ArchitectClient;

app_config!: AppConfig;
architect!: ArchitectClient;

async init() {
this.architect = new ArchitectClient();
if (!ArchitectCommand.app_config) {
ArchitectCommand.app_config = new AppConfig();
ArchitectCommand.architect = new ArchitectClient(ArchitectCommand.app_config.api_host);
}
this.app_config = ArchitectCommand.app_config;
this.architect = ArchitectCommand.architect;
}

async tasks(): Promise<Listr.ListrTask[]> { throw Error('Not implemented'); }

async _tasks(): Promise<Listr.ListrTask[] | undefined> {
let err: Error | undefined;
try {
// remove redirected env var to allow subsessions to run autoupdated client
delete process.env[this.config.scopedEnvVarKey('REDIRECTED')];

await this.init();
return await this.tasks();
} catch (e) {
err = e;
await this.catch(e);
} finally {
await this.finally(err);
}
}

styled_json(obj: object) {
Expand All @@ -16,8 +53,34 @@ export default abstract class extends Command {
}
}

class UserEntity {
readonly access_token: string;
readonly username: string;

constructor(partial: { [key: string]: string }) {
this.access_token = partial.access_token;
this.username = partial['https://architect.io/username'];
}
}

class ArchitectClient {
private readonly domain = 'https://api.architect.io';
private readonly domain: string;

constructor(domain: string) {
this.domain = domain;
}

async getUser() {
const credentials = await keytar.findCredentials('architect.io');
if (credentials.length === 0) {
throw Error('denied: `architect login` required');
}
const user = new UserEntity(JSON.parse(credentials[0].password));
if (!user.username) {
throw Error('denied: `architect login` required');
}
return user;
}

async get(path: string) {
return this.request('GET', path);
Expand All @@ -40,7 +103,8 @@ class ArchitectClient {
if (credentials.length === 0) {
throw Error('denied: `architect login` required');
}
const access_token = JSON.parse(credentials[0].password).access_token;
const user = await this.getUser();
const access_token = user.access_token;

const options = {
url: url.resolve(this.domain, path),
Expand Down
Loading