Skip to content

Commit

Permalink
rules format csv (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner authored Sep 30, 2024
1 parent e524c64 commit dbcb2bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/cli/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,9 @@ utils
utils
.command('rules')
.description('returns technology rules')
.addOption(new Option('--format [string]', 'output format').default('yaml').choices(['yaml', 'json', 'latex']))
.addOption(
new Option('--format [string]', 'output format').default('yaml').choices(['yaml', 'json', 'latex', 'csv'])
)
.action(
hae.exit(async options => {
std.out(await Controller.utils.rules(options))
Expand Down
6 changes: 5 additions & 1 deletion src/controller/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export type RuleOptions = {

export default async function (options: RuleOptions) {
options.format = options.format ?? 'yaml'

const headers = ['component', 'artifact', 'hosting', 'technology', 'weight']

return files.toFormat(Registry.rules, options.format, {
latex: {headers: ['component', 'artifact', 'hosting', 'technology', 'weight']},
latex: {headers},
csv: {headers},
})
}
13 changes: 12 additions & 1 deletion src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as yaml from 'js-yaml'
import lnk from 'lnk'
import _ from 'lodash'
import os from 'os'
import papa from 'papaparse'
import path from 'path'
// TODO: fix import problem
// @ts-ignore
Expand Down Expand Up @@ -163,13 +164,14 @@ export async function loadXML<T>(file: string) {
return (await xml2js.parseStringPromise(loadFile(file) /*, options */)) as T
}

export function toFormat(obj: any, format: string, options: {latex?: LatexOptions} = {}) {
export function toFormat(obj: any, format: string, options: {latex?: LatexOptions; csv?: CSVOptions} = {}) {
if (format === 'yaml') return toYAML(obj)
if (format === 'json') return toJSON(obj)
if (format === 'ini') return toINI(obj)
if (format === 'env') return toENV(obj)
if (format === 'xml') return toXML(obj)
if (format === 'latex') return toLatex(obj, options.latex)
if (format === 'csv') return toCSV(obj, options.csv)

throw new Error(`Format "${format}" not supported`)
}
Expand All @@ -178,6 +180,15 @@ export type LatexOptions = {
headers?: string[]
}

export type CSVOptions = {
headers?: string[]
}

export function toCSV(obj: any, options: CSVOptions = {}) {
assert.isArray(obj)
return papa.unparse(obj, {columns: options.headers})
}

export function toLatex(obj: any, options: LatexOptions = {}) {
assert.isArray(obj)
// TODO: this is dirty
Expand Down

0 comments on commit dbcb2bf

Please sign in to comment.