function.fish 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. set -l base_dir "$HOME/nix-dots/data"
  2. argparse 'f/flake' 'l/license' 'list' -- $argv
  3. or return 1
  4. if not set -q _flag_flake; and not set -q _flag_license
  5. echo "Error: Must specify either --flake (-f) or --license (-l)."
  6. return 1
  7. else if set -q _flag_flake; and set -q _flag_license
  8. echo "Error: Cannot specify both --flake and --license at the same time."
  9. return 1
  10. end
  11. set -l mode ""
  12. set -l ext ""
  13. set -l dest ""
  14. if set -q _flag_flake
  15. set mode "flake"
  16. set ext "nix"
  17. set dest "flake.nix"
  18. else if set -q _flag_license
  19. set mode "license"
  20. set ext "txt"
  21. set dest "LICENSE"
  22. end
  23. set -l target_base "$base_dir/$mode"
  24. if set -q _flag_list
  25. if command -q tree
  26. tree --noreport -P "*.$ext" --prune "$target_base"
  27. else
  28. for dir in "$target_base"/*/
  29. if test -d "$dir"
  30. echo (basename "$dir")"/"
  31. for file in "$dir"*."$ext"
  32. if test -f "$file"
  33. echo " └── "(basename "$file")
  34. end
  35. end
  36. end
  37. end
  38. end
  39. return 0
  40. end
  41. set -l tool $argv[1]
  42. set -l name $argv[2]
  43. if test -z "$tool"
  44. echo "Error: No tool/category specified."
  45. return 1
  46. end
  47. set -l target_dir "$target_base/$tool"
  48. if not test -d "$target_dir"
  49. echo "Error: Directory $target_dir does not exist."
  50. return 1
  51. end
  52. set -l target_file ""
  53. if test -n "$name"
  54. set target_file "$target_dir/$name.$ext"
  55. else
  56. set target_file "$target_dir/default.$ext"
  57. end
  58. if not test -f "$target_file"
  59. echo "Error: File $target_file does not exist."
  60. return 1
  61. end
  62. echo "Copying $target_file -> ./$dest"
  63. cp "$target_file" "./$dest"
  64. if set -q _flag_flake
  65. echo "use flake" > .envrc
  66. direnv allow
  67. end