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

feat: save clients nested via their git sha #211

Merged
merged 3 commits into from
Feb 6, 2021
Merged
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
31 changes: 24 additions & 7 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default class UpdateCommand extends Command {

private channel!: string

private currentVersion?: string

private updatedVersion!: string

private readonly clientRoot = this.config.scopedEnvVar('OCLIF_CLIENT_HOME') || path.join(this.config.dataDir, 'client')

private readonly clientBin = path.join(this.clientRoot, 'bin', this.config.windows ? `${this.config.bin}.cmd` : this.config.bin)
Expand All @@ -38,7 +42,9 @@ export default class UpdateCommand extends Command {
this.channel = args.channel || await this.determineChannel()
await this.config.runHook('preupdate', {channel: this.channel})
const manifest = await this.fetchManifest()
const reason = await this.skipUpdate(manifest)
this.currentVersion = await this.determineCurrentVersion()
this.updatedVersion = (manifest as any).sha ? `${manifest.version}-${(manifest as any).sha}` : manifest.version
const reason = await this.skipUpdate()
if (reason) cli.action.stop(reason || 'done')
else await this.update(manifest)
this.debug('tidy')
Expand Down Expand Up @@ -94,14 +100,14 @@ export default class UpdateCommand extends Command {
private async update(manifest: IManifest, channel = 'stable') {
const {version, channel: manifestChannel} = manifest
if (manifestChannel) channel = manifestChannel
cli.action.start(`${this.config.name}: Updating CLI from ${color.green(this.config.version)} to ${color.green(version)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)
cli.action.start(`${this.config.name}: Updating CLI from ${color.green(this.currentVersion)} to ${color.green(this.updatedVersion)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)
const http: typeof HTTP = require('http-call').HTTP
const filesize = (n: number): string => {
const [num, suffix] = require('filesize')(n, {output: 'array'})
return num.toFixed(1) + ` ${suffix}`
}
await this.ensureClientDir()
const output = path.join(this.clientRoot, version)
const output = path.join(this.clientRoot, this.updatedVersion)

const gzUrl = manifest.gz || this.config.s3Url(this.config.s3Key('versioned', {
version,
Expand Down Expand Up @@ -145,20 +151,20 @@ export default class UpdateCommand extends Command {
await extraction

await this.setChannel()
await this.createBin(version)
await this.createBin(this.updatedVersion)
await this.touch()
await this.reexec()
}

private async skipUpdate(manifest: IManifest): Promise<string | false> {
private async skipUpdate(): Promise<string | false> {
if (!this.config.binPath) {
const instructions = this.config.scopedEnvVar('UPDATE_INSTRUCTIONS')
if (instructions) this.warn(instructions)
return 'not updatable'
}
if (this.config.version === manifest.version) {
if (this.currentVersion === this.updatedVersion) {
if (this.config.scopedEnvVar('HIDE_UPDATED_MESSAGE')) return 'done'
return `already on latest version: ${this.config.version}`
return `already on latest version: ${this.currentVersion}`
}
return false
}
Expand All @@ -172,6 +178,17 @@ export default class UpdateCommand extends Command {
return this.config.channel || 'stable'
}

private async determineCurrentVersion(): Promise<string|undefined> {
try {
const currentVersion = await fs.readlink(path.join(this.clientRoot, 'current'))
const matches = currentVersion.match(/\.\.[/|\\](.+)[/|\\]bin/)
return matches ? matches[1] : this.config.version
} catch (error) {
this.debug(error)
}
return this.config.version
}

private s3ChannelManifestKey(bin: string, platform: string, arch: string, folder?: string): string {
let s3SubDir = folder || ''
if (s3SubDir !== '' && s3SubDir.slice(-1) !== '/') s3SubDir = `${s3SubDir}/`
Expand Down