๐Ÿ“ฆ Libraries

๐Ÿ“ฆ Libraries

๐Ÿงฉ Source libraries from your command function

Useful function are accessible by including a Valet library in your script or command function. For this, you need to source the library that you need, e.g.:

source string
source interactive

All Valet functions are prefixed with the library name. E.g. the function string::cutField is from the string library. A clear error message will be output if you are trying to use a library function without sourcing the library.

๐Ÿ’ก
The bash built-in source is overridden by a function in Valet. This allows to not source the same file twice, so you can safely call source mylibrary several times without impacting the runtime performance. If you need to use the default source keyword, use builtin source.

๐Ÿช„ Use Valet functions directly in bash

Thanks to the self export command, you can export Valet functions so they are usable directly in your bash session:

eval "$(valet self export -a)"

log::info "Cool logs!"
if interactive::promptYesNo "Do you want to continue?"; then echo "Yes."; else echo "No."; fi

๐Ÿ“— Add a new library

You can add your own libraries as you can add new commands to extend Valet functionalities.

User libraries are read from the Valet user directory which defaults to ~/.valet.d. They are expected to be bash scripts starting with lib- directly placed under a libs.d directory.

Here is an example content for your user directory:

      • lib-gitlab
      • lib-github
        • lib-git
  • Each function that you want to expose as a library function should be commented following a strict format. Many illustrations can be found in the extras/lib-valet script or in the core library scripts.

    An example is given below:

    # ## mylib::myfunction
    # 
    # A description of my function.
    #
    # Can be done in multiple paragraph and should be formatted as **markdown**.
    #
    # The following argument descriptions should be formatted precisely in order to generate
    # the correct vscode snippets.
    # 
    # - $1: **argument name** _as type_:
    #       The description of the first mandatory argument.
    #
    #       Can also be on multiple lines, be careful of the indentation.
    # - $2: optional argument name _as string_:
    #       (optional) This one is optional. It should not be emphasized (like the previous **argument name**).
    #       (defaults to empty string)
    # - $@: more args _as string_:
    #       For functions that take an undetermined number of arguments, you can use $@.
    # 
    # Returns:
    # 
    # - $?: 0 if ok, 1 otherwise.
    # - `RETURNED_VALUE`: The first returned value
    # - `RETURNED_ARRAY`: A second returned value, as array
    # 
    # ```bash
    # mylib::myfunction arg1 && echo "${RETURNED_VALUE}"
    # mylib::myfunction arg1 optional_arg2 && echo "${RETURNED_VALUE}"
    # ```
    # 
    # > A comment on this particular function.
    function mylib::myfunction() { :; }

    Once defined, run valet self build to let Valet find your custom libraries. Finally, you can use valet self document to update your libraries documentation and vscode snippets.

    ๐ŸŽ€ Available core libraries

    For more details, please check the documentation on each library: