Comparing version 1.0.6 to 1.1.0
{ | ||
"name": "flickr-cli", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "command line interface for flickr", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,6 +7,9 @@ const commandLineArgs = require('command-line-args'); | ||
{ name: 'upload', description: "Upload files", typeLabel: '[underline]{file} [file...]', alias: 'u', type: String, multiple: true }, | ||
{ name: 'addToAlbum', alias: "a", type: String, multiple: true} | ||
{ name: 'album', description: "CRUD for albums", alias: "a", type: String, multiple: true }, | ||
{ name: 'include', alias: "i", type: String, multiple: true}, | ||
{ name: 'noheaders', description: "Do not include headers in a list command", alias: "H", type: Boolean} | ||
]; | ||
Commands = { | ||
Album: "album", | ||
Help: "help", | ||
@@ -23,2 +26,3 @@ Login: "login", | ||
this.parse = this.parse.bind(this); | ||
this.parseAlbum = this.parseAlbum.bind(this); | ||
this.parseHelp = this.parseHelp.bind(this); | ||
@@ -29,2 +33,3 @@ this.parseLogin = this.parseLogin.bind(this); | ||
this.parsers = [ | ||
this.parseAlbum, | ||
this.parseHelp, | ||
@@ -67,2 +72,14 @@ this.parseLogin, | ||
parseAlbum(args) { | ||
if (args.album) { | ||
const include = args.include || []; | ||
const params = { | ||
command: args.album[0], | ||
include, | ||
headers: args.noheaders !== true // (we want a real bool, not a truthy indicator) | ||
}; | ||
return this.makeRes(Commands.Album, params); | ||
} | ||
} | ||
addDashIfMissing(argv) { | ||
@@ -69,0 +86,0 @@ if (argv[2] && argv[2].length > 2 && !argv[2].startsWith("--")) { |
@@ -19,5 +19,5 @@ const usage = require("command-line-usage"); | ||
const message = usage(structure); | ||
console.log(message); | ||
config.logger.log(message); | ||
}; | ||
module.exports = help; |
const login = async (args, config, flickr) => { | ||
const token = await flickr.login(); | ||
console.log(`Logged in as ${token.fullname}`); | ||
config.logger.log(`Logged in as ${token.fullname}`); | ||
config.setToken(token); | ||
@@ -5,0 +5,0 @@ }; |
@@ -1,7 +0,7 @@ | ||
const unknown = async (args) => { | ||
const unknown = async (args, config) => { | ||
args.unknown.shift(); | ||
args.unknown.shift(); | ||
console.log(`Unknown command: ${args.unknown}`) | ||
config.logger.log(`Unknown command: ${args.unknown}`) | ||
}; | ||
module.exports = unknown; |
const {RetryPolicies, Retry} = require("../utils/retry"); | ||
const retry = new Retry(5, 1000, RetryPolicies.exponential); | ||
const upload = async (params, config, flickr) => { | ||
const retry = new Retry(5, 1000, RetryPolicies.exponential, config.logger); | ||
for (const file of params.files) { | ||
process.stdout.write(`Uploading ${file} `); | ||
config.logger.write(`Uploading ${file} `); | ||
try { | ||
const res = await retry.retry(async () => await flickr.upload(file)); | ||
process.stdout.write(` OK - photoid: ${res.photoid._content}\n`); | ||
config.logger.log(`photoid: ${res.photoid._content}`); | ||
} | ||
catch (error) | ||
{ | ||
console.error(`Failed to upload ${file}: ${error.message}`); | ||
config.logger.error(`Failed to upload ${file}: ${error.message}`); | ||
} | ||
@@ -16,0 +16,0 @@ } |
const LocalStorage = require('node-localstorage').LocalStorage; | ||
const path = require("path"); | ||
const homedir = require("homedir"); | ||
const ConsoleLogger = require("./ConsoleLogger"); | ||
@@ -10,2 +11,3 @@ class Configuration { | ||
this.logger = new ConsoleLogger(); | ||
this.getToken = this.getToken.bind(this); | ||
@@ -12,0 +14,0 @@ this.setToken = this.setToken.bind(this); |
@@ -45,3 +45,3 @@ const Flickr = require("flickr-sdk"); | ||
const { oauth_token, oauth_token_secret } = res.body; | ||
console.log(`Go to this URL and authorize the application: ${oauth.authorizeUrl(oauth_token, "write")}`); | ||
this.configuration.logger.log(`Go to this URL and authorize the application: ${oauth.authorizeUrl(oauth_token, "write")}`); | ||
const verifier = await listenToAnswer(); | ||
@@ -59,4 +59,20 @@ const verifyRes = await oauth.verify(oauth_token, verifier, oauth_token_secret); | ||
} | ||
async listAlbums() { | ||
const generateFieldFlattener = (field) => (entry) => { | ||
entry[field] = entry[field]["_content"]; | ||
return entry; | ||
}; | ||
const auth = this.getFlickrAuth(); | ||
const flickr = new Flickr(auth); | ||
const res = await flickr.photosets.getList(); | ||
const list = res.body.photosets.photoset; | ||
const flattenedList = list | ||
.map(generateFieldFlattener("title")) | ||
.map(generateFieldFlattener("description")); | ||
return flattenedList; | ||
} | ||
} | ||
module.exports = FlickrAdapter; |
@@ -18,6 +18,7 @@ const waitAsync = (timeout) => { | ||
constructor(retryCount = 5, initialTimeout = 1000, retryPolicy = retryPolicies.constant) { | ||
constructor(retryCount = 5, initialTimeout = 1000, retryPolicy = retryPolicies.constant, logger = console) { | ||
this.retryCount = retryCount; | ||
this.retryPolicy = retryPolicy; | ||
this.initialTimeout = initialTimeout; | ||
this.logger = logger; | ||
@@ -35,7 +36,7 @@ this.retry = this.retry.bind(this); | ||
} catch(error) { | ||
this.logger.error(error.message); | ||
if (++i >= this.retryCount) | ||
throw error; | ||
console.error(error.message); | ||
} | ||
console.log(`Retrying in ${timeout / 1000}s`); | ||
this.logger.log(`Retrying in ${timeout / 1000}s`); | ||
await waitAsync(timeout); | ||
@@ -42,0 +43,0 @@ timeout = this.retryPolicy(timeout); |
@@ -56,4 +56,4 @@ const should = require("should"); | ||
for(const cmd of [ | ||
["-u", "123.png", "456.png", "--addToAlbum", "1231234", "456456"], | ||
["something", "something", "--upload", "123.png", "456.png", "-a", "1231234", "-a", "456456"]]){ | ||
["-u", "123.png", "456.png", ], | ||
["something", "something", "--upload", "123.png", "456.png"]]){ | ||
const res = parser.parse(makeArgv(cmd)); | ||
@@ -64,8 +64,28 @@ should(res.command).equal(Commands.Upload); | ||
should(res.params.files).containEql("456.png"); | ||
should(res.params.albums).have.length(2); | ||
should(res.params.albums).containEql("1231234"); | ||
should(res.params.albums).containEql("456456"); | ||
} | ||
}); | ||
context("for albums", () => { | ||
it("should parse album list", () => { | ||
const res = parser.parse(makeArgv(["album", "list"])); | ||
should(res.command).equal(Commands.Album); | ||
should(res.params.command).equal("list"); | ||
should(res.params.include).be.empty(); | ||
}); | ||
it("should parse includes for album list", () => { | ||
const res = parser.parse(makeArgv(["album", "list", "--include", "title", "id"])); | ||
should(res.command).equal(Commands.Album); | ||
should(res.params.command).equal("list"); | ||
should(res.params.include).be.deepEqual(["title", "id"]); | ||
should(res.params.headers).be.true(); | ||
}); | ||
it("should parse headers for album list", () => { | ||
const res = parser.parse(makeArgv(["album", "list", "--include", "title", "id", "--noheaders"])); | ||
should(res.command).equal(Commands.Album); | ||
should(res.params.command).equal("list"); | ||
should(res.params.include).be.deepEqual(["title", "id"]); | ||
should(res.params.headers).be.false(); | ||
}); | ||
}); | ||
}); |
@@ -8,2 +8,3 @@ const td = require("testdouble"); | ||
const config = td.object(["setToken"]); | ||
config.logger = td.object(["log"]); | ||
@@ -10,0 +11,0 @@ flickr = { login: () => Promise.resolve(token) }; |
@@ -10,3 +10,4 @@ const should = require("should"); | ||
const start = Date.now(); | ||
const retry = new Retry(4, 100, RetryPolicies.constant); | ||
const logger = td.object(["log", "error"]); | ||
const retry = new Retry(4, 100, RetryPolicies.constant, logger); | ||
await (retry.retry(async () => { i++; throw error;})).should.be.rejectedWith(error); | ||
@@ -16,11 +17,13 @@ i.should.equal(4); | ||
(Date.now() - start).should.be.below(400); | ||
td.verify(logger.error("error"), {times: 4}); | ||
}); | ||
it("should stop retrying when process works", async () => { | ||
let i = 0; | ||
const logger = td.object(["log", "error"]); | ||
const start = Date.now(); | ||
const retry = new Retry(4, 100, RetryPolicies.constant); | ||
const retry = new Retry(4, 100, RetryPolicies.constant, logger); | ||
const result = await retry.retry(async () => { if (++i < 3) throw error; return 123}); | ||
i.should.equal(3); | ||
should(result).equal(123); | ||
(Date.now() - start).should.be.greaterThan(200); | ||
(Date.now() - start).should.be.greaterThanOrEqual(200); | ||
(Date.now() - start).should.be.below(400); | ||
@@ -30,4 +33,5 @@ }); | ||
it("should stop retrying exponentially", async () => { | ||
const logger = td.object(["log", "error"]); | ||
const start = Date.now(); | ||
const retry = new Retry(4, 100, RetryPolicies.exponential); | ||
const retry = new Retry(4, 100, RetryPolicies.exponential, logger); | ||
await retry.retry(async () => { throw error; }).should.be.rejectedWith(error); | ||
@@ -34,0 +38,0 @@ (Date.now() - start).should.be.greaterThan(100 + 200 + 400); |
@@ -7,2 +7,4 @@ const td = require("testdouble"); | ||
const flickr = td.object(["upload"]); | ||
const config = { logger: td.object(["log", "write"]) }; | ||
const files = [1, 2, 3].map((index) => `${index}.jpg`); | ||
@@ -13,3 +15,3 @@ for (file of files) { | ||
await upload({files}, {}, flickr); | ||
await upload({files}, config, flickr); | ||
@@ -24,9 +26,11 @@ for(file of files) { | ||
const flickr = td.object(["upload"]); | ||
const config = { logger: td.object(["error", "log", "write"]) }; | ||
const file = "ljhkjh"; | ||
td.when(flickr.upload(file)).thenReject(Error("bla")); | ||
await upload({files: [file]}, {}, flickr); | ||
await upload({files: [file]}, config, flickr); | ||
td.verify(flickr.upload(file), {times: 5}); | ||
td.verify(config.logger.error(`Failed to upload ${file}: bla`)); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22441
18
552