-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add cleanup path command (#3970)
* feat(cli): add cleanup paths command * test: fix test * fix: prettier
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Cli } from 'clipanion'; | ||
import { describe, expect, test, vi } from 'vitest'; | ||
import { prepareCommands } from '.'; | ||
|
||
const mocks = vi.hoisted(() => ({ | ||
deleteAsync: vi.fn(), | ||
})); | ||
|
||
vi.mock('del', () => mocks); | ||
|
||
describe('cli/command/cleanup-path', () => { | ||
test('download-file', async () => { | ||
const cli = new Cli({ binaryName: 'containerbase-cli' }); | ||
prepareCommands(cli, null); | ||
|
||
expect( | ||
await cli.run(['cleanup', 'path', '/tmp/**:/var/tmp', '/some/path/**']), | ||
).toBe(0); | ||
|
||
expect(mocks.deleteAsync).toHaveBeenCalledOnce(); | ||
expect(mocks.deleteAsync).toHaveBeenCalledWith( | ||
['/tmp/**', '/var/tmp', '/some/path/**'], | ||
{ dot: true }, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Command, Option } from 'clipanion'; | ||
import { deleteAsync } from 'del'; | ||
import prettyMilliseconds from 'pretty-ms'; | ||
import { logger } from '../utils'; | ||
|
||
export class CleanupPathCommand extends Command { | ||
static override paths = [['cleanup', 'path']]; | ||
|
||
static override usage = Command.Usage({ | ||
description: 'Cleanup passed paths.', | ||
examples: [ | ||
[ | ||
'Cleanup multiple paths', | ||
'$0 cleanup path "/tmp/**:/var/tmp" "/some/paths/**"', | ||
], | ||
], | ||
}); | ||
|
||
cleanupPaths = Option.Rest({ required: 1 }); | ||
|
||
async execute(): Promise<number | void> { | ||
const start = Date.now(); | ||
let error = false; | ||
const paths = this.cleanupPaths.flatMap((p) => p.split(':')); | ||
logger.info({ paths }, `Cleanup paths ...`); | ||
try { | ||
const deleted = await deleteAsync(paths, { dot: true }); | ||
logger.debug({ deleted }, 'Deleted paths'); | ||
return 0; | ||
} catch (err) { | ||
error = true; | ||
logger.debug(err); | ||
if (err instanceof Error) { | ||
logger.error(err.message); | ||
} | ||
return 1; | ||
} finally { | ||
if (error) { | ||
logger.fatal( | ||
`Cleanup failed in ${prettyMilliseconds(Date.now() - start)}.`, | ||
); | ||
} else { | ||
logger.info( | ||
`Cleanup succeded in ${prettyMilliseconds(Date.now() - start)}.`, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters