Skip to content

Commit

Permalink
feat(cli): add upgrade command and update notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
umbopepato committed Apr 6, 2021
1 parent 13136e8 commit 8bcf024
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 7 deletions.
14 changes: 12 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { loadConfig } from "./src/load_config.ts";
import { VrCommand } from "./src/cli/commands/vr.ts";
import { version } from "./src/version.ts";
import { DenoLand, UpdateNotifier } from "./deps.ts";
import { VR_NAME } from "./src/consts.ts";

if (import.meta.main) {
const notifier = new UpdateNotifier({
name: VR_NAME,
registry: DenoLand,
currentVersion: version,
});
const checkForUpdates = notifier.checkForUpdates();
const config = await loadConfig();
new VrCommand(config)
.parse(Deno.args);
new VrCommand(config).parse(Deno.args);
await checkForUpdates;
notifier.notify("vr upgrade");
}
7 changes: 7 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ export {
ValidationError,
} from "https://deno.land/x/cliffy@v0.18.1/command/mod.ts";
export { kill } from "https://deno.land/x/process@v0.3.0/mod.ts";
export {
DenoLand,
latestVersion,
parseURL,
UpdateNotifier,
} from "https://x.nest.land/hatcher@0.10.1/mod.ts";
export * as semver from "https://deno.land/x/semver@v1.0.0/mod.ts";
42 changes: 42 additions & 0 deletions src/cli/commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Command, DenoLand, semver } from "../../../deps.ts";
import { log } from "../../logger.ts";
import { version as currentVersion } from "../../version.ts";
import { VR_NAME } from "../../consts.ts";
import { spawn } from "../../util.ts";

export class UpgradeCommand extends Command {
constructor() {
super();
this.description(
"Upgrade Velociraptor to the latest version or to a specific one",
)
.arguments("[version:string]")
.action(async (options, version: string | undefined) => {
let newVersion = version;
if (!newVersion) {
newVersion = await DenoLand.latestVersion(VR_NAME);
}
if (!newVersion) {
log.error("Cannot retrieve the latest version tag");
return;
}
if (semver.eq(newVersion, currentVersion)) {
log.info("Velociraptor is already up-to-date");
return;
}
try {
await spawn([
"deno",
"install",
"--reload",
"-qAfn",
"vr",
`https://deno.land/x/${VR_NAME}@${newVersion}/cli.ts`,
]);
log.info(`✅ Successfully upgraded to ${newVersion}`);
} catch (e) {
console.log(e.message);
}
});
}
}
2 changes: 2 additions & 0 deletions src/cli/commands/vr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RunHookCommand } from "./run_hook.ts";
import { VR_HOOKS, VR_LOG, VR_SHELL } from "../../consts.ts";
import { checkGitHooks } from "../../git_hooks.ts";
import { validateConfigData } from "../../validate_config_data.ts";
import { UpgradeCommand } from "./upgrade.ts";

export class VrCommand extends Command {
constructor(private configData: ConfigData | null) {
Expand Down Expand Up @@ -41,6 +42,7 @@ export class VrCommand extends Command {
.command("run", new RunCommand(this.configData))
.command("run-hook", new RunHookCommand(this.configData))
.command("export", new ExportCommand(this.configData))
.command("upgrade", new UpgradeCommand())
.command("completions", new CompletionsCommand())
.reset();
}
Expand Down
3 changes: 2 additions & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const VR_MARK = "Generated by velociraptor";
export const VR_NAME = "velociraptor";
export const VR_MARK = "Generated by Velociraptor";
export const VR_SHELL = "VR_SHELL";
export const VR_LOG = "VR_LOG";
export const VR_HOOKS = "VR_HOOKS";
2 changes: 1 addition & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ await logger.setup({
},
loggers: {
default: {
level: Deno.env.get(VR_LOG) as LevelName || "WARNING",
level: Deno.env.get(VR_LOG) as LevelName || "INFO",
handlers: ["console"],
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/run_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function runCommand(
cwd,
env: getEnvVars(command),
};
log.info(
log.debug(
`Running > ${cmd}${
additionalArgs && additionalArgs.length > 0
? ` -- ${additionalArgs.join(" ")}`
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export function makeFileExecutable(filePath: string) {
}
}

export async function spawn(args: string[], cwd: string): Promise<string> {
export async function spawn(args: string[], cwd?: string): Promise<string> {
const process = Deno.run({
cmd: args,
cwd,
stdout: "piped",
stderr: "null",
stderr: "piped",
});
const { code } = await process.status();
if (code === 0) {
Expand Down

0 comments on commit 8bcf024

Please sign in to comment.