#763130 Improved version of "on" function

#763130#5
Date:
2014-09-28 02:15:47 UTC
From:
To:
I'm using an improved version of the on() function in my .mrconfig,
which handles usernames, etc. Maybe you'd like to include it in the
defaults for mr, so people don't have to reinvent the wheel. Here it is:

               # Checks whether the given host basename is one of the listed
               # values. A value can also have a username in it, ie
"joey@dodo".
               on() {
                       for host in $@; do
                               # Handle username specfication.
                               if [ "${host%@*}" != "${host#*@}" ]; then
                                       if [ "$whoami" != "${host%@*}" ];
then
                                               continue
                                       fi
                                       # Now strip username from host
variable.
                                       host="${host#*@}"
                               fi
                               # Check hostname
                               if [ "$shorthostname" = "$host" -o
"$fullhostname" = "$host" -o \
                                    "$domainname" = "$host" ]; then
                                       return 0
                               fi
                       done
                       return 1
               }

#763130#10
Date:
2014-09-28 17:04:50 UTC
From:
To:
Christian Hudon wrote:

None of these variables seem to be defined in your patch.

#763130#15
Date:
2014-09-28 17:08:19 UTC
From:
To:
Le 2014-9-28 13:04, Joey Hess a écrit :

Doh! This should be the full thing.

                # 'Handy 'on' alias.
                whoami="$(whoami)"
                shorthostname="$(hostname -s)"
                fullhostname="$(hostname -f)"
                domainname="${fullhostname#*.}"
                # Checks whether the given host basename is one of the
listed
                # values. A value can also have a username in it, ie
"joey@dodo".
                on() {
                        for host in $@; do
                                # Handle username specfication.
                                if [ "${host%@*}" != "${host#*@}" ]; then
                                        if [ "$whoami" != "${host%@*}"
]; then
                                                continue
                                        fi
                                        # Now strip username from host
variable.
                                        host="${host#*@}"
                                fi
                                # Check hostname
                                if [ "$shorthostname" = "$host" -o
"$fullhostname" = "$host" -o \
                                     "$domainname" = "$host" ]; then
                                        return 0
                                fi
                        done
                        return 1
                }

#763130#20
Date:
2014-09-28 18:47:26 UTC
From:
To:
Christian Hudon wrote:

These commands would run every time myrepos is run, even if `on` is not
used.

I think it would probably be better to put the function in a lib/ file.

#763130#25
Date:
2014-09-28 21:05:05 UTC
From:
To:
Le 2014-9-28 14:47, Joey Hess a écrit :

That was a not a problem for me, because in my .mrconfig file, the on()
function is run every time. But good point in general. Anyways, I guess
the initial version of this on() function in this bug report was more of
a "Would you potentially be interested in this?" thing. If you are
interested and give me a list of things to fix, I have no problems
reworking this function to fix them. Let me know.

   Christian

#763130#30
Date:
2014-09-29 18:34:58 UTC
From:
To:
Christian Hudon wrote:

I am definitely interested -- perhaps it would make sense for it to get
and cache the hostname values only when on() is used.

It also needs to be documented.