Skip to content

Commit

Permalink
feat(bud): allow configuring hosts, users and modules in 'cfg'
Browse files Browse the repository at this point in the history
  • Loading branch information
yusdacra committed Aug 25, 2021
1 parent 8a4d32d commit be09e1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
53 changes: 39 additions & 14 deletions bud/cfg.bash
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,26 @@ function print_source_help () {
echo "if 'source' is given as 'owner/repo', it will default to 'github:owner/repo'"
}

function show_tree_without_default () {
# TODO: why doesn't `grep -v "(default.nix|profiles|modules)$"` work?
exa ${3} -Th "$1" | grep -v "default.nix" | grep -v "profiles" | grep -v "modules" | sed 's/\.nix//g' | tail -n +1 || echo "$2"
}

GIT_CACHE="$HOME/.cache/bud/git"

case "$1" in
"show")
shift 1

url="$1"
ty="$2"

if [[ -z "$url" ]]; then
echo "Show profiles available in the specified source\n"
echo "Usage: show source\n"
echo "Show profiles / modules / users / hosts available in the specified source\n"
echo "Usage: show source (profiles|users|hosts|modules)\n"
print_source_help
echo "if 'source' is '.', it will show your own profiles"
echo "if 'source' is '.', it will show your own profiles / users / hosts / modules\n"
echo "if none of 'profiles', 'users', 'hosts' or 'modules' are specified, it will default to 'profiles'"
exit 1
fi

Expand All @@ -100,19 +107,37 @@ case "$1" in
fetch_repo "$url" "$repo_path" || exit $?
fi

echo "nixos profiles:"
exa -Th "$repo_path/profiles" | grep -v "default.nix" | tail -n +2 || echo "no nixos profiles found"
echo "home-manager profiles:"
exa -Th "$repo_path/users/profiles" | grep -v "default.nix" | tail -n +2 || echo "no home-manager profiles found"
case "$ty" in
"users")
echo "users:"
show_tree_without_default "$repo_path/users" "no users found" "--level=1"
;;
"hosts")
echo "hosts:"
show_tree_without_default "$repo_path/hosts" "no hosts found" "--level=1"
;;
"modules")
echo "nixos modules:"
show_tree_without_default "$repo_path/modules" "no nixos profiles found"
echo "home-manager modules:"
show_tree_without_default "$repo_path/users/modules" "no home-manager profiles found"
;;
*)
echo "nixos profiles:"
show_tree_without_default "$repo_path/profiles" "no nixos profiles found"
echo "home-manager profiles:"
show_tree_without_default "$repo_path/users/profiles" "no home-manager profiles found"
;;
esac
;;
"remove")
shift 1

attr_origpath=$1

if [[ -z "$attr_origpath" ]]; then
echo "Remove a profile from your config\n"
echo "Usage: remove (nixos|hm).profiles.[PROFILE]"
echo "Remove a profile / user / host / module from your config\n"
echo "Usage: remove nixos.(profiles|users|hosts|modules).[NAME] / add source hm.(profiles|modules).[NAME]"
exit 1
fi

Expand All @@ -133,8 +158,8 @@ case "$1" in
attr_origpath=$2

if [[ -z "$url" || -z "$attr_origpath" ]]; then
echo "Add a profile from the specified source\n"
echo "Usage: add source (nixos|hm).profiles.[PROFILE]\n"
echo "Add a profile / user / host / module from the specified source\n"
echo "Usage: add source nixos.(profiles|users|hosts|modules).[NAME] / add source hm.(profiles|modules).[NAME]\n"
print_source_help
exit 1
fi
Expand All @@ -157,9 +182,9 @@ case "$1" in
;;
*)
echo "Available subcommands are:"
echo " - 'add': Add a profile from the specified source"
echo " - 'show': Show profiles available in the specified source"
echo " - 'remove': Remove a profile from your config\n"
echo " - 'add': Add a profile / user / host / module from the specified source"
echo " - 'show': Show profiles / users / hosts / modules available in the specified source"
echo " - 'remove': Remove a profile / user / host / module from your config\n"
echo "run 'bud cfg command' for more info about a command"
exit 1
;;
Expand Down
2 changes: 1 addition & 1 deletion bud/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
script = ./get.bash;
};
cfg = {
writer = budUtils.writeBashWithPaths [ git coreutils exa gnugrep ];
writer = budUtils.writeBashWithPaths [ git coreutils exa gnugrep gnused ];
synopsis = "cfg [SUBCOMMAND]";
help = "Manage profiles (add, remove, etc.) for your configuration";
script = ./cfg.bash;
Expand Down

0 comments on commit be09e1b

Please sign in to comment.