-
Notifications
You must be signed in to change notification settings - Fork 85
/
ZosFilesAttributes.ts
127 lines (105 loc) · 3.9 KB
/
ZosFilesAttributes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* 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.
*
*/
import * as minimatch from "minimatch";
import { Logger, TextUtils } from "@zowe/imperative";
import { ZosFilesMessages } from "../constants/ZosFiles.messages";
import * as pathUtils from "path";
export enum TransferMode {BINARY, TEXT}
interface IUploadAttributes {
ignore: boolean;
localEncoding?: string;
remoteEncoding?: string;
}
/**
* Attributes for a set of files relating to how they will be uploaded to USS
*/
export class ZosFilesAttributes {
private static MAX_EXPECTED_FIELDS = 3; // eslint-disable-line @typescript-eslint/no-magic-numbers
private static MIN_EXPECTED_FIELDS = 2;
private attributes = new Map<string,IUploadAttributes>();
private basePath: string;
constructor(attributesFileContents: string, basePath?: string) {
this.parse(attributesFileContents);
this.basePath = basePath;
}
public fileShouldBeUploaded(path: string): boolean {
let result = false;
const attributes = this.findLastMatchingAttributes(path);
if (attributes === null) {
result = true;
} else {
result = !attributes.ignore;
}
Logger.getAppLogger().debug("path: " + path + " : " + attributes + " result = " + result);
return result;
}
public getFileTransferMode(path: string): TransferMode {
const attributes = this.findLastMatchingAttributes(path);
if (attributes === null) {
return TransferMode.BINARY;
}
if (attributes.localEncoding === attributes.remoteEncoding) {
return TransferMode.BINARY;
} else {
return TransferMode.TEXT;
}
}
public getRemoteEncoding(path: string): string {
const attributes = this.findLastMatchingAttributes(path);
if (attributes === null) {
return "ISO8859-1";
}
return attributes.remoteEncoding;
}
public getLocalEncoding(path: string): string {
const attributes = this.findLastMatchingAttributes(path);
if (attributes === null) {
return "ISO8859-1";
}
return attributes.localEncoding;
}
private parse(attributesFileContents: string) {
const lines = attributesFileContents.split("\n");
let lineNumber = 0;
lines.forEach((line) => {
lineNumber++;
line = line.trim();
if (line === "" || line.startsWith("#")) {
return;
}
const parts = line.trim().split(/\s+/);
const pattern = parts[0];
const localEncoding = parts[1];
const remoteEncoding = parts[2];
if (parts.length > ZosFilesAttributes.MAX_EXPECTED_FIELDS || parts.length < ZosFilesAttributes.MIN_EXPECTED_FIELDS) {
throw new Error(TextUtils.formatMessage(ZosFilesMessages.invalidAttributesSyntax.message, {lineNumber}));
}
if (localEncoding === "-") {
this.attributes.set(pattern, {ignore: true});
} else {
this.attributes.set(pattern, {ignore: false, localEncoding, remoteEncoding});
}
});
}
private findLastMatchingAttributes(path: string): IUploadAttributes {
let relativePath = path;
if (this.basePath) {
relativePath = pathUtils.relative(this.basePath,path);
}
let result: IUploadAttributes = null;
this.attributes.forEach((attributes, pattern) => {
if (minimatch(relativePath,pattern,{matchBase: true, dot: true })) {
result = attributes;
}
});
return result;
}
}