Skip to content

Commit 8b47346

Browse files
committed
Handling parameters + fix helm pull command
1 parent cf24194 commit 8b47346

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

src/commands/validate.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ export default class Validate extends SleekCommand {
4040

4141
static flags = {
4242
// todo check exclusive flags
43-
file: Flags.string({description: "Path to add-on input file", exclusive: ['helmUrl']}), // or file or URL, full or bits
43+
// todo check Flags.url type
44+
file: Flags.string({description: "Path to add-on input file", exclusive: ['helmUrl'], char:'f'}), // or file or URL, full or bits
4445
helmUrl: Flags.string({description: "Fully qualified URL of the Repo", exclusive: ['file']}), // fully qualified URL of helm repo
45-
helmRepo: Flags.string({description: "Helm repo of the addon", exclusive: ['file', 'helmUrl']}), // construct it piecemeal
46-
protocol: Flags.string({description: "Protocol of the helm hosting to use", exclusive: ['file', 'helmUrl']}),
47-
version: Flags.string({description: "Version of the addon to validate", exclusive: ['file']}),
46+
helmRepo: Flags.string({description: "Helm repo of the addon", exclusive: ['file', 'helmUrl'], char:'r'}), // construct it piecemeal
47+
protocol: Flags.string({description: "Protocol of the helm hosting to use", exclusive: ['file', 'helmUrl'], char:'p'}),
48+
version: Flags.string({description: "Version of the addon to validate", exclusive: ['file'], char:'v'}),
4849
addonName: Flags.string({description: "Name of the addon"}),
49-
extended: Flags.boolean({description: "Run extended validation", hidden: true}), // triggers security and extended checks. NEEDS THE CONTAINER IMAGE FOR THE ADDON
50-
// todo check Flags.url type
50+
extended: Flags.boolean({description: "Run extended validation", hidden: true, char:'e'}), // triggers security and extended checks. NEEDS THE CONTAINER IMAGE FOR THE ADDON
5151
}
5252

5353
static summary = "Validates the addon after pulling it from the helm repository.";
@@ -59,49 +59,49 @@ export default class Validate extends SleekCommand {
5959
// if file is given, validate based on the path
6060
// else, raise error stating one or the other arg/flag should be provided
6161
let repoProtocol, repoUrl, versionTag, addonName;
62-
62+
// uncomment for debugging purposes
63+
// this.log('---')
64+
// this.log(`>> args: ${JSON.stringify(args)}`)
65+
// this.log(`>> flags: ${JSON.stringify(flags)}`)
66+
// this.log('---')
6367
if (flags.addonName) {
6468
addonName = flags.addonName;
6569
}
6670
if (args.helmUrl || flags.helmUrl) {
6771
// JD decompose url, pull and validate
68-
repoUrl = args.helmUrl || flags.helmUrl;
69-
const {protocol, host, port} = url.parse(repoUrl || ""); // todo: validate this works as expected
70-
71-
repoProtocol = protocol;
72-
versionTag = port; // will never be falsy because the flag or arg validation
73-
repoUrl = host; // ensure it works
72+
const repoUrlInput = args.helmUrl || flags.helmUrl;
73+
this.log(`Validating chart from url: ${repoUrlInput}`)
74+
repoProtocol = this.getProtocolFromFullQualifiedUrl(repoUrlInput!);
75+
repoUrl = this.getRepoFromFullChartUri(repoUrlInput!).substring(repoProtocol.length+3); // 3 == '://'.length
76+
versionTag = this.getVersionTagFromChartUri(repoUrlInput!);
7477
} else if (
75-
(args.helmUrl || flags.helmUrl) && (flags.helmRepo || flags.protocol || flags.version) || // base url + flags to override
78+
(args.helmUrl || flags.helmUrl) && (flags.helmRepo || flags.protocol || flags.version) || // base url + flags to override // todo
7679
(flags.helmRepo && flags.protocol && flags.version) // all the url bits
7780
) {
78-
// JD get the base url from
79-
// todo
80-
81+
repoProtocol = flags.protocol;
82+
repoUrl = flags.helmRepo;
83+
versionTag = flags.version;
84+
this.log(`Validating chart from flags: ${repoProtocol}://${repoUrl}:${versionTag}`)
8185
} else if (flags.file) {
82-
// JD
83-
// schema validation
8486
const filePath = flags.file;
87+
this.log(`Validating chart from input file ${filePath}`)
88+
// schema validation
8589
const fileContents = fs.readFileSync(filePath, 'utf8');
8690
const schemaValidator = new SchemaValidationService(this);
8791
const data = await schemaValidator.validateInputFileSchema(fileContents);
88-
const inputDataParsed = data.body as IssueData;
8992
// get url
93+
const inputDataParsed = data.body as IssueData;
9094
const addonData = inputDataParsed.addon;
91-
repoUrl = addonData.helmChartUrl;
9295
repoProtocol = addonData.helmChartUrlProtocol;
93-
versionTag = this.getVersionTagFromUrl(addonData.helmChartUrl);
94-
// pull and validate
96+
repoUrl = this.getRepoFromFullChartUri(addonData.helmChartUrl);
97+
versionTag = this.getVersionTagFromChartUri(addonData.helmChartUrl);
9598
} else {
9699
this.error("Either a Helm URL or a file path should be provided");
97100
}
98101

99102
const helmManager = new HelmManagerService(this);
100-
101103
const chartPath = await helmManager.pullAndUnzipChart(repoUrl!, repoProtocol!, versionTag!, addonName);
102104

103-
// const chartPath = await helmManager.pullAndUnzipChartV2(addonData.name, chartTag, repoUrl, 'oci')
104-
// const charPath = await pullHelmChart(addonData.name, chartTag, repo)
105105
const validatorService = new ChartValidatorService(this, chartPath);
106106
const validatorServiceResp = await validatorService.validate();
107107

@@ -112,20 +112,15 @@ export default class Validate extends SleekCommand {
112112
}
113113
}
114114

115-
private getVersionTagFromUrl(helmChartUrl: string) {
116-
return helmChartUrl.lastIndexOf(':') ? `${helmChartUrl.substring(helmChartUrl.lastIndexOf(':') + 1)}` : '';
115+
private getProtocolFromFullQualifiedUrl(helmChartUrl: string) {
116+
return helmChartUrl?.substring(0,helmChartUrl?.indexOf(':'))
117117
}
118118

119-
private async validateFile(filePath: string): Promise<void> {
120-
const validator = new ChartValidatorService(this, filePath);
121-
await validator.validate();
119+
private getRepoFromFullChartUri(helmChartUrl: string) {
120+
return helmChartUrl.substring(0, helmChartUrl.lastIndexOf(':'));
122121
}
123122

124-
private async validateHelmChart(helmUrl: string, addonName: string): Promise<void> {
125-
const helmManager = new HelmManagerService(this);
126-
127-
const untarLocation = await helmManager.pullAndUnzipChart(helmUrl, addonName);
128-
const validator = new ChartValidatorService(this, untarLocation);
129-
await validator.validate();
123+
private getVersionTagFromChartUri(helmChartUrl: string) {
124+
return helmChartUrl.lastIndexOf(':') ? `${helmChartUrl.substring(helmChartUrl.lastIndexOf(':') + 1)}` : '';
130125
}
131126
}

src/services/helm.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,7 @@ export default class HelmManagerService extends BaseService {
1111
const untarLocation = `./unzipped-${addonName}`;
1212
const pullCmd = `rm -rf ${untarLocation} &&
1313
mkdir ${untarLocation} &&
14-
helm pull ${helmProtocol}://${helmProtocol} ${chartVersionFlag} --untar --untardir ${untarLocation} >/dev/null 2>&1`;
15-
try {
16-
execSync(pullCmd);
17-
} catch (e) {
18-
this.error(`Helm chart pull failed with error ${e}`);
19-
}
20-
return untarLocation;
21-
}
22-
public async pullAndUnzipChartV2(name:string, chartTag:string, url:string): Promise<string> {
23-
const chartVersionFlag = !! chartTag ? `--version ${chartTag}`:''
24-
const untarLocation = `./unzipped-${name}`;
25-
const pullCmd = `rm -rf ${untarLocation} &&
26-
mkdir ${untarLocation} &&
27-
helm pull oci://${url} ${chartVersionFlag} --untar --untardir ${untarLocation} >/dev/null 2>&1`;
14+
helm pull ${helmProtocol}://${helmUrl} ${chartVersionFlag} --untar --untardir ${untarLocation} >/dev/null`;
2815
try {
2916
execSync(pullCmd);
3017
} catch (e) {

0 commit comments

Comments
 (0)