Last active
October 1, 2020 23:41
-
-
Save natemccurdy/1958e0dcaa37ef45c71e7b5bbd5da99e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is: ben_weird_sort/functions/sort_hiera_roles.pp | |
# | |
# Returns a list of hiera role paths in sorted order | |
# | |
# Strings are returned in lexical order, unless one path is a substring | |
# of another, in which case the longer string appears first. | |
# | |
# This is so that child roles are checked before parent roles | |
# | |
function ben_weird_sort::sort_hiera_roles($list) { | |
$list.sort |$a, $b| { | |
if $a == $b { | |
0 | |
} elsif length($a) > length($b) and $a[0, length($b)+1] == "${b}/" { | |
-1 | |
} elsif length($b) > length($a) and $b[0, length($a)+1] == "${a}/" { | |
1 | |
} else { | |
compare($a, $b) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is: ben_weird_sort/spec/functions/sort_hiera_roles.rb | |
require 'spec_helper' | |
describe 'ben_weird_sort::sort_hiera_roles' do | |
context 'with multiple roles' do | |
it { | |
is_expected.to run.with_params( | |
['qux', 'foo', 'foo/barbie', 'foo/bar', 'foo/bar/baz', 'foo/baz'], | |
).and_return( | |
['foo/bar/baz', 'foo/bar', 'foo/barbie', 'foo/baz', 'foo', 'qux'], | |
) | |
} | |
end | |
end |
Author
natemccurdy
commented
Oct 1, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment