class CommandLine::SubCommands::ConfigCommand

Public Class Methods

new(new_args, new_input_stream = STDIN) click to toggle source
Calls superclass method CommandLine::Command::new
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 16
def initialize(new_args, new_input_stream = STDIN)
  super
end

Public Instance Methods

define_options() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 20
def define_options
end
run(input = "") click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 26
def run(input = "")
  type = next_token
  case type

  when "site"
    run_site
  when "get"
    run_get
  when "set"
    run_set
  else
    usage
  end

end
set_default_options() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 23
def set_default_options
end

Private Instance Methods

get_banner() click to toggle source

Get Banner Text

# File lib/git/contest/command_line/sub_commands/config_command.rb, line 237
def get_banner
  ""\
    "usage: git contest config [type]\n"\
    "\n"\
    "Available types are:\n"\
    "  %-8s: set value\n"\
    "  %-8s: get value\n"\
    "  %-8s: set site info\n"\
    " \n" % ["set", "get", "site"]
end
run_get() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 80
def run_get
  if tokens.length == 1
    # read key
    config = get_config
    cur = config
    keys = tokens.shift.to_s.strip.split('.')
    while ! keys.empty?
      key = keys.shift
      if cur.has_key? key
        cur = cur[key]
      else
        abort "ERROR: Not Found"
      end
    end
    # output
    if cur.is_a? Hash
      puts "keys = #{cur.keys.join(', ')}"
    else
      puts cur
    end
  else
    show_get_usage
  end
end
run_set() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 61
def run_set
  if tokens.length == 1
    # read values
    keys = next_token.split('.')
    puts "input value"
    value = input_stream.gets.strip

    update_config!(get_config, keys, value)
  elsif tokens.length == 2
    # read values from command args
    keys = tokens.shift.to_s.strip.split('.')
    value = tokens.shift.to_s.strip

    update_config!(get_config, keys, value)
  else
    show_set_usage
  end
end
run_site() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 105
def run_site
  if tokens.length >= 1
    type = next_token
    case type
    when "add"
      run_site_add
    when "rm"
      run_site_remove
    else
      show_site_usage
    end
  else
    show_site_usage
  end
end
run_site_add() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 149
def run_site_add
  # git-contest-config site add
  if tokens.length == 1
    puts "# input site config (password will be hidden)"

    # read info
    site_name = next_token
    config = get_config

    # init config
    config["sites"][site_name] = {}

    # input site info
    # TODO: to check not found
    config["sites"][site_name]["driver"] = terminal.ask("%10s > " % "driver").to_s
    # TODO: to depend on above driver
    config["sites"][site_name]["user"] = terminal.ask("%10s > " % "user id").to_s
    config["sites"][site_name]["password"] = terminal.ask("%10s > " % "password") do |q|
      unless /mswin(?!ce)|mingw|cygwin|bccwin/ === RUBY_PLATFORM
        q.echo = "*"
      end
    end.to_s

    # set config
    File.open($git_contest_config, 'w') {|f| f.write config.to_yaml }

    puts ""
    puts "updated successfully!!"
    puts ""
  else
    show_site_add_usage
  end
end
run_site_remove() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 121
def run_site_remove
  # git-contest-config site rm
  if tokens.length == 1
    # TODO: to check not found
    site_name = tokens.shift.to_s.strip

    puts "Are you sure you want to remove `#{site_name}`?"
    this_is_yes = terminal.ask("when you remove the site, type `yes` > ").to_s

    if this_is_yes == "yes"
      # update config
      config = get_config
      config["sites"].delete site_name
      # save config
      File.open($git_contest_config, 'w') {|f| f.write config.to_yaml }
      puts ""
      puts "updated successfully!!"
      puts ""
    else
      puts ""
      puts "operation cancelled"
      puts ""
    end
    else
      show_site_rm_usage
    end
end
show_get_usage() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 183
def show_get_usage
  puts ""\
    "usage: git contest config get [key]\n"\
    "\n"\
    "Example Usage:\n"\
    "  $ git contest config get key1\n"\
    "  $ git contest config get namespace1.key1\n"\
    "  $ git contest config get sites.some_judge.user\n"\
    " \n"
end
show_set_usage() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 194
def show_set_usage
  puts ""\
    "usage: git contest config set [key] <value>\n"\
    "\n"\
    "Example Usage:\n"\
    "  $ git contest config set key1 value1\n"\
    "    -> key1 = value1\n"\
    "  $ git contest config set key1\n"\
    "    -> set value from command-line\n"\
    "  $ git contest config set namespace1.key1 value1\n"\
    "  $ git contest config set sites.some_judge.user username\n"\
    " \n"
end
show_site_add_usage() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 217
def show_site_add_usage
  puts ""\
    "usage: git contest config site add <site-name>\n"\
    "\n"\
    "Example Usage:\n"\
    "  $ git contest config site add site1\n"\
    "  -> input information\n"\
    " \n"
end
show_site_rm_usage() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 227
def show_site_rm_usage
  puts ""\
    "usage: git contest config site rm <site-name>\n"\
    "\n"\
    "Example Usage:\n"\
    "  $ git contest config site rm site1\n"\
    " \n"
end
show_site_usage() click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 208
def show_site_usage
  puts ""\
    "usage: git contest config site <type>\n"\
    "\n"\
    "Available types are:\n"\
    "  %-8s: add site\n"\
    " \n" % ["add"]
end
update_config!(config, keys, value) click to toggle source
# File lib/git/contest/command_line/sub_commands/config_command.rb, line 44
def update_config!(config, keys, value)
  # update yaml value
  new_config = {}
  cur = new_config
  while ! keys.empty?
    key = keys.shift
    if keys.empty?
      cur[key] = value
    else
      cur[key] = {}
      cur = cur[key]
    end
  end
  config.deep_merge! new_config
  File.open($git_contest_config, 'w') {|f| f.write config.to_yaml }
end
usage() click to toggle source

Show Banner

# File lib/git/contest/command_line/sub_commands/config_command.rb, line 249
def usage
  puts get_banner
  return 0
end