diff options
| author | Paul Ouellette <oue.paul18@gmail.com> | 2019-06-12 23:49:46 -0400 |
|---|---|---|
| committer | Paul Ouellette <oue.paul18@gmail.com> | 2019-07-30 00:43:53 -0400 |
| commit | d6f296e108972982cf071d42cee37ac0cb122174 (patch) | |
| tree | 36b0dca44b3d4705af582c1f620a96f0f9509418 /src | |
| parent | 3afab9d46de3889241c0f882cb312b8ec4a385ed (diff) | |
| download | luarocks-d6f296e108972982cf071d42cee37ac0cb122174.tar.gz luarocks-d6f296e108972982cf071d42cee37ac0cb122174.tar.bz2 luarocks-d6f296e108972982cf071d42cee37ac0cb122174.zip | |
Move config command to argparse
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/cmd/config.lua | 121 |
1 files changed, 59 insertions, 62 deletions
diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index b3cb5e07..6ec0efdf 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua | |||
| @@ -9,9 +9,10 @@ local deps = require("luarocks.deps") | |||
| 9 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
| 10 | local fs = require("luarocks.fs") | 10 | local fs = require("luarocks.fs") |
| 11 | 11 | ||
| 12 | config_cmd.help_summary = "Query information about the LuaRocks configuration." | 12 | function config_cmd.add_to_parser(parser) |
| 13 | config_cmd.help_arguments = "(<key> | <key> <value> --scope=<scope> | <key> --unset --scope=<scope> | )" | 13 | local cmd = parser:command("config", [[ |
| 14 | config_cmd.help = [[ | 14 | Query information about the LuaRocks configuration. |
| 15 | |||
| 15 | * When given a configuration key, it prints the value of that key | 16 | * When given a configuration key, it prints the value of that key |
| 16 | according to the currently active configuration (taking into account | 17 | according to the currently active configuration (taking into account |
| 17 | all config files and any command-line flags passed) | 18 | all config files and any command-line flags passed) |
| @@ -45,23 +46,35 @@ config_cmd.help = [[ | |||
| 45 | configuration, resulting from reading the config files from | 46 | configuration, resulting from reading the config files from |
| 46 | all scopes. | 47 | all scopes. |
| 47 | 48 | ||
| 48 | Example: luarocks config | 49 | Example: luarocks config]], util.see_also([[ |
| 49 | 50 | https://github.com/luarocks/luarocks/wiki/Config-file-format | |
| 50 | OPTIONS | 51 | for detailed information on the LuaRocks config file format. |
| 51 | --scope=<scope> The scope indicates which config file should be rewritten. | 52 | ]])) |
| 52 | Accepted values are "system", "user" or "project". | 53 | :summary("Query information about the LuaRocks configuration.") |
| 53 | * Using a wrapper created with `luarocks init`, | 54 | :add_help(false) |
| 54 | the default is "project". | 55 | |
| 55 | * Using --local (or when `local_by_default` is `true`), | 56 | cmd:argument("key", "The configuration key.") |
| 56 | the default is "user". | 57 | :args("?") |
| 57 | * Otherwise, the default is "system". | 58 | cmd:argument("value", "The configuration value.") |
| 58 | 59 | :args("?") | |
| 59 | --json Output as JSON | 60 | |
| 60 | ]] | 61 | cmd:option("--scope", "The scope indicates which config file should be rewritten.\n".. |
| 61 | config_cmd.help_see_also = [[ | 62 | 'Accepted values are "system", "user" or "project".\n'.. |
| 62 | https://github.com/luarocks/luarocks/wiki/Config-file-format | 63 | '* Using a wrapper created with `luarocks init`, the default is "project".\n'.. |
| 63 | for detailed information on the LuaRocks config file format. | 64 | '* Using --local (or when `local_by_default` is `true`), the default is "user".\n'.. |
| 64 | ]] | 65 | '* Otherwise, the default is "system".') |
| 66 | :choices({"system", "user", "project"}) | ||
| 67 | cmd:flag("--unset", "Delete the key from the configuration file.") | ||
| 68 | cmd:flag("--json", "Output as JSON.") | ||
| 69 | |||
| 70 | -- Deprecated flags | ||
| 71 | cmd:flag("--lua-incdir"):hidden(true) | ||
| 72 | cmd:flag("--lua-libdir"):hidden(true) | ||
| 73 | cmd:flag("--lua-ver"):hidden(true) | ||
| 74 | cmd:flag("--system-config"):hidden(true) | ||
| 75 | cmd:flag("--user-config"):hidden(true) | ||
| 76 | cmd:flag("--rock-trees"):hidden(true) | ||
| 77 | end | ||
| 65 | 78 | ||
| 66 | local function config_file(conf) | 79 | local function config_file(conf) |
| 67 | print(dir.normalize(conf.file)) | 80 | print(dir.normalize(conf.file)) |
| @@ -218,45 +231,40 @@ local function write_entries(keys, scope, do_unset) | |||
| 218 | end | 231 | end |
| 219 | end | 232 | end |
| 220 | 233 | ||
| 221 | local function check_scope(flags) | 234 | local function get_scope(flags) |
| 222 | local scope = flags["scope"] | 235 | return flags["scope"] |
| 223 | or (flags["local"] and "user") | 236 | or (flags["local"] and "user") |
| 224 | or (flags["project-tree"] and "project") | 237 | or (flags["project_tree"] and "project") |
| 225 | or (cfg.local_by_default and "user") | 238 | or (cfg.local_by_default and "user") |
| 226 | or "system" | 239 | or "system" |
| 227 | if scope ~= "system" and scope ~= "user" and scope ~= "project" then | ||
| 228 | return nil, "Valid values for scope are: system, user, project" | ||
| 229 | end | ||
| 230 | |||
| 231 | return scope | ||
| 232 | end | 240 | end |
| 233 | 241 | ||
| 234 | --- Driver function for "config" command. | 242 | --- Driver function for "config" command. |
| 235 | -- @return boolean: True if succeeded, nil on errors. | 243 | -- @return boolean: True if succeeded, nil on errors. |
| 236 | function config_cmd.command(flags, var, val) | 244 | function config_cmd.command(args) |
| 237 | deps.check_lua_incdir(cfg.variables) | 245 | deps.check_lua_incdir(cfg.variables) |
| 238 | deps.check_lua_libdir(cfg.variables) | 246 | deps.check_lua_libdir(cfg.variables) |
| 239 | 247 | ||
| 240 | -- deprecated flags | 248 | -- deprecated flags |
| 241 | if flags["lua-incdir"] then | 249 | if args["lua_incdir"] then |
| 242 | print(cfg.variables.LUA_INCDIR) | 250 | print(cfg.variables.LUA_INCDIR) |
| 243 | return true | 251 | return true |
| 244 | end | 252 | end |
| 245 | if flags["lua-libdir"] then | 253 | if args["lua_libdir"] then |
| 246 | print(cfg.variables.LUA_LIBDIR) | 254 | print(cfg.variables.LUA_LIBDIR) |
| 247 | return true | 255 | return true |
| 248 | end | 256 | end |
| 249 | if flags["lua-ver"] then | 257 | if args["lua_ver"] then |
| 250 | print(cfg.lua_version) | 258 | print(cfg.lua_version) |
| 251 | return true | 259 | return true |
| 252 | end | 260 | end |
| 253 | if flags["system-config"] then | 261 | if args["system_config"] then |
| 254 | return config_file(cfg.config_files.system) | 262 | return config_file(cfg.config_files.system) |
| 255 | end | 263 | end |
| 256 | if flags["user-config"] then | 264 | if args["user_config"] then |
| 257 | return config_file(cfg.config_files.user) | 265 | return config_file(cfg.config_files.user) |
| 258 | end | 266 | end |
| 259 | if flags["rock-trees"] then | 267 | if args["rock_trees"] then |
| 260 | for _, tree in ipairs(cfg.rocks_trees) do | 268 | for _, tree in ipairs(cfg.rocks_trees) do |
| 261 | if type(tree) == "string" then | 269 | if type(tree) == "string" then |
| 262 | util.printout(dir.normalize(tree)) | 270 | util.printout(dir.normalize(tree)) |
| @@ -268,29 +276,22 @@ function config_cmd.command(flags, var, val) | |||
| 268 | return true | 276 | return true |
| 269 | end | 277 | end |
| 270 | 278 | ||
| 271 | if var == "lua_version" and val then | 279 | if args.key == "lua_version" and args.value then |
| 272 | local scope, err = check_scope(flags) | 280 | local scope = get_scope(args) |
| 273 | if not scope then | ||
| 274 | return nil, err | ||
| 275 | end | ||
| 276 | |||
| 277 | if scope == "project" and not cfg.config_files.project then | 281 | if scope == "project" and not cfg.config_files.project then |
| 278 | return nil, "Current directory is not part of a project. You may want to run `luarocks init`." | 282 | return nil, "Current directory is not part of a project. You may want to run `luarocks init`." |
| 279 | end | 283 | end |
| 280 | 284 | ||
| 281 | local prefix = dir.dir_name(cfg.config_files[scope].file) | 285 | local prefix = dir.dir_name(cfg.config_files[scope].file) |
| 282 | local ok, err = persist.save_default_lua_version(prefix, val) | 286 | local ok, err = persist.save_default_lua_version(prefix, args.value) |
| 283 | if not ok then | 287 | if not ok then |
| 284 | return nil, "could not set default Lua version: " .. err | 288 | return nil, "could not set default Lua version: " .. err |
| 285 | end | 289 | end |
| 286 | print("Lua version will default to " .. val .. " in " .. prefix) | 290 | print("Lua version will default to " .. args.value .. " in " .. prefix) |
| 287 | end | 291 | end |
| 288 | 292 | ||
| 289 | if var == "lua_dir" and val then | 293 | if args.key == "lua_dir" and args.value then |
| 290 | local scope, err = check_scope(flags) | 294 | local scope = get_scope(args) |
| 291 | if not scope then | ||
| 292 | return nil, err | ||
| 293 | end | ||
| 294 | local keys = { | 295 | local keys = { |
| 295 | ["variables.LUA_DIR"] = cfg.variables.LUA_DIR, | 296 | ["variables.LUA_DIR"] = cfg.variables.LUA_DIR, |
| 296 | ["variables.LUA_BINDIR"] = cfg.variables.LUA_BINDIR, | 297 | ["variables.LUA_BINDIR"] = cfg.variables.LUA_BINDIR, |
| @@ -298,25 +299,21 @@ function config_cmd.command(flags, var, val) | |||
| 298 | ["variables.LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, | 299 | ["variables.LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, |
| 299 | ["lua_interpreter"] = cfg.lua_interpreter, | 300 | ["lua_interpreter"] = cfg.lua_interpreter, |
| 300 | } | 301 | } |
| 301 | return write_entries(keys, scope, flags["unset"]) | 302 | return write_entries(keys, scope, args["unset"]) |
| 302 | end | 303 | end |
| 303 | 304 | ||
| 304 | if var then | 305 | if args.key then |
| 305 | if val or flags["unset"] then | 306 | if args.value or args["unset"] then |
| 306 | local scope, err = check_scope(flags) | 307 | local scope = get_scope(args) |
| 307 | if not scope then | 308 | return write_entries({ [args.key] = args.value }, scope, args["unset"]) |
| 308 | return nil, err | ||
| 309 | end | ||
| 310 | |||
| 311 | return write_entries({ [var] = val }, scope, flags["unset"]) | ||
| 312 | else | 309 | else |
| 313 | return print_entry(var, cfg, flags["json"]) | 310 | return print_entry(args.key, cfg, args["json"]) |
| 314 | end | 311 | end |
| 315 | end | 312 | end |
| 316 | 313 | ||
| 317 | local cleancfg = cleanup(cfg) | 314 | local cleancfg = cleanup(cfg) |
| 318 | 315 | ||
| 319 | if flags["json"] then | 316 | if args["json"] then |
| 320 | return print_json(cleancfg) | 317 | return print_json(cleancfg) |
| 321 | else | 318 | else |
| 322 | print(persist.save_from_table_to_string(cleancfg)) | 319 | print(persist.save_from_table_to_string(cleancfg)) |
