From d1f4283ae50334a986dac03d0e8e4a48076c84b2 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:02:06 +0200 Subject: [PATCH] regclient: regctl version Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/regclient/regctl.test.ts | 63 +++++++++++++++++++++ dev.Dockerfile | 3 + src/regclient/regctl.ts | 91 ++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 __tests__/regclient/regctl.test.ts create mode 100644 src/regclient/regctl.ts diff --git a/__tests__/regclient/regctl.test.ts b/__tests__/regclient/regctl.test.ts new file mode 100644 index 0000000..94ea0b0 --- /dev/null +++ b/__tests__/regclient/regctl.test.ts @@ -0,0 +1,63 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, expect, it, jest, test} from '@jest/globals'; +import * as semver from 'semver'; + +import {Exec} from '../../src/exec'; +import {Regctl} from '../../src/regclient/regctl'; + +describe('isAvailable', () => { + it('checks regctl is available', async () => { + const execSpy = jest.spyOn(Exec, 'getExecOutput'); + const regctl = new Regctl(); + await regctl.isAvailable(); + // eslint-disable-next-line jest/no-standalone-expect + expect(execSpy).toHaveBeenCalledWith(`regctl`, [], { + silent: true, + ignoreReturnCode: true + }); + }); +}); + +describe('printVersion', () => { + it('prints regctl version', async () => { + const execSpy = jest.spyOn(Exec, 'exec'); + const regctl = new Regctl(); + await regctl.printVersion(); + expect(execSpy).toHaveBeenCalledWith(`regctl`, ['version'], { + failOnStdErr: false + }); + }); +}); + +describe('version', () => { + it('valid', async () => { + const regctl = new Regctl(); + expect(semver.valid(await regctl.version())).not.toBeUndefined(); + }); +}); + +describe('versionSatisfies', () => { + test.each([ + ['v0.8.2', '>=0.6.0', true], + ['v0.8.0', '>0.6.0', true], + ['v0.8.0', '<0.3.0', false] + ])('given %p', async (version, range, expected) => { + const regctl = new Regctl(); + expect(await regctl.versionSatisfies(range, version)).toBe(expected); + }); +}); diff --git a/dev.Dockerfile b/dev.Dockerfile index 748dcb5..91a2626 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -19,6 +19,7 @@ ARG DOCKER_VERSION=27.2.1 ARG BUILDX_VERSION=0.23.0 ARG COMPOSE_VERSION=2.32.4 ARG UNDOCK_VERSION=0.8.0 +ARG REGCTL_VERSION=v0.8.2 FROM node:${NODE_VERSION}-alpine AS base RUN apk add --no-cache cpio findutils git @@ -79,6 +80,7 @@ FROM docker:${DOCKER_VERSION} AS docker FROM docker/buildx-bin:${BUILDX_VERSION} AS buildx FROM docker/compose-bin:v${COMPOSE_VERSION} AS compose FROM crazymax/undock:${UNDOCK_VERSION} AS undock +FROM ghcr.io/regclient/regctl:${REGCTL_VERSION} AS regctl FROM deps AS test RUN --mount=type=bind,target=.,rw \ @@ -90,6 +92,7 @@ RUN --mount=type=bind,target=.,rw \ --mount=type=bind,from=compose,source=/docker-compose,target=/usr/libexec/docker/cli-plugins/docker-compose \ --mount=type=bind,from=compose,source=/docker-compose,target=/usr/bin/compose \ --mount=type=bind,from=undock,source=/usr/local/bin/undock,target=/usr/bin/undock \ + --mount=type=bind,from=regctl,source=/regctl,target=/usr/bin/regctl \ --mount=type=secret,id=GITHUB_TOKEN \ GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) yarn run test:coverage --coverageDirectory=/tmp/coverage diff --git a/src/regclient/regctl.ts b/src/regclient/regctl.ts new file mode 100644 index 0000000..0c80808 --- /dev/null +++ b/src/regclient/regctl.ts @@ -0,0 +1,91 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as core from '@actions/core'; +import * as semver from 'semver'; + +import {Exec} from '../exec'; + +export interface RegctlOpts { + binPath?: string; +} + +export class Regctl { + private readonly binPath: string; + private _version: string; + private _versionOnce: boolean; + + constructor(opts?: RegctlOpts) { + this.binPath = opts?.binPath || 'regctl'; + this._version = ''; + this._versionOnce = false; + } + + public async isAvailable(): Promise { + const ok: boolean = await Exec.getExecOutput(this.binPath, [], { + ignoreReturnCode: true, + silent: true + }) + .then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.debug(`Regctl.isAvailable cmd err: ${res.stderr.trim()}`); + return false; + } + return res.exitCode == 0; + }) + .catch(error => { + core.debug(`Regctl.isAvailable error: ${error}`); + return false; + }); + + core.debug(`Regctl.isAvailable: ${ok}`); + return ok; + } + + public async version(): Promise { + if (this._versionOnce) { + return this._version; + } + this._versionOnce = true; + this._version = await Exec.getExecOutput(this.binPath, ['version', '--format', '{{.VCSTag}}'], { + ignoreReturnCode: true, + silent: true + }).then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + throw new Error(res.stderr.trim()); + } + return res.stdout.trim(); + }); + return this._version; + } + + public async printVersion() { + await Exec.exec(this.binPath, ['version'], { + failOnStdErr: false + }); + } + + public async versionSatisfies(range: string, version?: string): Promise { + const ver = version ?? (await this.version()); + if (!ver) { + core.debug(`Regctl.versionSatisfies false: undefined version`); + return false; + } + const res = semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null; + core.debug(`Regctl.versionSatisfies ${ver} statisfies ${range}: ${res}`); + return res; + } +}