diff --git a/modelina-cli/.eslintignore b/modelina-cli/.eslintignore index 06a195ae67..0430133d29 100644 --- a/modelina-cli/.eslintignore +++ b/modelina-cli/.eslintignore @@ -1,4 +1,5 @@ /lib /tmp /scripts -/test/helpers/init.js \ No newline at end of file +/test/helpers/init.js +/test/fixtures/generate \ No newline at end of file diff --git a/modelina-cli/src/commands/generate.ts b/modelina-cli/src/commands/generate.ts index 3117df74d4..0091dd2f39 100644 --- a/modelina-cli/src/commands/generate.ts +++ b/modelina-cli/src/commands/generate.ts @@ -14,9 +14,9 @@ export default class Models extends ModelinaCommand { try { document = await readFile(file, 'utf8'); } catch { - throw new Error('Unable to read input file content.'); + throw new Error(`Unable to read input file content: ${file}`); } - + const logger = { info: (message: string) => { this.log(message); diff --git a/modelina-cli/src/helpers/go.ts b/modelina-cli/src/helpers/go.ts index ffd3a58d91..ac09dce990 100644 --- a/modelina-cli/src/helpers/go.ts +++ b/modelina-cli/src/helpers/go.ts @@ -1,7 +1,19 @@ -import { GoFileGenerator } from "@asyncapi/modelina"; +import { GO_DESCRIPTION_PRESET, GO_COMMON_PRESET, GoCommonPresetOptions, GoFileGenerator } from "@asyncapi/modelina"; import { BuilderReturnType } from "./generate"; +import { Flags } from "@oclif/core"; -export const GoOclifFlags = { } +export const GoOclifFlags = { + goIncludeComments: Flags.boolean({ + description: 'Golang specific, if enabled add comments while generating models.', + required: false, + default: false, + }), + goIncludeTags: Flags.boolean({ + description: 'Golang specific, if enabled add tags while generating models.', + required: false, + default: false, + }), +} /** * This function builds all the relevant information for the main generate command @@ -10,13 +22,20 @@ export const GoOclifFlags = { } * @returns */ export function buildGoGenerator(flags: any): BuilderReturnType { - const { packageName } = flags; - + const { packageName, goIncludeComments, goIncludeTags } = flags; + if (packageName === undefined) { throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.'); } - const fileGenerator = new GoFileGenerator(); + const presets = [] + if (goIncludeTags) { + const options: GoCommonPresetOptions = { addJsonTag: true }; + presets.push({ preset: GO_COMMON_PRESET, options }) + } + + if (goIncludeComments) { presets.push(GO_DESCRIPTION_PRESET); } + const fileGenerator = new GoFileGenerator({ presets }); const fileOptions = { packageName };