Sha256: 3d4995745319dac301527dba3b02671be35dc3a2a9d91ddfa835ad2bdd78cab4

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require "colorize"
require "fileutils"
require "json"

class QiitaConfig
  def initialize(option, input)
    @option = option
    @input = input
    @setup = "#{ENV["HOME"]}/.qiita.conf"
  end

  # check qiita.conf or copy qiita.conf
  def check_or_copy_config()
    lib = File.expand_path("../../../lib", __FILE__)
    cp_file = File.join(lib, "qiita_org", ".qiita.conf")

    if File.exists?("#{ENV["HOME"]}/.qiita.conf")
      puts @setup.green
      print_config("now", "black")
    else
      FileUtils.cp(cp_file, @setup, verbose: true)
    end
  end

  def set_config()
    print_config("before", "red")
    items = JSON.load(File.read(@setup))
    items["#{@option}"] = @input.join(" ").strip
    conts = JSON.pretty_generate(items)
    File.write(@setup, conts)
    print_config("after", "green")
  end

  def print_config(status, color)
    puts status if status != "now"

    items = JSON.load(File.read(@setup))
    format = JSON.pretty_generate(items)
    File.write(@setup, format)

    conts = File.read(@setup)
    adjust_conts = conts.gsub(/{|}|,/, "{" => "", "}" => "", "," => "")
    puts adjust_conts if color == "black"
    puts adjust_conts.green if color == "green"
    puts adjust_conts.red if color == "red"
  end

  def run()
    if @option == nil
      check_or_copy_config()
    else
      set_config()
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
qiita_org-0.1.4 lib/qiita_org/config.rb
qiita_org-0.1.3 lib/qiita_org/config.rb
qiita_org-0.1.2 lib/qiita_org/config.rb
qiita_org-0.1.1 lib/qiita_org/config.rb
qiita_org-0.1.0 lib/qiita_org/config.rb