
そこで、私は Puppet の不変変数に慣れつつあり、今、私がすでに正しい方向に進んでいるかどうかについてのアドバイスや説明を求めています。
同期したいディレクトリがありますが、そのサブディレクトリが複数ある可能性があります。そこで、再帰的に渡す1つ以上のパスの配列を構築したいと思います。ファイルリソース。理想的には、このようなものがあればいいのですが
$paths = ['fist/path'] # assume this is provided via class parameter
# this should work, since it's only overriding what was provided from higher scope
if($condition) {
$paths += 'second/path'
}
# this won't fly, since $paths has already been set once locally
if($another_condition) {
$paths += 'third/path'
}
しかし、変数は不変なので、そうすることはできない。これまでのところ、私が「最善」のアプローチとして思いついたのは、次のようなものだ。
$paths = ['fist/path']
if($condition) {
$condition_1_path = ['second/path']
} else {
$condition_1_path = []
}
if($another_condition) {
$condition_2_path = ['third/path']
} else {
$condition_2_path = []
}
$paths = concat($paths, $condition_1_path, $condition_2_path)
よく分かりません連結引数の1つに空の配列が指定された場合は結果からエントリを省略しますが、それをテストするのはstdlib をロードする方法を理解する。
いずれにせよ、このコードを見ると、私にとっては明らかに醜いです。このようなことをするよりきれいな方法はあるでしょうか?
答え1
醜いですが、このようにしました。
if($condition) {
$condition_1_path = 'first/path'
} else {
$condition_1_path = ''
}
if($another_condition) {
$condition_2_path = 'second/path'
} else {
$condition_2_path = ''
}
# split into array based on whitespace
$mylistofpaths = split("$condition_1_path $condition_2_path", '\s+')