Class: Ninny::Commands::Setup

Inherits:
Ninny::Command show all
Defined in:
lib/ninny/commands/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ninny::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Setup

Returns a new instance of Setup.



10
11
12
13
14
# File 'lib/ninny/commands/setup.rb', line 10

def initialize(options)
  @options = options
  @private_token = options[:token]
  @config = Ninny.user_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/ninny/commands/setup.rb', line 8

def config
  @config
end

#private_tokenObject (readonly)

Returns the value of attribute private_token.



8
9
10
# File 'lib/ninny/commands/setup.rb', line 8

def private_token
  @private_token
end

Instance Method Details

#config_set_gitlab_private_token(private_token) ⇒ Object



40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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_tokenObject



61
62
63
64
65
66
67
68
69
70
71
# 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_configObject



33
34
35
36
37
38
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# 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