commit 0bb8d7eb7f32e7286ff1c3c6f1c0c855b27159e4
parent 18021f665e57d21ca8042c2600c62cf85481d200
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sat, 14 Sep 2024 09:34:57 +0000
Random command
Diffstat:
4 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
@@ -60,7 +60,10 @@ how different it is; but now it installs `.age-recipients` and re-encrypts.
 
 ### New Features and Extensions
 
-TODO
+- The new `random` command leverages password generation without touching
+the password store.
+
+- TODO
 
 ## Manual
 
diff --git a/spec/usage_spec.sh b/spec/usage_spec.sh
@@ -1416,6 +1416,37 @@ Describe 'Command-Line Parsing'
     End
   End
 
+  Describe 'cmd_random'
+    COMMAND=generate
+    GENERATED_LENGTH=25
+
+    random_chars() { mocklog random_chars "$@"; }
+
+    It 'generates random characters with default parameters'
+      When run cmd_random
+      The error should equal \
+        "$ random_chars ${GENERATED_LENGTH} ${CHARACTER_SET}"
+    End
+
+    It 'generates random characters with default character set'
+      When run cmd_random 8
+      The error should equal "$ random_chars 8 ${CHARACTER_SET}"
+    End
+
+    It 'generates random characters with the given arguments'
+      When run cmd_random 8 a-z
+      The error should equal "$ random_chars 8 a-z"
+    End
+
+    It 'reports too many arguments'
+      cat() { @cat; }
+      When run cmd_random 1 2 3
+      The output should be blank
+      The error should equal 'Usage: prg random [pass-length [character-set]]'
+      The status should equal 1
+    End
+  End
+
   Describe 'cmd_usage'
     COMMAND=usage
     CLIP_TIME='$CLIP_TIME'
@@ -1443,6 +1474,7 @@ Describe 'Command-Line Parsing'
       The output should include 'prg insert'
       The output should include 'prg [list]'
       The output should include 'prg move'
+      The output should include 'prg random'
       The output should include 'prg [show]'
       The output should include 'prg version'
     End
diff --git a/src/pashage.sh b/src/pashage.sh
@@ -1411,6 +1411,15 @@ cmd_move() {
 	cmd_copy_move "$@"
 }
 
+cmd_random() {
+	if [ $# -gt 2 ]; then
+		cmd_usage 'Usage: ' random >&2
+		exit 1
+	fi
+
+	random_chars "${1:-${GENERATED_LENGTH}}" "${2:-${CHARACTER_SET}}"
+}
+
 # Outputs the whole usage text
 #   $1: indentation
 #   ... commands to document
@@ -1432,7 +1441,7 @@ cmd_usage(){
 	if [ $# -eq 0 ]; then
 		echo 'Usage:'
 		set -- list show copy delete edit find generate \
-		    git gitconfig grep help init insert move version
+		    git gitconfig grep help init insert move random version
 		VERBOSE=yes
 	else
 		VERBOSE=no
@@ -1577,6 +1586,16 @@ ${I}    Renames or moves old-path to new-path, optionally forcefully,
 ${I}    selectively reencrypting.
 EOF
 			;;
+		    random)
+			cat <<EOF
+${F}${PROGRAM} random [pass-length [character-set]]
+EOF
+			[ "${VERBOSE}" = yes ] && cat <<EOF
+${I}    Generate a new password of pass-length (or ${GENERATED_LENGTH:-25} if unspecified)
+${I}    using the given character set (or ${CHARACTER_SET} if unspecified)
+${I}    without recording it in the password store.
+EOF
+			;;
 		    version)
 			cat <<EOF
 ${F}${PROGRAM} version
diff --git a/src/run.sh b/src/run.sh
@@ -114,6 +114,7 @@ case "${COMMAND}" in
     list)	shift; cmd_list_or_show "$@" ;;
     ls)		shift; cmd_list_or_show "$@" ;;
     move|mv)	shift; cmd_move "$@" ;;
+    random)	shift; cmd_random "$@" ;;
     remove)	shift; cmd_delete "$@" ;;
     rm)		shift; cmd_delete "$@" ;;
     show)	shift; cmd_list_or_show "$@" ;;