Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input checks to functions #160

Merged
merged 3 commits into from
May 15, 2016
Merged

Conversation

moogle19
Copy link
Contributor

Because missing arguments could led to undefined behaviour, input checks were added for all methods which require input parameters.
I looked at the 'zfs' output when parameters are missing for the error message format.

Because missing arguments could led to undefined behaviour or
partly created machines, input checks were added for all methods which
require input parameters.
EpiJunkie added a commit to chyves/chyves that referenced this pull request May 1, 2016
Added function __verify_number_of_arguments to verify the number of
command parameters supplied by the user.

To utilize this function
    setup)    __verify_number_of_arguments “2” "$#"
              __setup "$@"
              exit
    ;;
    destroy)  __verify_number_of_arguments “2” "$#" “2”
              __destroy "$@"
              exit
    ;;

This is my implementation of:
pr1ntf/iohyve#160
@EpiJunkie
Copy link
Contributor

Might it be better to use a function that gets ran in __parse_cmd to validate? That way the validation code can be kept out of the function doing the work. You could use $# to return the number of parameters used from the command line.

I was thinking something like this:

# Verifies the number of parameters and exits if met or exceeded
__verify_number_of_arguments() {
    local _number_of_minimum_parameters=$1        # Minimum number of parameters
    local _number_of_supplied_parameters=$2       # As counted by "$#", has to be ran from __parse_cmd() and called by __parse_cmd $@
    local _number_of_maximum_parameters=$3        # (Optional) Maximum number of parameters

    # While $3 is optional, it does need a value if not set.
    [ -z "${_number_of_maximum_parameters}" ] && local _number_of_maximum_parameters=$_number_of_supplied_parameters

    if [ "${_number_of_supplied_parameters}" -lt "${_number_of_minimum_parameters}" ] || [ "${_number_of_supplied_parameters}" -gt "${_number_of_maximum_parameters}" ]; then
        __help
        echo "Incorrect number of arguments used. Please see above for correct syntax."
                exit 1
    fi
}

And then this gets utilized in __parse_cmd this way:

    list)     __verify_number_of_arguments "1" "$#" "2"
              __list "$2"
              exit
    ;;
    create)   __verify_number_of_arguments "3" "$#" "4"
              __create "$@"
              exit
    ;;
    set)   __verify_number_of_arguments "3" "$#"
              __set "$@"
              exit
    ;;

Thoughts?

@pr1ntf
Copy link
Owner

pr1ntf commented May 2, 2016

I agree with @EpiJunkie on this. This seems easier in the long run, and helps "future proof" the idea.

@EpiJunkie
Copy link
Contributor

@moogle19 I can submit this as a PR if you do not wish to. If you do though, I request you leave my name and handle in the commit message. I know that is pretty vain.

Either way, thank you for the effort.

@pr1ntf
Copy link
Owner

pr1ntf commented May 2, 2016

"You're so vain you probably think this PR is about you"

reference

@EpiJunkie
Copy link
Contributor

lawlz, that should probably be an edition name at some point.

@moogle19
Copy link
Contributor Author

moogle19 commented May 2, 2016

Yeah, that is possible, but it could be difficult for all functions with flags (like delete), because you first have to check for the flag and than for the number of arguments. That could be a bit too much stuff in the __parse_cmd function.

Also the count of arguments is tightly connected to the function. For example if a flag is added or removed and the input check happens in the __parse_cmd function, you would have to change the implementation in two different places/files, which is not ideal.

@EpiJunkie
Copy link
Contributor

Sorry I was unclear, the "$#" has a special meaning in Bourne. "$#" supplies the number of parameters and "$#" is the literal variable you would use. See here.

For delete the call to verify the number of inputs from __parse_cmd would be:

      delete)   __verify_number_of_arguments "2" "$#" "3"
                __delete "$@"
                exit
      ;;

Where "2" is the minimum number of parameters accepted (without the "-f" flag used) and the "3" is the maximum number of parameters accepted for this command (when the "-f" is used.)

Each case in __parse_cmd would have it's own call to the __verify_number_of_arguments function with it's appropriate parameters to check.

@moogle19
Copy link
Contributor Author

moogle19 commented May 2, 2016

But wouldn't that allow something like iohyve delete -f to be verified successfully, even if it is not allowed?

@EpiJunkie
Copy link
Contributor

Agreed, I realize my suggestion is not a complete solution but it is a step in the right direction, as is your suggestion. With this PR we are suggesting parameter count verification and not input validity.

@moogle19
Copy link
Contributor Author

moogle19 commented May 2, 2016

Just thought about it and realized that my comment was stupid, because something like iohyve delete -f -f would be verified successfully in my implementation and is not allowed.

@EpiJunkie
Copy link
Contributor

EpiJunkie commented May 2, 2016

It is a valid point.

@pr1ntf
Copy link
Owner

pr1ntf commented May 6, 2016

@moogle19 makes a very good point on the flags. I think in order to future-proof this idea, we should add a Contributing document to the Wiki to remind future devs to add this type of check if they intend to add a function to iohyve.

I'll get started on that document if you would like to fix the conflicts, @moogle19.

@pr1ntf
Copy link
Owner

pr1ntf commented May 15, 2016

AYYYYYYYY only two weeks wait time this time. My bad.

@pr1ntf pr1ntf merged commit fc74131 into pr1ntf:master May 15, 2016
@moogle19
Copy link
Contributor Author

@moogle19 moogle19 deleted the parameter_checks branch May 29, 2016 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants