# # Copyright 2013 Mortar Data Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Portions of this code from heroku (https://github.com/heroku/heroku/) Copyright Heroku 2008 - 2013, # used under an MIT license (https://github.com/heroku/heroku/blob/master/LICENSE). # require "mortar/command/base" # manage project config variables # class Mortar::Command::Config < Mortar::Command::Base # config # # Display the config keys (but not values) for a project. # # $ mortar config # A # B def index validate_arguments! project_name = options[:project] || project.name config_body = api.get_config_vars(project_name).body vars = config_body['config'] shadow_vars = config_body['shadow_config'] || {} if vars.empty? display("#{project_name} has no config vars.") else styled_header("#{project_name} Config Vars") keys = vars.keys.sort keys.each {|key| display("#{key}")} unless shadow_vars.empty? styled_header("Project config settings overriden by user config") shadow_vars.each {|key, value| display("#{key}")} end end end # config:set KEY1=VALUE1 [KEY2=VALUE2 ...] # # Set one or more config vars # # -u, --user # Set the config value for your user. Only visible to your user # # and will apply to all projects. Will overwrite a project config # # variable with the same key. # #Example: # # $ mortar config:set A=one # Setting config vars... done. # A: one # # $ mortar config:set A=one B=two # Setting config vars... done. # A: one # B: two # # $ mortar config:set B=three -u # Setting config vars... done. # A: one # B: three # def set unless args.size > 0 and args.all? { |a| a.include?('=') } error("Usage: mortar config:set KEY1=VALUE1 [KEY2=VALUE2 ...]\nMust specify KEY and VALUE to set.") end vars = args.inject({}) do |vars, arg| key, value = arg.split('=', 2) vars[key] = value vars end if options[:user] action("Setting config vars for your user") do api.put_user_config_vars(vars) end else project_name = options[:project] || project.name action("Setting config vars for project #{project_name}") do api.put_config_vars(project_name, vars) end end vars.each {|key, value| vars[key] = value.to_s} styled_hash(vars) end alias_command "config:add", "config:set" alias_command "config:put", "config:set" # config:unset KEY1 [KEY2 ...] # # unset one or more config vars # # -u, --user # Unset the config value for your user. Only affecsts your user # # and will apply to all projects. # # $ mortar config:unset A # Unsetting A... done # # $ mortar config:unset A B # Unsetting A... done # Unsetting B... done # # $ mortar config:unset A B -u # Unsetting A... done # Unsetting B... done # def unset if args.empty? error("Usage: mortar config:unset KEY1 [KEY2 ...]\nMust specify KEY to unset.") end if options[:user] args.each do |key| action("Unsetting #{key} for your user") do api.delete_user_config_var(key) end end else project_name = options[:project] || project.name args.each do |key| action("Unsetting #{key} for project #{project_name}") do api.delete_config_var(project_name, key) end end end end alias_command "config:remove", "config:unset" end