Skip to content

Commit

Permalink
fix(mcl/json): Add writeJsonFile function that ensures that parent …
Browse files Browse the repository at this point in the history
…dirs are created as needed
  • Loading branch information
PetarKirov committed Jan 23, 2025
1 parent 0496211 commit 1e5405e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/mcl/src/src/mcl/commands/deploy_spec.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ module mcl.commands.deploy_spec;

import std.algorithm : filter;
import std.logger : infof, warningf;
import std.file : exists, readText;
import std.file : exists;
import std.path : buildPath;
import std.file : writeFile = write;
import std.json : parseJSON, JSONOptions;

import mcl.utils.process : spawnProcessInline;
import mcl.utils.path : resultDir;
import mcl.utils.env : parseEnv;
import mcl.utils.cachix : cachixNixStoreUrl, DeploySpec, createMachineDeploySpec;
import mcl.utils.tui : bold;
import mcl.utils.json : toJSON, fromJSON, tryDeserializeFromJsonFile;
import mcl.utils.json : tryDeserializeFromJsonFile, writeJsonFile;

import mcl.commands.ci_matrix : flakeAttr, params, Params, nixEvalJobs, SupportedSystem;
import mcl.commands.ci_matrix : flakeAttr, params, nixEvalJobs, SupportedSystem;

export void deploy_spec()
{
Expand Down Expand Up @@ -42,7 +39,7 @@ export void deploy_spec()
throw new Exception("Some Nixos configurations are not in cachix. Please cache them first.");

spec = nixosConfigs.createMachineDeploySpec();
writeFile(deploySpecFile, spec.toJSON.toPrettyString(JSONOptions.doNotEscapeSlashes));
writeJsonFile(spec, deploySpecFile);
}
else
{
Expand Down
9 changes: 9 additions & 0 deletions packages/mcl/src/src/mcl/utils/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,12 @@ T tryDeserializeFromJsonFile(T)(string path)
.fromJSON!T()
.tryGet("Error deserializing %s. JSON: \n%s".format(T.stringof.bold, json.toPrettyString().bold));
}

void writeJsonFile(T)(in T value, const(char)[] path)
{
import std.path : dirName;
import std.file : mkdirRecurse, writeFile = write;
auto json = value.toJSON.toPrettyString(JSONOptions.doNotEscapeSlashes);
mkdirRecurse(path.dirName);
writeFile(path, json);
}

0 comments on commit 1e5405e

Please sign in to comment.