diff --git a/src/app.ts b/src/app.ts index ad8b05b..14bfd09 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,6 +2,7 @@ import { readFileSync } from 'fs' import { exit } from '~/utils' import { StaxConfig, SetupOptions, FindContainerOptions } from '~/types' import Staxfile from '~/staxfile' +import icons from '~/icons' import docker from '~/docker' import Container from '~/container' import settings from '~/settings' @@ -48,14 +49,20 @@ export default class App { const container = Container.find(context, containerName, { mustExist: true }) if (!container) { - console.warn(`🤷 App '${containerName}@${context}' not found`) + console.warn(`${icons.warning} App '${containerName}@${context}' not found`) return null } return new App(containerName, [container]) } + static exists(context: string, name: string): boolean { + return this.all(context).find(app => app.name == name) != null + } + static async setup(config: StaxConfig, options: SetupOptions = {}) { const staxfile = new Staxfile(config) + console.log(staxfile.config) + process.exit() const composeFile = staxfile.compile() if (!composeFile) @@ -119,10 +126,18 @@ export default class App { this.containers.forEach(async container => container.runHooks()) } - addAlias(alias) { + addAlias(alias: string) { const aliases = settings.read('aliases') || {} aliases[alias] = this.name settings.write('aliases', aliases) } + + async duplicate(newName: string) { + if (this.constructor.exists(this.context, newName)) + exit(1, `${icons.error} An app named '${newName}' already exists.`) + + const config = { ...this.primary.config, app: newName } as unknown as StaxConfig + return this.constructor.setup(config) + } } diff --git a/src/cli.ts b/src/cli.ts index 0b883df..9b3ac44 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -48,6 +48,12 @@ program.command('down') target.down() }) +program.command('duplicate') + .argument('', 'Name of application') + .argument('', 'Name of new application') + .description('Duplicate an application') + .action(async (name, newName) => stax.find(name).duplicate(newName)) + program.command('edit') .argument('', 'Name of application') .description(`Open application in a vscode based editor`) @@ -142,7 +148,7 @@ program.command('setup') .option('-s, --staxfile ', 'Staxfile to use for setup') .option('-i, --inspect', 'Show the compose file') .description('Setup an application') - .action(async (location, options) => stax.setup({ source: location, ...options, ...overrides }, options)) + .action(async (location, options) => stax.setup({ source: location, ...options }, { ...options, overrides: overrides })) program.command('shell') .alias('sh') diff --git a/src/icons.ts b/src/icons.ts index dedc743..dc69c08 100644 --- a/src/icons.ts +++ b/src/icons.ts @@ -11,6 +11,7 @@ const icons = { unknown: '❔', local: '📁', remote: '🌐', + error: '😡', } export default icons diff --git a/src/stax.ts b/src/stax.ts index 2a19a6a..350f0c6 100644 --- a/src/stax.ts +++ b/src/stax.ts @@ -16,7 +16,14 @@ export default class Stax { list(this.apps()) } - async setup(config: StaxConfig, options: { inspect?: boolean } = { inspect: false }) { + async setup(config: StaxConfig, options: { inspect?: boolean, overrides?: Record } = { inspect: false }) { + // if (options.overrides) { + // for (const [ key, value ] of Object.entries(options.overrides)) { + // console.log(key, value) + // } + // } + // console.log(config) + // process.exit() App.setup({ context: this.context, ...config } as unknown as StaxConfig, options) } diff --git a/src/utils.ts b/src/utils.ts index e9d597b..d1295da 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -81,7 +81,7 @@ export function parseAndRemoveWildcardOptions(args: string[], startsWith: string const staxVars: Record = {} const filteredArgs = args.filter(arg => { if (arg.startsWith(startsWith)) { - const [key, value] = arg.slice(startsWith.length).split('=') + const [key, value] = arg.slice(2).split('=') staxVars[key] = value return false }