add_scripts_path.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. current_dir=$(pwd)
  3. # Declares a function named `save_path` that takes two arguments: `path` and `extension`.
  4. # The function is responsible for checking if the given path is already present in the `bashrc` file.
  5. function save_path() {
  6. path="$1"
  7. extension="$2"
  8. # Check if the path is already present in bashrc
  9. if grep -qF ". $1" ~/.$2rc; then
  10. echo "Path already exists in .$2rc"
  11. else
  12. # Add the path to bashrc
  13. echo ". $1" >> ~/.$2rc
  14. echo "Path added to .$2rc"
  15. fi
  16. }
  17. # The `main` function is responsible for executing the main logic of the script.
  18. # It sets the `script_path` variable to the path of the "ci" directory under the current directory.
  19. # It sets the `extension` variable to the value passed as an argument to the script.
  20. function find_file() {
  21. directory="$1"
  22. extension="$2"
  23. file=$(find "$directory" -type f -name "click-complete.$extension")
  24. # Searches for a file with the name "click-complete.<extension>" in the given directory.
  25. # Assigns the file path to the `file` variable.
  26. # Check if file was found
  27. if [[ -n "$file" ]]; then
  28. echo "$file"
  29. return 0
  30. else
  31. return 1
  32. fi
  33. }
  34. function main() {
  35. script_path="$current_dir/flexmeasures/cli/scripts"
  36. extension="$1"
  37. if [[ "$extension" != "bash" && "$extension" != "fish" && "$extension" != "zsh" ]]; then
  38. echo "Invalid extension. Only 'bash', 'fish', or 'zsh' extensions are allowed."
  39. exit 1
  40. fi
  41. # In case file is found, then add the complete path in the required file
  42. if found_file=$(find_file "$script_path" "$extension"); then
  43. save_path "$found_file" "$extension"
  44. else
  45. echo "No file found with $extension in its name."
  46. exit 1
  47. fi
  48. }
  49. # Run the main function with the given extension argument
  50. main "$1"