Ideas for a nicer way to support different os's/implementation in types #157

Closed
opened 2021-11-20 15:20:25 +00:00 by ungleich-gitea · 3 comments

Created by: asteven

Just thinking out loud.

Instead of the endless case esac if os then else what if a type would have a internal API?
Maybe in form of shell functions?
Then there could be a default implementation, e.g. in __some_type/lib/default
And if some $os doesn't like that it can create it's own implementation in _some_type/lib/$os

The gencode-* would then just call shell functions which write to stdout.
e.g.

_some_type/lib/default:

_add_user() {
   printf 'gpasswd -a "%s" "%s"\n' "$1" "$2"
}

_remove_user() {
   printf 'gpasswd -d "%s" "%s"\n' "$1" "$2"
}

_some_type/lib/netbsd:

_add_user() {
   printf 'usermod -G "%s" "%s"\n' "$1" "$2"
}

_remove_user() {
   printf 'usermod ;;# "%s" "%s"\n' "$1" "$2"
}

_some_type/gencode-remote:

. "$__type/lib/default"
if [ -f "$__type/lib/$os" ]; then
   . "$__type/lib/$os"
fi
...

case "$state_should" in
   present)
      for group in $(comm -13 "$__object/explorer/group" "$__object/files/group.sorted"); do
         _add_user "$group" "$user"
      done
   ;;
   absent)
      for group in $(comm -12 "$__object/explorer/group" "$__object/files/group.sorted"); do
         _remove_user "$group" "$user"
      done
   ;;
esac

One could even implement kind of 'call super' using an aproach like this.

No more stinking spagetti code.
Comments?

*Created by: asteven* Just thinking out loud. Instead of the endless case esac if os then else what if a type would have a internal API? Maybe in form of shell functions? Then there could be a default implementation, e.g. in __some_type/lib/default And if some $os doesn't like that it can create it's own implementation in _some_type/lib/$os The gencode-* would then just call shell functions which write to stdout. e.g. _some_type/lib/default: ```shell _add_user() { printf 'gpasswd -a "%s" "%s"\n' "$1" "$2" } _remove_user() { printf 'gpasswd -d "%s" "%s"\n' "$1" "$2" } ``` _some_type/lib/netbsd: ```shell _add_user() { printf 'usermod -G "%s" "%s"\n' "$1" "$2" } _remove_user() { printf 'usermod ;;# "%s" "%s"\n' "$1" "$2" } ``` _some_type/gencode-remote: ```shell . "$__type/lib/default" if [ -f "$__type/lib/$os" ]; then . "$__type/lib/$os" fi ... case "$state_should" in present) for group in $(comm -13 "$__object/explorer/group" "$__object/files/group.sorted"); do _add_user "$group" "$user" done ;; absent) for group in $(comm -12 "$__object/explorer/group" "$__object/files/group.sorted"); do _remove_user "$group" "$user" done ;; esac ``` One could even implement kind of 'call super' using an aproach like [this](http://mivok.net/2009/09/20/bashfunctionoverrist.html). No more stinking spagetti code. Comments?
ungleich-gitea added the
Stale
label 2021-11-20 15:20:25 +00:00
Author
Owner

mentioned in issue #782

mentioned in issue #782
Author
Owner

closed

closed
Author
Owner

Created by: telmich

Great idea! But I suggest using $__type/files/lib instead

*Created by: telmich* Great idea! But I suggest using $__type/files/lib instead
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: ungleich-public/cdist#157
No description provided.