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

[v3] Zos files attributes #2319

Merged
merged 17 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 4 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the Zowe CLI package will be documented in this file.

## Recent Changes

- Enhancement: Added optional `--attributes` flag to `zowe zos-files upload file-to-uss` to allow passing a .zosattributes file path for upload encoding format. [#2319] (https://github.com/zowe/zowe-cli/pull/2319)

## `8.3.0`

- Enhancement: Issue the `zowe files search data-sets` command with the new `encoding` option to use a different code page when searching data set contents. [#2161](https://github.com/zowe/zowe-cli/issues/2161)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import { Upload } from "@zowe/zos-files-for-zowe-sdk";
import { UNIT_TEST_ZOSMF_PROF_OPTS } from "../../../../../../../__tests__/__src__/TestConstants";
import { ZosFilesAttributes } from "@zowe/zos-files-for-zowe-sdk";

describe("Upload file-to-uss handler", () => {
describe("process method", () => {
Expand All @@ -29,7 +30,7 @@ describe("Upload file-to-uss handler", () => {
let fakeSession = null;

// Mock the submit JCL function
Upload.fileToUssFile = jest.fn(async (session, file, name, options = {}) => {
Upload.uploadFile = jest.fn(async (session, file, name, options = {}) => {
fakeSession = session;
return {
success: true,
Expand Down Expand Up @@ -79,20 +80,113 @@ describe("Upload file-to-uss handler", () => {
}

expect(error).toBeUndefined();
expect(Upload.fileToUssFile).toHaveBeenCalledTimes(1);
expect(Upload.fileToUssFile).toHaveBeenCalledWith(fakeSession, inputfile, USSFileName, {
expect(Upload.uploadFile).toHaveBeenCalledTimes(1);
expect(Upload.uploadFile).toHaveBeenCalledWith(fakeSession, inputfile, USSFileName, {
binary: undefined,
encoding: undefined,
task: {
percentComplete: 0,
stageName: 0,
statusMessage: "Uploading USS file"
}
},
filesMap: null,
includeHidden: undefined,
maxConcurrentRequests: undefined,
responseTimeout: undefined
});
expect(jsonObj).toMatchSnapshot();
expect(apiMessage).toMatchSnapshot();
expect(logMessage).toMatchSnapshot();
});
it("should upload a file to a USS if requested - zosattributes file", async () => {
// Require the handler and create a new instance
const handlerReq = require("../../../../../src/zosfiles/upload/ftu/FileToUSS.handler");
const handler = new handlerReq.default();
const inputfile = "test-file";
const USSFileName = "testing";
let zosAttributes: any;

let error;
let apiMessage = "";
let jsonObj;
let logMessage = "";
let fakeSession = null;

jest.spyOn(ZosFilesAttributes, "loadFromFile").mockImplementation(() => {
zosAttributes = Object.create(ZosFilesAttributes.prototype);
zosAttributes.attributes = new Map([
['*.json', { ignore: true }],
['*.bin', { ignore: false, localEncoding: 'binary', remoteEncoding: 'binary' }],
['*.jcl', { ignore: false, localEncoding: 'IBM-1047', remoteEncoding: 'IBM-1047' }],
['*.md', { ignore: false, localEncoding: 'UTF-8', remoteEncoding: 'UTF-8' }],
['*.txt', { ignore: false, localEncoding: 'UTF-8', remoteEncoding: 'IBM-1047' }]
]);
zosAttributes.basePath = undefined;
return zosAttributes;
});
Upload.uploadFile = jest.fn(async (session, file, name, options = {}) => {
fakeSession = session;
return {
success: true,
commandResponse: "uploaded",
apiResponse: [
{ success: true, from: inputfile, to: USSFileName }
]
};
});
try {
await handler.process({
arguments: {
$0: "fake",
_: ["fake"],
inputfile,
USSFileName,
...UNIT_TEST_ZOSMF_PROF_OPTS
},
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
apiMessage = setMsgArgs;
}),
setObj: jest.fn((setObjArgs) => {
jsonObj = setObjArgs;
})
},
console: {
log: jest.fn((logArgs) => {
logMessage += "\n" + logArgs;
})
},
progress: {
startBar: jest.fn(() => {
// do nothing
}),
endBar: jest.fn(() => {
// do nothing
})
}
}
} as any);
} catch (e) {
error = e;
}
expect(error).toBeUndefined();
expect(Upload.uploadFile).toHaveBeenCalledTimes(1);
expect(Upload.uploadFile).toHaveBeenCalledWith(fakeSession, inputfile, USSFileName, {
attributes: zosAttributes,
binary: undefined,
includeHidden: undefined,
maxConcurrentRequests: undefined,
responseTimeout: undefined,
task: {
percentComplete: 0,
stageName: 0,
statusMessage: "Uploading USS file"
}
});
expect(jsonObj).toMatchSnapshot();
expect(apiMessage).toMatchSnapshot();
expect(logMessage).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ Array [
"name": "encoding",
"type": "string",
},
Object {
"aliases": Array [
"attrs",
],
"conflictsWith": Array [
"ascii-files, binary-files",
],
"description": "Path of an attributes file to control how files are uploaded.",
"name": "attributes",
"type": "string",
},
]
`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Upload file-to-uss handler process method should upload a file to a USS if requested - zosattributes file 1`] = `
Object {
"apiResponse": Array [
Object {
"from": "test-file",
"success": true,
"to": "testing",
},
],
"commandResponse": "uploaded",
"success": true,
}
`;

exports[`Upload file-to-uss handler process method should upload a file to a USS if requested - zosattributes file 2`] = `""`;

exports[`Upload file-to-uss handler process method should upload a file to a USS if requested - zosattributes file 3`] = `
"
- 
success: true
from:  test-file
to:  testing


uploaded"
`;

exports[`Upload file-to-uss handler process method should upload a file to a uss if requested 1`] = `
Object {
"apiResponse": Array [
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/zosfiles/upload/ftu/FileToUSS.definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const FileToUSSDefinition: ICommandDefinition = {
],
options: [
UploadOptions.binary,
UploadOptions.encoding
UploadOptions.encoding,
UploadOptions.attributes
],
examples: [
{
Expand Down
91 changes: 71 additions & 20 deletions packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,93 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
Comment on lines -2 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious how this happens since running npm run build seems to take those extra spaces away 🤔

This makes me wonder if we should implement prettier (or similar tools) as pre-commit hooks (like husky) 😋

👀 @t1m0thyj, any thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying that this needs to be updated as part of this PR 😋


import { AbstractSession, IHandlerParameters, ITaskWithStatus, TaskStage, TextUtils } from "@zowe/imperative";
import {
AbstractSession,
IHandlerParameters,
ITaskWithStatus,
TaskStage,
TextUtils,
} from "@zowe/imperative";
import { Upload, IZosFilesResponse } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesBaseHandler } from "../../ZosFilesBase.handler";
import { IUploadOptions } from "@zowe/zos-files-for-zowe-sdk";
import { ZosFilesAttributes } from "@zowe/zos-files-for-zowe-sdk";
jace-roell marked this conversation as resolved.
Show resolved Hide resolved
import { IUploadMap } from "@zowe/zos-files-for-zowe-sdk";
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

/**
* Handler to upload content from a local file to a USS file
* @export
*/
export default class FileToUSSHandler extends ZosFilesBaseHandler {
public async processWithSession(commandParameters: IHandlerParameters,
session: AbstractSession): Promise<IZosFilesResponse> {
public async processWithSession(
commandParameters: IHandlerParameters,
session: AbstractSession
): Promise<IZosFilesResponse> {
const task: ITaskWithStatus = {
percentComplete: 0,
statusMessage: "Uploading USS file",
stageName: TaskStage.IN_PROGRESS
stageName: TaskStage.IN_PROGRESS,
};
commandParameters.response.progress.startBar({ task });

const response = await Upload.fileToUssFile(session, commandParameters.arguments.inputfile,
commandParameters.arguments.USSFileName, {
binary: commandParameters.arguments.binary,
encoding: commandParameters.arguments.encoding,
task,
responseTimeout: commandParameters.arguments.responseTimeout
});
const uploadOptions: IUploadOptions = {
binary: commandParameters.arguments.binary,
maxConcurrentRequests:
commandParameters.arguments.maxConcurrentRequests,
task: task,
responseTimeout: commandParameters.arguments.responseTimeout,
includeHidden: commandParameters.arguments.includeHidden,
};

const attributes = ZosFilesAttributes.loadFromFile(
commandParameters.arguments.attributes,
commandParameters.arguments.inputDir
);
if (attributes != null) {
uploadOptions.attributes = attributes;
} else {
uploadOptions.filesMap = this.buildFilesMap(commandParameters);
}

const response = await Upload.uploadFile(
session,
commandParameters.arguments.inputfile,
commandParameters.arguments.USSFileName,
uploadOptions
);

const formatMessage = TextUtils.prettyJson(response.apiResponse);
commandParameters.response.console.log(formatMessage);
return response;
}
private buildFilesMap(commandParameters: IHandlerParameters) {
let filesMap: IUploadMap = null;

// checking if binary-files or ascii-files are used, and update filesMap argument
if (commandParameters.arguments.binaryFiles) {
jace-roell marked this conversation as resolved.
Show resolved Hide resolved
filesMap = {

Check warning on line 76 in packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts#L76

Added line #L76 was not covered by tests
binary: true,
fileNames: commandParameters.arguments.binaryFiles
.split(",")
.map((fileName: string) => fileName.trim()),

Check warning on line 80 in packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts#L80

Added line #L80 was not covered by tests
};
}
if (commandParameters.arguments.asciiFiles) {
filesMap = {

Check warning on line 84 in packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts#L84

Added line #L84 was not covered by tests
binary: false,
fileNames: commandParameters.arguments.asciiFiles
.split(",")
.map((fileName: string) => fileName.trim()),

Check warning on line 88 in packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/zosfiles/upload/ftu/FileToUSS.handler.ts#L88

Added line #L88 was not covered by tests
};
}
return filesMap;
}
}
4 changes: 4 additions & 0 deletions packages/zosfiles/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the Zowe z/OS files SDK package will be documented in this file.

## Recent Changes

- Enhancement: Added optional `--attributes` flag to `zowe zos-files upload file-to-uss` to allow passing a .zosattributes file path for upload encoding format. [#2319] (https://github.com/zowe/zowe-cli/pull/2319)

## `8.2.0`

- Enhancement: Added an optional `continueSearch` function to the `ISearchOptions` interface. After a data set listing is completed, the new function is called with the list of data sets about to be searched. This allows the extender or end users to continue with the search or cancel it. [#2300](https://github.com/zowe/zowe-cli/pull/2300)
Expand Down
Loading
Loading