From 6cf6eab8fa4d7b64716e4c9a9fa37b289b4aa15f Mon Sep 17 00:00:00 2001
From: Erik Seliger <erikseliger@me.com>
Date: Fri, 31 Aug 2018 21:44:40 +0200
Subject: [PATCH] fix: allow cli json lists (#2457)

Now supports `--endpoints=[........]` via CLI.

Closes #2455
---
 lib/config/cli.js         | 12 ++++++++++--
 lib/config/definitions.js |  1 +
 test/config/cli.spec.js   | 27 +++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/lib/config/cli.js b/lib/config/cli.js
index 77b42d9490..2446d01868 100644
--- a/lib/config/cli.js
+++ b/lib/config/cli.js
@@ -22,8 +22,16 @@ function getConfig(argv) {
 
   const coersions = {
     boolean: val => val === 'true',
-    list: val =>
-      val === '[]' || val === '' ? [] : val.split(',').map(el => el.trim()),
+    list: val => {
+      if (val === '') {
+        return [];
+      }
+      try {
+        return JSON.parse(val);
+      } catch (err) {
+        return val.split(',').map(el => el.trim());
+      }
+    },
     string: val => val,
     integer: parseInt,
   };
diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index 75dec33b5e..79c225e732 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -1161,6 +1161,7 @@ const options = [
     description: 'Endpoint configuration for credentials',
     type: 'list',
     stage: 'repository',
+    cli: true,
     mergeable: true,
   },
 ];
diff --git a/test/config/cli.spec.js b/test/config/cli.spec.js
index f9a08e36ae..b5970bf019 100644
--- a/test/config/cli.spec.js
+++ b/test/config/cli.spec.js
@@ -65,5 +65,32 @@ describe('config/cli', () => {
       argv.push('bar');
       cli.getConfig(argv).should.eql({ repositories: ['foo', 'bar'] });
     });
+    it('parses json lists correctly', () => {
+      argv.push(
+        `--endpoints=[{"host":"docker.io","platform":"docker","username":"user","password":"password"}]`
+      );
+      cli.getConfig(argv).should.deep.equal({
+        endpoints: [
+          {
+            host: 'docker.io',
+            platform: 'docker',
+            username: 'user',
+            password: 'password',
+          },
+        ],
+      });
+    });
+    it('parses [] correctly as empty list of endpoints', () => {
+      argv.push(`--endpoints=[]`);
+      cli.getConfig(argv).should.eql({
+        endpoints: [],
+      });
+    });
+    it('parses an empty string correctly as empty list of endpoints', () => {
+      argv.push(`--endpoints=`);
+      cli.getConfig(argv).should.eql({
+        endpoints: [],
+      });
+    });
   });
 });
-- 
GitLab