class Ninny::Commands::Setup

Attributes

config[R]
private_token[R]

Public Class Methods

new(options) click to toggle source
# File lib/ninny/commands/setup.rb, line 10
def initialize(options)
  @options = options
  @private_token = options[:token]
  @config = Ninny.user_config
end

Public Instance Methods

config_set_gitlab_private_token(private_token) click to toggle source
# File lib/ninny/commands/setup.rb, line 40
def config_set_gitlab_private_token(private_token)
  # TODO: This only works with thor gem < 1. So, we need to make this work when TTY
  #   releases versions compatible with thor versions >= 1 as well.
  config.set(:gitlab_private_token, value: private_token)
  :success
rescue ArgumentError
  puts '  Unable to set new token via TTY... continuing anyway...'
  :failed
end
execute(output: $stdout) click to toggle source
# File lib/ninny/commands/setup.rb, line 16
def execute(output: $stdout)
  try_reading_user_config

  unless @private_token
    @private_token = prompt_for_gitlab_private_token

    unless @private_token
      output.puts "Please create a private token on GitLab and then rerun 'ninny setup'."
      return
    end
  end

  set_response = config_set_gitlab_private_token(@private_token)
  write_gitlab_private_token(@private_token, set_response)
  output.puts "User config #{@result}!"
end
prompt_for_gitlab_private_token() click to toggle source
# File lib/ninny/commands/setup.rb, line 61
def prompt_for_gitlab_private_token
  begin
    new_token_text = config.gitlab_private_token ? ' new' : ''
  rescue MissingUserConfig
    new_token_text = ''
  end

  return unless prompt.yes?("Do you have a#{new_token_text} GitLab private token?")

  prompt.ask('Enter private token:', required: true)
end
try_reading_user_config() click to toggle source
# File lib/ninny/commands/setup.rb, line 33
def try_reading_user_config
  config.read
  @result = 'updated'
rescue MissingUserConfig
  @result = 'created'
end
write_gitlab_private_token(private_token, set_response) click to toggle source
# File lib/ninny/commands/setup.rb, line 50
def write_gitlab_private_token(private_token, set_response)
  raise StandardError unless set_response == :success

  # TODO: This only works with thor gem < 1. So, we need to make this work when TTY
  #   releases versions compatible with thor versions >= 1 as well.
  config.write(force: true)
rescue StandardError
  puts '  Unable to write config file via TTY... continuing anyway...'
  File.open("#{ENV['HOME']}/.ninny.yml", 'w') { |file| file.puts "gitlab_private_token: #{private_token}" }
end