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

fix(cli): validate version #1203

Merged
merged 5 commits into from
Jul 14, 2023
Merged
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
16 changes: 10 additions & 6 deletions src/cli/install-tool/install-tool-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { chmod, chown, stat, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { injectable } from 'inversify';
import type { EnvService, PathService } from '../services';
import { fileRights, logger } from '../utils';
import { fileRights, isValid, logger } from '../utils';

export interface ShellWrapperConfig {
name: string;
name?: string;
srcDir: string;
exports?: string;
}
Expand All @@ -32,6 +32,10 @@ export abstract class InstallToolBaseService {

abstract link(version: string): Promise<void>;

needsPrepare(): boolean {
return true;
}

test(_version: string): Promise<void> {
return Promise.resolve();
}
Expand All @@ -40,16 +44,16 @@ export abstract class InstallToolBaseService {
return this.name;
}

validate(_version: string): boolean {
return true;
validate(version: string): boolean {
return isValid(version);
}

protected async shellwrapper({
name,
srcDir,
exports,
}: ShellWrapperConfig): Promise<void> {
const tgt = join(this.pathSvc.binDir, name);
const tgt = join(this.pathSvc.binDir, name ?? this.name);

let content = `#!/bin/bash

Expand All @@ -62,7 +66,7 @@ export abstract class InstallToolBaseService {
content += `export ${exports}\n`;
}

content += `${srcDir}/${name} "$@"\n`;
content += `${srcDir}/${name ?? this.name} "$@"\n`;

await writeFile(tgt, content, { encoding: 'utf8' });
await this.setOwner({ file: tgt });
Expand Down
2 changes: 1 addition & 1 deletion src/cli/install-tool/install-tool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class InstallToolService {
return;
}

if (!(await this.pathSvc.findToolPath(tool))) {
if (toolSvc.needsPrepare() && !(await this.pathSvc.findToolPath(tool))) {
logger.debug({ tool }, 'tool not prepared');
const res = await prepareTools([tool], dryRun);
if (res) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tools/dart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class InstallDartService extends InstallToolBaseService {
override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

await this.shellwrapper({ name: 'dart', srcDir: src });
await this.shellwrapper({ srcDir: src });
}

override async test(_version: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tools/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class InstallDockerService extends InstallToolBaseService {
override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

await this.shellwrapper({ name: 'docker', srcDir: src });
await this.shellwrapper({ srcDir: src });
}

override async test(_version: string): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions src/cli/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import semver from 'semver';
import { type StrictValidator, makeValidator } from 'typanion';

export function isValid(version: string): boolean {
return semver.valid(version) !== null;
}

export function validateSemver(): StrictValidator<string, string> {
return makeValidator<string, string>({
test: (value, state): value is string => {
Expand Down