Skip to content

Commit 78ea282

Browse files
authored
Merge pull request #89 from rcowsill/fix/87
Use path module instead of string manipulation
2 parents 653164a + 46c6909 commit 78ea282

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/LayerCache.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LayerCache {
1414
ids: string[] = []
1515
unformattedSaveKey: string = ''
1616
restoredRootKey: string = ''
17-
imagesDir: string = path.resolve(`${__dirname}/../.adlc`)
17+
imagesDir: string = path.join(__dirname, '..', '.adlc')
1818
enabledParallel = true
1919
concurrency: number = 4
2020

@@ -49,8 +49,8 @@ class LayerCache {
4949
}
5050

5151
private async saveImageAsUnpacked() {
52-
await fs.mkdir(this.getSavedImageTarDir(), { recursive: true })
53-
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getSavedImageTarDir() })
52+
await fs.mkdir(this.getUnpackedTarDir(), { recursive: true })
53+
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getUnpackedTarDir() })
5454
}
5555

5656
private async makeRepotagsDockerSaveArgReady(repotags: string[]): Promise<string[]> {
@@ -91,14 +91,14 @@ class LayerCache {
9191

9292
private async moveLayerTarsInDir(fromDir: string, toDir: string) {
9393
const layerTars = (await recursiveReaddir(fromDir))
94-
.filter(path => path.endsWith(`/layer.tar`))
95-
.map(path => path.replace(`${fromDir}/`, ``))
94+
.filter(layerPath => path.basename(layerPath) === `layer.tar`)
95+
.map(layerPath => path.relative(fromDir, layerPath))
9696

9797
const moveLayer = async (layer: string) => {
98-
const from = path.resolve(`${fromDir}/${layer}`)
99-
const to = path.resolve(`${toDir}/${layer}`)
98+
const from = path.join(fromDir, layer)
99+
const to = path.join(toDir, layer)
100100
core.debug(`Moving layer tar from ${from} to ${to}`)
101-
await fs.mkdir(`${path.dirname(to)}`, { recursive: true })
101+
await fs.mkdir(path.dirname(to), { recursive: true })
102102
await fs.rename(from, to)
103103
}
104104
await Promise.all(layerTars.map(moveLayer))
@@ -201,14 +201,14 @@ class LayerCache {
201201
}
202202

203203
private async restoreSingleLayerBy(id: string): Promise<string> {
204-
const path = this.genSingleLayerStorePath(id)
204+
const layerPath = this.genSingleLayerStorePath(id)
205205
const key = await this.recoverSingleLayerKey(id)
206-
const dir = path.replace(/[^/\\]+$/, ``)
206+
const dir = path.dirname(layerPath)
207207

208-
core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, path, dir, key }))
208+
core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, layerPath, dir, key }))
209209

210210
await fs.mkdir(dir, { recursive: true })
211-
const result = await cache.restoreCache([path], key)
211+
const result = await cache.restoreCache([layerPath], key)
212212

213213
if (result == null) {
214214
throw new Error(`${LayerCache.ERROR_LAYER_CACHE_NOT_FOUND_STR}: ${JSON.stringify({ id })}`)
@@ -232,23 +232,19 @@ class LayerCache {
232232
}
233233

234234
getUnpackedTarDir(): string {
235-
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
235+
return path.join(this.getImagesDir(), this.getCurrentTarStoreDir())
236236
}
237237

238238
getLayerCachesDir() {
239239
return `${this.getUnpackedTarDir()}-layers`
240240
}
241241

242-
getSavedImageTarDir(): string {
243-
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
244-
}
245-
246242
getCurrentTarStoreDir(): string {
247243
return 'image'
248244
}
249245

250246
genSingleLayerStorePath(id: string) {
251-
return path.resolve(`${this.getLayerCachesDir()}/${id}/layer.tar`)
247+
return path.join(this.getLayerCachesDir(), id, `layer.tar`)
252248
}
253249

254250
async generateRootHashFromManifest(): Promise<string> {
@@ -296,8 +292,7 @@ class LayerCache {
296292
}
297293

298294
async getLayerIds(): Promise<string[]> {
299-
const getIdfromLayerRelativePath = (path: string) => path.replace('/layer.tar', '')
300-
const layerIds = (await this.getLayerTarFiles()).map(getIdfromLayerRelativePath);
295+
const layerIds = (await this.getLayerTarFiles()).map(path.dirname);
301296
core.debug(JSON.stringify({ log: `getLayerIds`, layerIds }))
302297
return layerIds
303298
}

src/Tar.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertType } from 'typescript-is'
22
import { promises as fs } from 'fs'
3+
import * as path from 'path'
34

45
export interface Manifest {
56
Config: string
@@ -13,8 +14,8 @@ export function assertManifests(x: unknown): asserts x is Manifests {
1314
assertType<Manifests>(x)
1415
}
1516

16-
export async function loadRawManifests(path: string) {
17-
return (await fs.readFile(`${path}/manifest.json`)).toString()
17+
export async function loadRawManifests(rootPath: string) {
18+
return (await fs.readFile(path.join(rootPath, `manifest.json`))).toString()
1819
}
1920

2021
export async function loadManifests(path: string) {

0 commit comments

Comments
 (0)