Class: ItchRewards::CLI::Commands::Setup

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
AuthOptions, Helper
Defined in:
lib/itch_rewards/cli.rb

Instance Method Summary collapse

Methods included from AuthOptions

included

Methods included from Helper

#authenticated_client, #authenticated_client!, #cli, #color, #objects_to_table, #render_table, #show_rewards

Instance Method Details

#call(**options) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/itch_rewards/cli.rb', line 117

def call(**options)
  options[:cookies] ||= cli.ask("Where would you like to store your login cookies?  ", default: ".itch-cookies.yml")
  options[:interactive] = true

  if authenticated_client(options)
    cli.say "Saved cookies to #{options[:cookie_path]}"
  else
    cli.say "Login failed, cookies not saved"
  end

  config_path = "itch-reward-config.yml"

  if !File.exist? config_path
    result = cli.yes?("Config file #{config_path} does not exist, would you like to create it?")

    if result
      write_config(config_path, options)
      cli.say "Config file written to #{config_path}"
    end
  else
    cli.warn "Config file #{config_path} already exists, skipping..."
  end
end

#write_config(path, options) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/itch_rewards/cli.rb', line 106

def write_config(path, options)
  require 'erb'
  client = authenticated_client!(options)
  
  games = client.game_map.map.values
  template = File.read(File.join(__dir__, 'templates/reward_config.yml.erb'))

  File.write(path, ERB.new(template, trim_mode: '-').result(binding))
end