Skip to content

Commit

Permalink
feat: drop init in main and seq through pkg managers
Browse files Browse the repository at this point in the history
  • Loading branch information
musdotdigital committed Nov 30, 2023
1 parent 6ebd52c commit 13cdecb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
13 changes: 6 additions & 7 deletions __tests__/dockerfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {test, expect} from '@jest/globals'
import * as path from 'path'
import {load} from '../src/dockerfile'

test('load invalid dockerfile', () => {
test('load invalid dockerfile', async () => {
let dockerfilePath = path.join(__dirname, 'data', 'InvalidDockerfile')
function loadInvalid() {
load(dockerfilePath)
}
expect(loadInvalid).toThrowError('Unable to extract image from Dockerfile')
await expect(load(dockerfilePath)).rejects.toThrow(
'Unable to extract image from Dockerfile'
)
})

test('load alpine dockerfile', () => {
test('load alpine dockerfile', async () => {
const dockerfilePath = path.join(__dirname, 'data', 'Dockerfile')
const dockerfile = load(dockerfilePath)
const dockerfile = await load(dockerfilePath)
expect(dockerfile.name).toBe('alpine:latest')
})
40 changes: 30 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -62,8 +71,12 @@ exports.load = void 0;
const image = __importStar(__nccwpck_require__(281));
const fs_1 = __importDefault(__nccwpck_require__(147));
function load(dockerfile) {
const content = fs_1.default.readFileSync(dockerfile).toString('utf-8');
return extract_docker_image(content);
return __awaiter(this, void 0, void 0, function* () {
const content = fs_1.default.readFileSync(dockerfile).toString('utf-8');
const extractedImage = extract_docker_image(content);
yield extractedImage.init_package_manager();
return extractedImage;
});
}
exports.load = load;
function extract_docker_image(dockerfile_content) {
Expand Down Expand Up @@ -100,6 +113,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Image = void 0;
const docker_cli_js_1 = __nccwpck_require__(771);
const packageManagers = [
{ command: 'apk --version', name: 'apk' },
{ command: 'apt-get --version', name: 'apt-get' }
];
class Image {
constructor(name) {
this.name = name;
Expand All @@ -109,13 +126,17 @@ class Image {
}
init_package_manager() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.docker.command(`run ${this.name} sh -c "apk --version > /dev/null"`);
this.pkgManager = 'apk';
}
catch (error) {
this.pkgManager = 'apt-get';
for (const manager of packageManagers) {
try {
yield this.docker.command(`run ${this.name} sh -c "${manager.command} > /dev/null"`);
this.pkgManager = manager.name;
return;
}
catch (error) {
// Continue to the next iteration if the current one fails
}
}
throw Error('Unable to find supported package manager');
});
}
get_latest_version(installed_package) {
Expand Down Expand Up @@ -213,8 +234,7 @@ function run() {
const dockerfile_path = core.getInput('dockerfile');
const dependencies_path = core.getInput('dependencies');
const apply = core.getBooleanInput('apply');
const image = dockerfile.load(dockerfile_path);
yield image.init_package_manager();
const image = yield dockerfile.load(dockerfile_path);
const dependencies_info = dependencies.load(dependencies_path);
const packages_update = dependencies_info.map(function (installed_pkg) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/dockerfile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as image from './image'
import fs from 'fs'

export function load(dockerfile: string): image.Image {
export async function load(dockerfile: string): Promise<image.Image> {
const content = fs.readFileSync(dockerfile).toString('utf-8')
return extract_docker_image(content)
const extractedImage = extract_docker_image(content)
await extractedImage.init_package_manager()
return extractedImage
}

function extract_docker_image(dockerfile_content: string): image.Image {
Expand Down
27 changes: 19 additions & 8 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import {Package} from './dependencies'

export type PackageManager = 'apk' | 'apt-get'

const packageManagers: {
command: string
name: PackageManager
}[] = [
{command: 'apk --version', name: 'apk'},
{command: 'apt-get --version', name: 'apt-get'}
]

export class Image {
name: string
pkgManager: PackageManager | null
Expand All @@ -20,16 +28,19 @@ export class Image {
)
this.docker = new Docker(options)
}

async init_package_manager(): Promise<void> {
try {
await this.docker.command(
`run ${this.name} sh -c "apk --version > /dev/null"`
)
this.pkgManager = 'apk'
} catch (error) {
this.pkgManager = 'apt-get'
for (const manager of packageManagers) {
try {
await this.docker.command(
`run ${this.name} sh -c "${manager.command} > /dev/null"`
)
this.pkgManager = manager.name
return
} catch (error) {
// Continue to the next iteration if the current one fails
}
}
throw Error('Unable to find supported package manager')
}

async get_latest_version(installed_package: Package): Promise<Package> {
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ async function run(): Promise<void> {
const dependencies_path = core.getInput('dependencies')
const apply = core.getBooleanInput('apply')

const image = dockerfile.load(dockerfile_path)
await image.init_package_manager()

const image = await dockerfile.load(dockerfile_path)
const dependencies_info = dependencies.load(dependencies_path)
const packages_update = dependencies_info.map(async function (
installed_pkg
Expand Down

0 comments on commit 13cdecb

Please sign in to comment.