Check that each directory is a valid flake

This commit is contained in:
Luc Perkins 2024-05-23 15:33:00 -03:00
parent f5dab0ead5
commit 1127ba41bd
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
3 changed files with 97 additions and 21 deletions

49
dist/index.js vendored
View file

@ -94751,6 +94751,8 @@ function mungeDiagnosticEndpoint(inputUrl) {
* Copyright (c) 2018-2020 [Samuel Carreira]
*/
//# sourceMappingURL=index.js.map
// EXTERNAL MODULE: external "fs"
var external_fs_ = __nccwpck_require__(7147);
;// CONCATENATED MODULE: ./dist/index.js
// src/nix.ts
function makeNixCommandArgs(nixOptions, flakeInputs, commitMessage) {
@ -94766,6 +94768,7 @@ function makeNixCommandArgs(nixOptions, flakeInputs, commitMessage) {
var EVENT_EXECUTION_FAILURE = "execution_failure";
var UpdateFlakeLockAction = class extends DetSysAction {
constructor() {
@ -94779,9 +94782,7 @@ var UpdateFlakeLockAction = class extends DetSysAction {
this.nixOptions = inputs_exports.getArrayOfStrings("nix-options", "space");
this.pathToFlakeDir = inputs_exports.getStringOrNull("path-to-flake-dir");
this.flakeDirs = inputs_exports.getArrayOfStrings("flake-dirs", "space");
if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") {
throw new Error("Both path-to-flake-dir and flake-dirs is defined");
}
this.validateInputs();
}
async main() {
await this.update();
@ -94792,7 +94793,7 @@ var UpdateFlakeLockAction = class extends DetSysAction {
async update() {
if (this.flakeDirs !== null && this.flakeDirs.length > 0) {
core.debug(
`Running flake lock update in multiple directories: ${this.flakeDirs}`
`Running flake lock update in multiple directories: ${this.flakeDirs.map((dir) => `\`${dir}\``).join(" ")}`
);
for (const directory of this.flakeDirs) {
await this.updateFlake(directory);
@ -94803,7 +94804,9 @@ var UpdateFlakeLockAction = class extends DetSysAction {
}
}
async updateFlake(flakeDir) {
core.debug(`Running flake lock update in directory ${flakeDir}`);
this.ensureDirectoryExists(flakeDir);
this.ensureDirectoryIsFlake(flakeDir);
core.debug(`Running flake lock update in directory \`${flakeDir}\``);
const nixCommandArgs = makeNixCommandArgs(
this.nixOptions,
this.flakeInputs,
@ -94827,14 +94830,46 @@ var UpdateFlakeLockAction = class extends DetSysAction {
exitCode
});
core.setFailed(
`non-zero exit code of ${exitCode} detected while updating directory ${flakeDir}`
`non-zero exit code of ${exitCode} detected while updating directory \`${flakeDir}\``
);
} else {
core.info(
`flake.lock file in ${flakeDir} was successfully updated`
`flake.lock file in \`${flakeDir}\` was successfully updated`
);
}
}
validateInputs() {
if (this.flakeDirs !== null && this.flakeDirs.length > 0 && this.pathToFlakeDir !== "") {
throw new Error(
"Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be set"
);
}
if (this.flakeDirs !== null && this.flakeDirs.length === 0) {
throw new Error(
"The `flake-dirs` input is set to an empty array; it must contain at least one directory"
);
}
}
ensureDirectoryExists(flakeDir) {
core.debug(`Checking that flake directory \`${flakeDir}\` exists`);
external_fs_.access(flakeDir, external_fs_.constants.F_OK, (err) => {
if (err !== null) {
throw new Error(`Directory \`${flakeDir}\` doesn't exist`);
} else {
core.debug(`Flake directory \`${flakeDir}\` exists`);
}
});
}
ensureDirectoryIsFlake(flakeDir) {
const flakeDotNix = `${flakeDir}/flake.nix`;
if (!external_fs_.existsSync(flakeDotNix)) {
throw new Error(
`Directory \`${flakeDir}\` is not a valid flake as it doesn't contain a \`flake.nix\``
);
} else {
core.debug(`Directory \`${flakeDir}\` is a valid flake`);
}
}
};
function main() {
new UpdateFlakeLockAction().execute();

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,7 @@ import { makeNixCommandArgs } from "./nix.js";
import * as actionsCore from "@actions/core";
import * as actionsExec from "@actions/exec";
import { DetSysAction, inputs } from "detsys-ts";
import * as fs from "fs";
const EVENT_EXECUTION_FAILURE = "execution_failure";
@ -25,15 +26,7 @@ class UpdateFlakeLockAction extends DetSysAction {
this.pathToFlakeDir = inputs.getStringOrNull("path-to-flake-dir");
this.flakeDirs = inputs.getArrayOfStrings("flake-dirs", "space");
// Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both
if (
this.flakeDirs !== null &&
this.flakeDirs.length > 0 &&
this.pathToFlakeDir !== ""
) {
// TODO: improve this error message
throw new Error("Both path-to-flake-dir and flake-dirs is defined");
}
this.validateInputs();
}
async main(): Promise<void> {
@ -46,7 +39,7 @@ class UpdateFlakeLockAction extends DetSysAction {
async update(): Promise<void> {
if (this.flakeDirs !== null && this.flakeDirs.length > 0) {
actionsCore.debug(
`Running flake lock update in multiple directories: ${this.flakeDirs}`,
`Running flake lock update in multiple directories: ${this.flakeDirs.map((dir) => `\`${dir}\``).join(" ")}`,
);
for (const directory of this.flakeDirs) {
@ -60,7 +53,10 @@ class UpdateFlakeLockAction extends DetSysAction {
}
private async updateFlake(flakeDir: string): Promise<void> {
actionsCore.debug(`Running flake lock update in directory ${flakeDir}`);
this.ensureDirectoryExists(flakeDir);
this.ensureDirectoryIsFlake(flakeDir);
actionsCore.debug(`Running flake lock update in directory \`${flakeDir}\``);
// Nix command of this form:
// nix ${maybe nix options} flake ${"update" or "lock"} ${maybe --update-input flags} --commit-lock-file --commit-lockfile-summary ${commit message}
@ -94,14 +90,59 @@ class UpdateFlakeLockAction extends DetSysAction {
exitCode,
});
actionsCore.setFailed(
`non-zero exit code of ${exitCode} detected while updating directory ${flakeDir}`,
`non-zero exit code of ${exitCode} detected while updating directory \`${flakeDir}\``,
);
} else {
actionsCore.info(
`flake.lock file in ${flakeDir} was successfully updated`,
`flake.lock file in \`${flakeDir}\` was successfully updated`,
);
}
}
private validateInputs(): void {
// Ensure that either path-to-flake-dir or flake-dirs is set to a meaningful value but not both
if (
this.flakeDirs !== null &&
this.flakeDirs.length > 0 &&
this.pathToFlakeDir !== ""
) {
// TODO: improve this error message
throw new Error(
"Both `path-to-flake-dir` and `flake-dirs` are set, whereas only one can be set",
);
}
// Ensure that flake-dirs isn't an empty array if set
if (this.flakeDirs !== null && this.flakeDirs.length === 0) {
throw new Error(
"The `flake-dirs` input is set to an empty array; it must contain at least one directory",
);
}
}
private ensureDirectoryExists(flakeDir: string): void {
actionsCore.debug(`Checking that flake directory \`${flakeDir}\` exists`);
// Ensure the directory exists
fs.access(flakeDir, fs.constants.F_OK, (err) => {
if (err !== null) {
throw new Error(`Directory \`${flakeDir}\` doesn't exist`);
} else {
actionsCore.debug(`Flake directory \`${flakeDir}\` exists`);
}
});
}
private ensureDirectoryIsFlake(flakeDir: string): void {
const flakeDotNix = `${flakeDir}/flake.nix`;
if (!fs.existsSync(flakeDotNix)) {
throw new Error(
`Directory \`${flakeDir}\` is not a valid flake as it doesn't contain a \`flake.nix\``,
);
} else {
actionsCore.debug(`Directory \`${flakeDir}\` is a valid flake`);
}
}
}
function main(): void {