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

Refactoring Command Handlers with Utils.mapArgumentsToOptions #2438

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
88 changes: 87 additions & 1 deletion packages/cli/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,98 @@
*
*/

import { IImperativeConfig } from "@zowe/imperative";
import { ICommandOptionDefinition, IImperativeConfig } from "@zowe/imperative";
import { Arguments } from "yargs";
import { ZosFilesOptionDefinitions } from "./zosfiles/ZosFiles.options";

/**
* Get the Imperative config object which defines properties of the CLI.
* This allows it to be accessed without calling Imperative.init.
*/
export function getImperativeConfig(): IImperativeConfig {
return require("./imperative");
}

/**
* Map command arguments to options based on their type definition, applying defaults,
* alias mapping, and type transformations.
*
* Special cases:
* - If the option name is "like", it is skipped (the "like" value is handled separately).
* - For the "dsntype" option, if not provided, then the value of the "dataSetType" argument is used.
* - For "dirblk", the value is determined from the "dsorg" argument.
* - For "alcunit", an inline conversion is applied.
*
* @param args - The command arguments.
* @param optionDefinitions - The option definitions from the command definition.
* @returns A mapped options object.
*/
export function mapArgumentsToOptions<T>(
args: Arguments,
optionDefinitions: ICommandOptionDefinition[]
): T {
const options = {} as T;
// Merge command-specific options with global ZosFiles option definitions.
const combinedDefinitions = [...optionDefinitions, ...ZosFilesOptionDefinitions];

combinedDefinitions.forEach(optionDef => {
const { name, type, defaultValue } = optionDef;

// Skip the "like" option as it is handled separately.
if (name === "like") {
return;
}

let argValue: unknown;
// For "dsntype", allow the CLI to provide "dataSetType" as an alias.
if (name === "dsntype") {
if (args[name] !== undefined) {
argValue = args[name];

Check warning on line 58 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L58

Added line #L58 was not covered by tests
} else if (args["dataSetType"] !== undefined) {
argValue = args["dataSetType"];
} else {
argValue = defaultValue;

Check warning on line 62 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L60-L62

Added lines #L60 - L62 were not covered by tests
}
} else {
argValue = args[name] !== undefined ? args[name] : defaultValue;
}

// If no value is found, skip adding this option.
if (argValue === undefined) {
return;
}

// Special handling for "dirblk": derive from dsorg.
if (name === "dirblk") {
const dsorg = args["dsorg"];
options[name as keyof T] = parseInt(

Check warning on line 76 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L75-L76

Added lines #L75 - L76 were not covered by tests
dsorg === "PO" || dsorg === "POE" ? "10" : "0",
10
) as unknown as T[keyof T];
return;

Check warning on line 80 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L80

Added line #L80 was not covered by tests
}

// Special handling for "alcunit": convert allocation unit values.
if (name === "alcunit") {
const alcMap: Record<string, string> = {

Check warning on line 85 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L85

Added line #L85 was not covered by tests
"TRACKS": "TRK",
"CYLINDERS": "CYL"
};
if (typeof argValue === "string") {
options[name as keyof T] = (alcMap[argValue.toUpperCase()] || "TRK") as unknown as T[keyof T];
} else {
options[name as keyof T] = "TRK" as unknown as T[keyof T];

Check warning on line 92 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L91-L92

Added lines #L91 - L92 were not covered by tests
}
return;

Check warning on line 94 in packages/cli/src/Utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/Utils.ts#L94

Added line #L94 was not covered by tests
}

// Apply type-based transformations.
if (type === "number") {
options[name as keyof T] = parseInt(argValue as string, 10) as unknown as T[keyof T];
} else {
options[name as keyof T] = argValue as T[keyof T];
}
});

return options;
}
40 changes: 0 additions & 40 deletions packages/cli/src/zosfiles/create/Create.utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { Create, CreateDataSetTypeEnum, IZosFilesResponse } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { BinaryPDSDefinition } from "./BinaryPDS.definition";

/**
* Handler to create a Binary-PDS data set
Expand All @@ -23,7 +24,7 @@ export default class BinaryPDSHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_BINARY,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, BinaryPDSDefinition.options )
);
}
}
5 changes: 3 additions & 2 deletions packages/cli/src/zosfiles/create/cPds/CPDS.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { IZosFilesResponse, Create, CreateDataSetTypeEnum } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { CPDSDefinition } from "./CPDS.definition";

/**
* Handler to create a C-PDS data set
Expand All @@ -23,7 +24,7 @@ export default class CPDSHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_C,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, CPDSDefinition.options)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { Create, CreateDataSetTypeEnum, IZosFilesResponse } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { ClassicPDSDefinition } from "./ClassicPDS.definition";

/**
* Handler to create a Classic-PDS data set
Expand All @@ -23,7 +24,7 @@ export default class ClassicPDSHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_CLASSIC,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, ClassicPDSDefinition.options)
);
}
}
7 changes: 4 additions & 3 deletions packages/cli/src/zosfiles/create/ds/ds.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { Create, CreateDataSetTypeEnum, IZosFilesResponse } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { DsDefinition } from "./ds.definition";

/**
* Handler to like a data set
Expand All @@ -25,7 +26,7 @@ export default class DataSetHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_BLANK,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, DsDefinition.options)
);

}
Expand All @@ -34,7 +35,7 @@ export default class DataSetHandler extends ZosFilesBaseHandler {
session,
commandParameters.arguments.dataSetName,
commandParameters.arguments.like,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, DsDefinition.options)
);
}

Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/zosfiles/create/pds/Pds.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { IZosFilesResponse, Create, CreateDataSetTypeEnum } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { PdsDefinition } from "./Pds.definition";

/**
* Handler to create a PDS data set
Expand All @@ -23,7 +24,7 @@ export default class PdsHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_PARTITIONED,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, PdsDefinition.options)
);
}
}
5 changes: 3 additions & 2 deletions packages/cli/src/zosfiles/create/ps/Ps.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import { AbstractSession, IHandlerParameters } from "@zowe/imperative";
import { IZosFilesResponse, Create, CreateDataSetTypeEnum } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { generateZosmfOptions } from "../Create.utils";
import { mapArgumentsToOptions } from "../../../Utils";
import { PsDefinition } from "./Ps.definition";

/**
* Handler to create a PDS data set
Expand All @@ -23,7 +24,7 @@ export default class PsHandler extends ZosFilesBaseHandler {
session,
CreateDataSetTypeEnum.DATA_SET_SEQUENTIAL,
commandParameters.arguments.dataSetName,
generateZosmfOptions(commandParameters.arguments)
mapArgumentsToOptions(commandParameters.arguments, PsDefinition.options)
);
}
}
Loading