1 | fs = require "fs"
|
2 | colors = require "colors"
|
3 | {ask, logger} = require "../lib/utils"
|
4 |
|
5 | log = logger "config"
|
6 |
|
7 | {findFile, guard} = require '../modules/common/helpers'
|
8 |
|
9 |
|
10 | module.exports = class Config
|
11 |
|
12 | alias:
|
13 | ls: "list"
|
14 |
|
15 | help:"""
|
16 | Koding configuration settings.
|
17 | """
|
18 |
|
19 | constructor: (@configFile)->
|
20 | guard not @configFile.config.publicKey, "you need to set your public key"
|
21 |
|
22 |
|
23 | set: (key, value...)=>
|
24 | unless key and value
|
25 | return log "You must define a key and a value to set a config variable."
|
26 | @configFile.set key, value...
|
27 | @configFile.save()
|
28 |
|
29 | guard not @configFile.config.publicKey, "you need to set your public key"
|
30 |
|
31 |
|
32 | get: (path)=>
|
33 | config = @configFile.getAll()
|
34 | try
|
35 | value = eval "config.#{path}"
|
36 | catch error
|
37 | log "Probably the path #{path} is wrong.", "red"
|
38 | process.exit 1
|
39 |
|
40 | unless value
|
41 | log "Config #{key} is not defined.", "red"
|
42 | process.exit 1
|
43 |
|
44 | console.log value
|
45 |
|
46 | remove: (path)=>
|
47 | config = @configFile.getAll()
|
48 |
|
49 | try
|
50 | eval "delete config.#{path}"
|
51 | catch error
|
52 | log "Probably the path #{path} is wrong.", "red"
|
53 | process.exit 1
|
54 |
|
55 |
|
56 | @configFile.save()
|
57 |
|
58 | list:=>
|
59 | ask "Your config file may contain confidental information, do you want to list? [yN]",
|
60 | format: /[yN]/
|
61 | callback: (answer)=>
|
62 | if answer is "y"
|
63 | config = @config.getAll()
|
64 | @configFile.disabled.forEach (item)=> delete config[item]
|
65 | log JSON.stringify config, null, 2
|
66 | process.exit()
|
67 |
|
68 | alias: (alias, module)=>
|
69 | return unless alias or module
|
70 | config = @configFile.getAll()
|
71 | config.alias = {} unless config.alias
|
72 | config.alias[alias] = module
|
73 | @configFile.save()
|
74 |
|
75 |
|
76 | username: (cb)=>
|
77 | ask "#{colors.yellow "config:"} What is your Koding username? (Leave blank to set as #{colors.blue process.env.USER}):",
|
78 | format: /^([a-z\d]+([-][a-z\d]+)*){4,25}$|^$/i
|
79 | callback: (answer)=>
|
80 | unless answer
|
81 | log "Setting your username to #{colors.green process.env.USER}", "blue"
|
82 | answer = process.env.USER
|
83 | @set "user.name", answer
|
84 | @set "username", answer
|
85 | if typeof cb is "function"
|
86 | cb(@configFile) |
\ | No newline at end of file |