diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | setup-user.in | 66 | ||||
-rwxr-xr-x | tests/setup_user_test | 57 |
3 files changed, 124 insertions, 0 deletions
@@ -25,6 +25,7 @@ SBIN_FILES := copy-modloop\ setup-proxy\ setup-sshd\ setup-timezone\ + setup-user\ setup-xen-dom0\ setup-xorg-base\ update-conf\ diff --git a/setup-user.in b/setup-user.in new file mode 100644 index 0000000..8b9e6cf --- /dev/null +++ b/setup-user.in @@ -0,0 +1,66 @@ +#!/bin/sh + +PREFIX=@PREFIX@ +: ${LIBDIR=$PREFIX/lib} +. "$LIBDIR/libalpine.sh" + +usage() { + cat <<-__EOF__ + usage: setup-user [-h] [-f FULLNAME] [USERNAME] + + Create user account + + options: + -h Show this help + -f Set full name for user + + If USERNAME is not specified user will be prompted. + __EOF__ + exit $1 +} + +while getopts "f:h" opt; do + case $opt in + h) usage 0;; + f) fullname="$OPTARG";; + '?') usage "1" >&2;; + esac +done +shift $(($OPTIND - 1)) + +if [ $# -gt 1 ]; then + usage "1" >&2 +elif [ $# -eq 1 ]; then + username="$1" + nopassword="-D" +else + interactive=1 +fi + +if [ -n "$interactive" ] && [ -z "$fullname" ]; then + ask "Enter full name for user account (or 'skip')" + case "$resp" in + skip) exit 0;; + *) fullname="$resp";; + esac +fi + +if [ -n "$interactive" ] && [ -z "$username" ]; then + while true; do + ask "Enter username for $fullname:" + username="$resp" + if [ -n "$fullname" ]; then + $MOCK adduser -g "$fullname" $nopassword "$username" && break + else + $MOCK adduser $nopassword "$username" && break + fi + done +else + if [ -n "$fullname" ]; then + $MOCK adduser -g "$fullname" $nopassword "$username" + else + $MOCK adduser $nopassword "$username" + fi +fi + + diff --git a/tests/setup_user_test b/tests/setup_user_test new file mode 100755 index 0000000..c2c7ce0 --- /dev/null +++ b/tests/setup_user_test @@ -0,0 +1,57 @@ +#!/usr/bin/env atf-sh + +. $(atf_get_srcdir)/test_env.sh +init_tests \ + setup_user_usage \ + setup_user_fullname \ + setup_user_interactive \ + setup_user_interactive_fullname + +setup_user_usage_body() { + test_usage setup-user +} + +setup_user_body() { + init_env + atf_check -s exit:0 \ + -o match:"adduser -D testuser" \ + -e empty \ + setup-user testuser +} + +setup_user_fullname_body() { + init_env + atf_check -s exit:0 \ + -o match:"adduser.* -D .*testuser" \ + -o match:"adduser.* -g Joe User .*testuser" \ + -e empty \ + setup-user -f "Joe User" testuser +} + +setup_user_interactive_body() { + init_env + ( + echo "Joe User" + echo "testuser" + ) | atf_check -s exit:0 \ + -o match:"Enter full name" \ + -o match:"Enter username" \ + -o not-match:"adduser.* -D .*testuser" \ + -o match:"adduser.* -g Joe User .*testuser" \ + -e empty \ + setup-user +} + +setup_user_interactive_fullname_body() { + init_env + ( + echo "testuser" + ) | atf_check -s exit:0 \ + -o not-match:"Enter full name" \ + -o match:"Enter username" \ + -o not-match:"adduser.* -D .*testuser" \ + -o match:"adduser.* -g Joe User .*testuser" \ + -e empty \ + setup-user -f "Joe User" +} + |