1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
npm owner add <username> <pkg>
npm owner rm <username> <pkg>
npm owner ls <pkg>
*/
module.exports = owner
var registry = require("./utils/registry")
, get = registry.request.GET
, put = registry.request.PUT
, log = require("./utils/log")
function owner (args, cb) {
var action = args.shift()
switch (action) {
case "ls": case "list": return ls(args[0], cb)
case "add": return add(args[0], args[1], cb)
case "rm": case "remove": return rm(args[0], args[1], cb)
default: return unknown(action, cb)
}
}
function ls (pkg, cb) {
get(pkg, function (er, data) {
if (er) return log.er(cb, "Couldn't get owner data for "+pkg)(er)
var owners = data.maintainers
if (!owners || !owners.length) console.log("admin party!")
else owners.forEach(function (o) { console.log(o.name +" <"+o.email+">") })
cb()
})
}
function add (user, pkg, cb) {
log.verbose(user+" to "+pkg, "owner add")
mutate(pkg, user, function (u, owners) {
if (!owners) owners = []
for (var i = 0, l = owners.length; i < l; i ++) {
var o = owners[i]
if (o.name === u.name || o.email === u.email) {
log( "Already a package owner: "+o.name+" <"+o.email+">"
, "owner add"
)
return false
}
}
owners.push(u)
return owners
}, cb)
}
function rm (user, pkg, cb) {
log.verbose(user+" from "+pkg, "owner rm")
mutate(pkg, user, function (u, owners) {
var found = false
, m = owners.filter(function (o) {
var match = (o.name === u.name || o.email === u.email)
found = found || match
return !match
})
if (!found) {
log("Not a package owner: "+u.name+" <"+u.email+">", "owner rm")
return false
}
if (!m.length) return new Error(
"Cannot remove all owners of a package. Add someone else first.")
return m
}, cb)
}
function mutate (pkg, user, mutation, cb) {
get("/getuser/org.couchdb.user:"+user, function (er, u) {
if (er) return log.er(cb, "Error getting user data for "+user)(er)
if (!u || u.error) return cb(new Error(
"Couldn't get user data for "+user+": "+JSON.stringify(u)))
get("/"+pkg, function (er, data) {
if (er) return log.er(cb, "Couldn't get package data for "+pkg)(er)
var m = mutation({ "name" : u.name, "email" : u.email }, data.maintainers)
if (!m) return cb() // handled
if (m instanceof Error) return cb(m) // error
data = { _id : data._id
, _rev : data._rev
, maintainers : m
}
put("/"+pkg+"/-rev/"+data._rev, data, function (er, data) {
if (er) return log.er(cb, "Failed to update package metadata")(er)
if (data.error) return cb(new Error(
"Failed to update pacakge metadata: "+JSON.stringify(data)))
cb(null, data)
})
})
})
}
function unknown (action, cb) {
cb(new Error("Unrecognized owner command "+action))
}
|