Skip to content

Commit

Permalink
refactor: cleanup [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Faber committed Aug 30, 2021
1 parent 9bb004c commit ca7918d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 73 deletions.
19 changes: 10 additions & 9 deletions src/ci-tests.ts
Expand Up @@ -2,15 +2,16 @@
import { existsSync, symlinkSync } from 'fs'
import { fileURLToPath } from 'url'
import yargs, { Argv } from 'yargs'
import { hf } from './cmd/hf'
import { validateTemplates } from './cmd/validate-templates'
import { validateValues } from './cmd/validate-values'
import { x } from './cmd/x'
import { hf } from './common/hf'
import { prepareEnvironment } from './common/setup'
import {
BasicArguments,
getFilename,
getParsedArgs,
logLevelString,
OtomiDebugger,
rootDir,
setParsedArgs,
Expand Down Expand Up @@ -47,7 +48,13 @@ export const ciTests = async (): Promise<void> => {
await validateValues()

debug.info('Running hf lint')
await hf({ ...argv, args: ['lint'] })
await hf(
{
logLevel: logLevelString(),
args: ['lint'],
},
{ streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)

debug.info('Running validate-templates')
await validateTemplates()
Expand All @@ -65,13 +72,7 @@ export const module = {
setParsedArgs(argv)
await prepareEnvironment({ skipAllPreChecks: true })
setup()

try {
await ciTests()
} catch (error) {
debug.error(error)
process.exit(1)
}
await ciTests()
},
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/apply.ts
Expand Up @@ -2,7 +2,7 @@ import { mkdirSync, rmdirSync, writeFileSync } from 'fs'
import { Argv, CommandModule } from 'yargs'
import { $, cd, nothrow } from 'zx'
import { env } from '../common/envalid'
import { hf, hfStream, hfValues } from '../common/hf'
import { hf, hfValues } from '../common/hf'
import { cleanupHandler, prepareEnvironment } from '../common/setup'
import {
getFilename,
Expand Down Expand Up @@ -97,14 +97,14 @@ export const apply = async (): Promise<void> => {
}
debug.info('Start apply')
const skipCleanup = argv.skipCleanup ? '--skip-cleanup' : ''
await hfStream(
await hf(
{
fileOpts: argv.file,
labelOpts: argv.label,
logLevel: logLevelString(),
args: ['apply', '--skip-deps', skipCleanup],
},
{ trim: true, streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
{ streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/destroy.ts
@@ -1,7 +1,7 @@
import { existsSync, unlinkSync, writeFileSync } from 'fs'
import { Argv } from 'yargs'
import { $, nothrow } from 'zx'
import { hf, hfStream } from '../common/hf'
import { hf } from '../common/hf'
import { cleanupHandler, prepareEnvironment } from '../common/setup'
import { getFilename, getParsedArgs, logLevelString, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
import { Arguments, helmOptions } from '../common/yargs-opts'
Expand Down Expand Up @@ -81,14 +81,14 @@ export const destroy = async (): Promise<void> => {
if (!argv.label && !argv.file) {
destroyAll()
} else {
await hfStream(
await hf(
{
fileOpts: argv.file,
labelOpts: argv.label,
logLevel: logLevelString(),
args: 'destroy',
},
{ trim: true, streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
{ streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/diff.ts
@@ -1,6 +1,6 @@
import { Argv } from 'yargs'
import { Arguments, decrypt } from '../common/crypt'
import { hfStream } from '../common/hf'
import { hf } from '../common/hf'
import { prepareEnvironment } from '../common/setup'
import { getFilename, getParsedArgs, logLevelString, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
import { helmOptions } from '../common/yargs-opts'
Expand All @@ -13,14 +13,14 @@ export const diff = async (): Promise<ProcessOutputTrimmed> => {
const argv: Arguments = getParsedArgs()
await decrypt(...(argv.files ?? []))
debug.info('Start Diff')
const res = await hfStream(
const res = await hf(
{
fileOpts: argv.file as string[],
labelOpts: argv.label as string[],
logLevel: logLevelString(),
args: ['diff', '--skip-deps'],
},
{ trim: true, streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
{ streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)
return new ProcessOutputTrimmed(res)
}
Expand Down
32 changes: 11 additions & 21 deletions src/cmd/hf.ts
@@ -1,7 +1,7 @@
import { Argv } from 'yargs'
import { hfStream } from '../common/hf'
import { hf as hfCommon } from '../common/hf'
import { prepareEnvironment } from '../common/setup'
import { getFilename, getParsedArgs, logLevelString, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
import { getFilename, logLevelString, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
import { Arguments as HelmArgs, helmOptions } from '../common/yargs-opts'

interface Arguments extends HelmArgs {
Expand All @@ -11,24 +11,6 @@ interface Arguments extends HelmArgs {
const cmdName = getFilename(import.meta.url)
const debug: OtomiDebugger = terminal(cmdName)

export const hf = async (inArgs?: Arguments): Promise<void> => {
const argv: Arguments = inArgs ?? getParsedArgs()
try {
await hfStream(
{
fileOpts: argv.file,
labelOpts: argv.label,
logLevel: logLevelString(),
args: argv.args ?? [],
},
{ trim: true, streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)
} catch (error) {
debug.error(error.stderr)
process.exit(1)
}
}

export const module = {
command: `${cmdName} [args..]`,
describe: undefined,
Expand All @@ -37,7 +19,15 @@ export const module = {
handler: async (argv: Arguments): Promise<void> => {
setParsedArgs(argv)
await prepareEnvironment()
await hf()
await hfCommon(
{
fileOpts: argv.file,
labelOpts: argv.label,
logLevel: logLevelString(),
args: argv.args ?? [],
},
{ streams: { stdout: debug.stream.log, stderr: debug.stream.error } },
)
},
}

Expand Down
1 change: 0 additions & 1 deletion src/cmd/lint.ts
Expand Up @@ -18,7 +18,6 @@ export const lint = async (): Promise<void> => {
args: ['lint', '--skip-deps'],
},
{
trim: true,
streams: {
stdout: debug.stream.log,
stderr: debug.stream.error,
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/sync.ts
@@ -1,5 +1,5 @@
import { Argv } from 'yargs'
import { hfStream } from '../common/hf'
import { hf } from '../common/hf'
import { prepareEnvironment } from '../common/setup'
import { getFilename, getParsedArgs, logLevelString, OtomiDebugger, setParsedArgs, terminal } from '../common/utils'
import { Arguments, helmOptions } from '../common/yargs-opts'
Expand All @@ -11,14 +11,14 @@ export const sync = async (): Promise<void> => {
const argv: Arguments = getParsedArgs()
debug.info('Start sync')
const skipCleanup = argv.skipCleanup ? '--skip-cleanup' : ''
await hfStream(
await hf(
{
fileOpts: argv.file,
labelOpts: argv.label,
logLevel: logLevelString(),
args: ['sync', '--skip-deps', skipCleanup],
},
{ trim: true, streams: { stdout: debug.stream.log } },
{ streams: { stdout: debug.stream.log } },
)
}

Expand Down
43 changes: 14 additions & 29 deletions src/common/hf.ts
@@ -1,5 +1,5 @@
import { load } from 'js-yaml'
import { Readable, Transform } from 'stream'
import { Transform } from 'stream'
import { $, ProcessOutput, ProcessPromise } from 'zx'
import { env } from './envalid'
import { asArray, getParsedArgs, logLevels, terminal } from './utils'
Expand Down Expand Up @@ -65,68 +65,53 @@ const hfCore = (args: HFParams): ProcessPromise<ProcessOutput> => {
return proc
}

const hfTrimmed = (args: HFParams): { proc: ProcessPromise<ProcessOutput>; stdout: Readable; stderr: Readable } => {
export type HFOptions = {
streams?: Streams
}

export const hf = async (args: HFParams, opts?: HFOptions): Promise<ProcessOutputTrimmed> => {
// we do some transformations to strip out unwanted noise, which helmfile generates because reasons
const transform = new Transform({
transform(chunk, encoding, next) {
const str = chunk.toString()
const transformation = trimHFOutput(str).trim()
// if (str.indexOf('basePath=') > -1) console.debug('transformation:', `${str} > ${transformation}`)
if (transformation && transformation.length > 0) this.push(transformation)
next()
},
})
const proc: ProcessPromise<ProcessOutput> = hfCore(args)
return {
const output = {
stdout: proc.stdout,
stderr: proc.stderr.pipe(transform),
proc,
}
}

export type HFOptions = {
trim?: boolean
streams?: Streams
}

export const hfStream = (args: HFParams, opts: HFOptions = {}): ProcessPromise<ProcessOutput> => {
const trimmedOutput = hfTrimmed(args)
if (opts?.streams?.stdout) trimmedOutput.stdout.pipe(opts.streams.stdout, { end: false })
if (opts?.streams?.stderr) trimmedOutput.stderr.pipe(opts.streams.stderr, { end: false })
return trimmedOutput.proc
}

export const hf = async (args: HFParams, opts?: HFOptions): Promise<ProcessOutputTrimmed> => {
return new ProcessOutputTrimmed(await hfStream(args, opts))
if (opts?.streams?.stdout) output.stdout.pipe(opts.streams.stdout, { end: false })
if (opts?.streams?.stderr) output.stderr.pipe(opts.streams.stderr, { end: false })
return new ProcessOutputTrimmed(await output.proc)
}

export type ValuesOptions = {
asString?: boolean
replacePath?: boolean
skipCache?: boolean
}

export const values = async (opts?: ValuesOptions): Promise<any | string> => {
export const values = async (opts?: ValuesOptions): Promise<Record<string, any>> => {
if (!opts?.skipCache) {
if (opts?.replacePath && value.rp) {
if (opts?.asString) return value.rp
return value.rp
}
if (value.clean) {
if (opts?.asString) return value.clean
return value.clean
}
}
const output = await hf(
{ fileOpts: `${process.cwd()}/helmfile.tpl/helmfile-dump.yaml`, args: 'build' },
{ trim: true },
)
const output = await hf({ fileOpts: `${process.cwd()}/helmfile.tpl/helmfile-dump.yaml`, args: 'build' })
value.clean = (load(output.stdout) as any).renderedvalues
value.rp = (load(replaceHFPaths(output.stdout)) as any).renderedvalues
if (opts?.asString) return opts && opts.replacePath ? replaceHFPaths(output.stdout) : output.stdout
return opts?.replacePath ? value.rp : value.clean
}

export const hfValues = async (skipCache = false): Promise<any> => {
export const hfValues = async (skipCache = false): Promise<Record<string, any>> => {
return values({ replacePath: true, skipCache })
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/setup.ts
Expand Up @@ -23,7 +23,7 @@ const checkKubeContext = async (): Promise<void> => {
const d = terminal('checkKubeContext')
d.info('Validating kube context')

const values: any = await hfValues()
const values = await hfValues()
const currentContext = (await $`kubectl config current-context`).stdout.trim()
const k8sContext = values?.cluster?.k8sContext
d.debug('currentContext: ', currentContext)
Expand Down

0 comments on commit ca7918d

Please sign in to comment.