feat(submodules): allow to match modules by version regex

This commit is contained in:
Jaka Hudoklin 2019-03-01 17:49:37 +01:00
parent 5a7eefc489
commit fc3ba14b5c
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
3 changed files with 83 additions and 6 deletions

View file

@ -92,12 +92,14 @@ let
parentConfig = config;
};
findModule = {name, version ? null, latest ? true}: let
versionPrefix = head (splitString [".x"] version);
findSubmodule = {name, version ? null, latest ? true}: let
matchingSubmodules = filter (el:
el.definition.name == name &&
(if version != null then hasPrefix versionPrefix el.definition.version else true)
(if version != null then
if hasPrefix "~" version
then (builtins.match (removePrefix "~" version) el.definition.version) != null
else el.definition.version == version
else true)
) cfg.imports;
versionSortedSubmodules = sort (s1: s2:
@ -191,7 +193,7 @@ in {
description = "Attribute set of submodule instances";
type = types.attrsOf (types.submodule ({name, config, ...}: let
# submodule associated with
submodule = findModule {
submodule = findSubmodule {
name = config.submodule;
version = config.version;
};
@ -216,7 +218,10 @@ in {
};
version = mkOption {
description = "Version of submodule to use";
description = ''
Version of submodule to use, if version starts with "~" it is
threated as regex pattern for example "~1.0.*"
'';
type = types.nullOr types.str;
default = null;
};

View file

@ -37,6 +37,7 @@ let
./istio/bookinfo.nix
./submodules/simple.nix
./submodules/defaults.nix
./submodules/versioning.nix
];
testing.defaults = ({kubenix, ...}: {
imports = [kubenix.k8s];

View file

@ -0,0 +1,71 @@
{ name, config, lib, kubenix, ... }:
with lib;
let
inst-exact = config.submodules.instances.inst-exact.config;
inst-regex = config.submodules.instances.inst-regex.config;
inst-latest = config.submodules.instances.inst-latest.config;
submodule = {
config.submodule.name = "subm";
options.version = mkOption {
type = types.str;
default = "undefined";
};
};
in {
imports = [
kubenix.submodules
];
test = {
name = "submodules-imports";
description = "Submodules imports tests";
assertions = [{
message = "should select exact version";
assertion = inst-exact.version == "1.1.0";
} {
message = "should select regex version";
assertion = inst-regex.version == "1.2.1";
} {
message = "should select latest version";
assertion = inst-latest.version == "1.2.1";
}];
};
submodules.imports = [{
modules = [{
config.submodule.version = "1.0.0";
config.version = "1.0.0";
} submodule];
} {
modules = [{
config.submodule.version = "1.1.0";
config.version = "1.1.0";
} submodule];
} {
modules = [{
config.submodule.version = "1.2.0";
config.version = "1.2.0";
} submodule];
} {
modules = [{
config.submodule.version = "1.2.1";
config.version = "1.2.1";
} submodule];
}];
submodules.instances.inst-exact = {
submodule = "subm";
version = "1.1.0";
};
submodules.instances.inst-regex = {
submodule = "subm";
version = "~1.2.*";
};
submodules.instances.inst-latest.submodule = "subm";
}