Class: Gem::Commands::NexusCommand

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

Overview

Copyright © 2019-present Sonatype, Inc. All rights reserved. “Sonatype” is a trademark of Sonatype, Inc.

Instance Method Summary collapse

Constructor Details

#initializeNexusCommand

Returns a new instance of NexusCommand.



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
62
63
64
# File 'lib/commands/nexus.rb', line 29

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



12
13
14
# File 'lib/commands/nexus.rb', line 12

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

#descriptionObject



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

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

#executeObject



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
104
# File 'lib/commands/nexus.rb', line 66

def execute
  names = get_all_gem_names rescue nil
  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 ]
    if ask( "delete all current credentials ? (y/N)" ) == 'y'
      config.clear_credentials
    end
  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



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
# File 'lib/commands/nexus.rb', line 106

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

    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(/\//).last}"
    end

    if !response.kind_of? Net::HTTPSuccess
      exit 1
    end
  end
end

#usageObject



16
17
18
# File 'lib/commands/nexus.rb', line 16

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