Skip to content

Commit

Permalink
chore: adding error habdling if tag not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
florianow committed Mar 13, 2024
1 parent 02778a8 commit ae3dc06
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/api/git/GitCliFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ export class GitCliFacade {
await this.processRunner.run(["git", "pull"], { cwd: repoDir });
}

async getLatestTag(repoDir: string): Promise<string> {
const result = await this.processRunner.run(["git", "describe", "--tags", "--abbrev=0"], { cwd: repoDir });
return result.stdout.trim();
async getLatestTag(repoDir: string): Promise<string> {
const result = await this.processRunner.run([
"git",
"describe",
"--tags",
"--abbrev=0",
], { cwd: repoDir });
return result.stdout.trim();
}

async checkout(repoDir: string, tagValue: string) {
await this.processRunner.run(["git", "checkout", tagValue], { cwd: repoDir });
await this.processRunner.run(["git", "checkout", tagValue], {
cwd: repoDir,
});
}
/**
* Checks if the given dir is a .git repo dir.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/compliance/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function registerImportCmd(program: TopLevelCommand) {
const factory = new CliApiFacadeFactory(logger);
const git = factory.buildGit();
const config = new CollieConfig(collie, logger);
const hub = new CollieHub(git, collie, config);
const hub = new CollieHub(git, collie, logger, config);

if (opts.clean) {
logger.progress("cleaning local cache of collie hub");
Expand Down
9 changes: 7 additions & 2 deletions src/commands/config/get.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { GlobalCommandOptions } from "../GlobalCommandOptions.ts";
import { TopLevelCommand } from "../TopLevelCommand.ts";
import { CollieConfig, CollieConfigProperties } from "../../model/CollieConfig.ts";
import {
CollieConfig,
CollieConfigProperties,
} from "../../model/CollieConfig.ts";
import { Logger } from "../../cli/Logger.ts";
import { CollieRepository } from "../../model/CollieRepository.ts";
import { CLI } from "../../info.ts";
Expand All @@ -18,7 +21,9 @@ export function registerGetCmd(program: TopLevelCommand) {
const repo = await CollieRepository.load();
const logger = new Logger(repo, opts);
const config = new CollieConfig(repo, logger);
const value = config.getProperty(property as keyof CollieConfigProperties);
const value = config.getProperty(
property as keyof CollieConfigProperties,
);
console.log(value);
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kit/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function registerImportCmd(program: TopLevelCommand) {
const factory = new CliApiFacadeFactory(logger);
const git = factory.buildGit();
const config = new CollieConfig(collie, logger);
const hub = new CollieHub(git, collie, config);
const hub = new CollieHub(git, collie, logger, config);

if (opts.clean) {
logger.progress("cleaning local cache of hub modules");
Expand Down
23 changes: 19 additions & 4 deletions src/model/CollieHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as fs from "std/fs";
import { GitCliFacade } from "/api/git/GitCliFacade.ts";
import { CollieRepository } from "/model/CollieRepository.ts";
import { CollieConfig } from "./CollieConfig.ts";
import { Logger } from "../cli/Logger.ts";

export class CollieHub {
constructor(
private readonly git: GitCliFacade,
private readonly repo: CollieRepository,
private readonly config: CollieConfig
private readonly logger: Logger,
private readonly config: CollieConfig,
) {}
private readonly hubCacheDirPath = [".collie", "hub"];

Expand Down Expand Up @@ -54,13 +56,26 @@ export class CollieHub {
".git",
);
const hasAlreadyCloned = await this.git.isRepo(hubCacheGitDir);
const collieHubVersion = this.config.getProperty("colliehubVersion");

if (! hasAlreadyCloned) {
if (!hasAlreadyCloned && !collieHubVersion) {
await this.git.clone(hubCacheDir, this.url);
const latestTag = await this.git.getLatestTag(hubCacheDir);
const latestTag = await this.git.getLatestTag(hubCacheDir);
await this.git.checkout(hubCacheDir, latestTag);
this.config.setProperty("colliehubVersion", latestTag);
}
} else if (collieHubVersion !== undefined) {
try {
await this.git.clone(hubCacheDir, this.url);
const latestTag = await this.git.getLatestTag(hubCacheDir);
if (latestTag !== collieHubVersion) {
throw new Error(`version tag ${collieHubVersion} not exist`);
}
} catch (error) {
this.logger.error(`${error}`);
Deno.exit(1);
}
await this.git.checkout(hubCacheDir, collieHubVersion);
}

return hubCacheDir;
}
Expand Down

0 comments on commit ae3dc06

Please sign in to comment.