Explorar o código

CLI user-info JSON option (#2968)

* CLI user-info JSON option

Add a JSON user-info output
https://github.com/FreshRSS/FreshRSS/issues/2967#issuecomment-627441744

* Uses ISO Zulu time

* Typo
Alexandre Alapetite %!s(int64=5) %!d(string=hai) anos
pai
achega
058d13de70
Modificáronse 2 ficheiros con 39 adicións e 7 borrados
  1. 17 3
      cli/README.md
  2. 22 4
      cli/user-info.php

+ 17 - 3
cli/README.md

@@ -61,9 +61,10 @@ cd /usr/share/FreshRSS
 ./cli/list-users.php
 # Return a list of users, with the default/admin user first
 
-./cli/user-info.php -h --header --user username1 --user username2 ...
+./cli/user-info.php [ -h --header --json --user username1 --user username2 ... ]
 # -h is to use a human-readable format
 # --header outputs some columns headers
+# --json JSON format (disables --header and -h but uses ISO Zulu format for dates)
 # --user indicates a username, and can be repeated
 # Returns: 1) a * if the user is admin, 2) the name of the user,
 #  3) the date/time of last user action, 4) the size occupied,
@@ -110,7 +111,7 @@ Now, cron will send you an email only if the exit code is non-zero and with the
 ## Unix piping
 
 It is possible to invoke a command multiple times, e.g. with different usernames, thanks to the `xargs -n1` command.
-Example showing user information for all users which username starts with 'a':
+Example showing user information for all users which username starts with ‘a’:
 
 ```sh
 ./cli/list-users.php | grep '^a' | xargs -n1 ./cli/user-info.php -h --user
@@ -119,15 +120,28 @@ Example showing user information for all users which username starts with 'a':
 Example showing all users ranked by date of last activity:
 
 ```sh
-./cli/user-info.php -h --user '*' | sort -k2 -r
+./cli/user-info.php -h | sort -k2 -r
 ```
 
 Example to get the number of feeds of a given user:
 
 ```sh
 ./cli/user-info.php --user alex | cut -f6
+#or
+./cli/user-info.php --user alex --json | jq '.[] | .feeds'
 ```
 
+Example to get the name of the users who have not been active since a given date:
+
+```sh
+cli/user-info.php --json | jq '.[] | select(.last_user_activity < "2020-05-01") | .user'
+```
+
+Example to get the date and name of users who have not been active the past 24 hours (86400 seconds):
+
+```sh
+cli/user-info.php --json | jq -r '.[] | select((.last_user_activity | fromdate) < (now - 86400)) | [.last_user_activity, .user] | @csv'
+```
 
 # Install and updates
 

+ 22 - 4
cli/user-info.php

@@ -7,11 +7,12 @@ const DATA_FORMAT = "%-7s | %-20s | %-25s | %-15s | %-10s | %-10s | %-10s | %-10
 $params = array(
 	'user:',
 	'header',
+	'json',
 );
 $options = getopt('h', $params);
 
 if (!validateOptions($argv, $params)) {
-	fail('Usage: ' . basename(__FILE__) . ' (-h --header --user username --user username …)');
+	fail('Usage: ' . basename(__FILE__) . ' (-h --header --json --user username --user username …)');
 }
 
 if (empty($options['user'])) {
@@ -24,10 +25,17 @@ if (empty($options['user'])) {
 
 sort($users);
 
+$formatJson = isset($options['json']);
+if ($formatJson) {
+	unset($options['header']);
+	unset($options['h']);
+	$jsonOutput = [];
+}
+
 if (array_key_exists('header', $options)) {
 	printf(
 		DATA_FORMAT,
-		'default',
+		'is_default',
 		'user',
 		'last user activity',
 		'space used',
@@ -56,7 +64,7 @@ foreach ($users as $username) {
 	$nbFavorites = $entryDAO->countUnreadReadFavorites();
 
 	$data = array(
-		'default' => $username === FreshRSS_Context::$system_conf->default_user ? '*' : '',
+		'is_default' => $username === FreshRSS_Context::$system_conf->default_user ? '*' : '',
 		'user' => $username,
 		'last_user_activity' => FreshRSS_UserDAO::mtime($username),
 		'database_size' => $databaseDAO->size(),
@@ -73,7 +81,17 @@ foreach ($users as $username) {
 		$data['last_user_activity'] = date('c', $data['last_user_activity']);
 		$data['database_size'] = format_bytes($data['database_size']);
 	}
-	vprintf(DATA_FORMAT, $data);
+	if ($formatJson) {
+		$data['is_default'] = !empty($data['is_default']);
+		$data['last_user_activity'] = gmdate('Y-m-d\TH:i:s\Z', $data['last_user_activity']);
+		$jsonOutput[] = $data;
+	} else {
+		vprintf(DATA_FORMAT, $data);
+	}
+}
+
+if ($formatJson) {
+	echo json_encode($jsonOutput), "\n";
 }
 
 done();