Class: Gem::Commands::NexusCommand

Inherits:
AbstractCommand show all
Defined in:
lib/commands/nexus.rb

Instance Method Summary collapse

Methods inherited from AbstractCommand

#ask_for_password, #authorization, #config, #configure_url, #http_proxy, #make_request, #prompt_encryption, #proxy_class, #setup, #sign_in, #url, #use_proxy!

Constructor Details

#initializeNexusCommand

Returns a new instance of NexusCommand.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/commands/nexus.rb', line 28

def initialize
  super 'nexus', description
  add_proxy_option

  add_option('--all-repos',
             'list all configured repos with their respective urls.') do |value, options|
    options[:nexus_all_repos] = value
  end

  add_option('--clear-all',
             'clears all credentials') do |value, options|
    options[:nexus_clear_all] = value
  end

  add_option('--secrets FILE',
             'move the credentials to the given secrets file.') do |value, options|
    options[:nexus_secrets] = File.expand_path(value)
  end

  add_option('--no-secrets',
             'move the credentials to the configuration file and delete the secrets file.') do |_value, options|
    options[:nexus_secrets] = false
  end

  add_option('--[no-]prompt',
             'always prompt for the credentials. this is helpful to reset your username/password for a specific host.') do |value, options|
    options[:nexus_prompt_all] = value
  end

  add_option('--[no-]encrypt',
             'encrypt/decrypt the credentials with a master password.') do |value, options|
    options[:nexus_encrypt] = value
  end
end

Instance Method Details

#argumentsObject



10
11
12
# File 'lib/commands/nexus.rb', line 10

def arguments
  'GEM       built gem to upload. some options do not require it.'
end

#descriptionObject



6
7
8
# File 'lib/commands/nexus.rb', line 6

def description
  'Upload a gem up to Nexus server'
end

#executeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/commands/nexus.rb', line 63

def execute
  names = begin
    get_all_gem_names
  rescue StandardError
    nil
  end
  if names && (!options[:nexus_all_repos].nil? ||
    !options[:nexus_clear_all].nil? ||
    !options[:nexus_prompt_all].nil? ||
    !options[:nexus_encrypt].nil? ||
    !options[:nexus_secrets].nil?)
    warn "given gemfile(s) #{names.join(' ')} get ignored due to the options used"
  end

  if options[:nexus_all_repos]
    list_repos
  elsif options[:nexus_prompt_all]
    if ask('setup nexus to always prompt username/passwords and delete all current credentials ? (y/N)') == 'y'
      config.always_prompt
    end
  elsif options[:nexus_prompt_all] == false
    say('setup nexus to store username/passwords')
    config.clear_always_prompt
  elsif options[:nexus_clear_all]
    config.clear_credentials if ask('delete all current credentials ? (y/N)') == 'y'
  elsif options[:nexus_encrypt]
    prompt_encryption
    config.encrypt_credentials
  elsif options[:nexus_encrypt] == false
    prompt_encryption
    config.decrypt_credentials
  elsif options[:nexus_secrets] == false
    config.new_secrets(nil)
  elsif options[:nexus_secrets]
    config.new_secrets(options[:nexus_secrets])
  else
    setup
    # if there is no gemname and no options which then fail with send_gem
    send_gem
  end
end

#send_gemObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/commands/nexus.rb', line 105

def send_gem
  gems = get_all_gem_names

  say "Uploading #{gems.size} gem#{'s' if gems.size != 1} to Nexus..."

  gems.each do |path|
    response = make_request(:put, "gems/#{File.basename(path)}") do |request|
      request.body = Gem.read_binary(path)
      request.add_field('Content-Length', request.body.size)
      request.add_field('Content-Type', 'application/octet-stream')
      request.add_field('Authorization', authorization.strip) if authorization
    end

    say "headers: #{response.each_header.to_h}"
    warn "headers: #{response.each_header.to_h}"
    IO.puts response.each_header.to_h

    case response.code
    when '400'
      say 'something went wrong - maybe (re)deployment is not allowed'
    when '401'
      say 'Unauthorized'
    when '500'
      say 'something went wrong'
    else
      say "#{response.message} #{path.split(%r{/}).last}"
    end

    exit 1 unless response.is_a? Net::HTTPSuccess
  end
end

#usageObject



14
15
16
# File 'lib/commands/nexus.rb', line 14

def usage
  "#{program_name} GEM [GEM [...]]"
end