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

rules format csv #423

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
rules format csv
milesstoetzner committed Sep 30, 2024
commit b8b77dd483bce768546b729069f504fb4407780d
4 changes: 3 additions & 1 deletion src/cli/program.ts
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 5 additions & 1 deletion src/controller/utils/rules.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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`)
}
@@ -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