Class: LicenseAcceptance::Acceptor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logger
Defined in:
lib/license_acceptance/acceptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

initialize, logger, #logger

Constructor Details

#initialize(opts = {}) ⇒ Acceptor

Returns a new instance of Acceptor.



18
19
20
21
22
23
24
25
26
# File 'lib/license_acceptance/acceptor.rb', line 18

def initialize(opts={})
  @config = Config.new(opts)
  Logger.initialize(config.logger)
  @product_reader = ProductReader.new
  @env_acceptance = EnvAcceptance.new
  @file_acceptance = FileAcceptance.new(config)
  @arg_acceptance = ArgAcceptance.new
  @prompt_acceptance = PromptAcceptance.new(config)
end

Instance Attribute Details

#arg_acceptanceObject (readonly)

Returns the value of attribute arg_acceptance.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def arg_acceptance
  @arg_acceptance
end

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def config
  @config
end

#env_acceptanceObject (readonly)

Returns the value of attribute env_acceptance.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def env_acceptance
  @env_acceptance
end

#file_acceptanceObject (readonly)

Returns the value of attribute file_acceptance.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def file_acceptance
  @file_acceptance
end

#product_readerObject (readonly)

Returns the value of attribute product_reader.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def product_reader
  @product_reader
end

#prompt_acceptanceObject (readonly)

Returns the value of attribute prompt_acceptance.



16
17
18
# File 'lib/license_acceptance/acceptor.rb', line 16

def prompt_acceptance
  @prompt_acceptance
end

Class Method Details

.check_and_persist(product_name, version, opts = {}) ⇒ Object



84
85
86
# File 'lib/license_acceptance/acceptor.rb', line 84

def self.check_and_persist(product_name, version, opts={})
  new(opts).check_and_persist(product_name, version)
end

.check_and_persist!(product_name, version, opts = {}) ⇒ Object



80
81
82
# File 'lib/license_acceptance/acceptor.rb', line 80

def self.check_and_persist!(product_name, version, opts={})
  new(opts).check_and_persist!(product_name, version)
end

Instance Method Details

#accepted?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/license_acceptance/acceptor.rb', line 88

def accepted?
  env_acceptance.accepted?(ENV) || arg_acceptance.accepted?(ARGV)
end

#accepted_no_persist?Boolean

no-persist is silent too

Returns:

  • (Boolean)


93
94
95
# File 'lib/license_acceptance/acceptor.rb', line 93

def accepted_no_persist?
  env_acceptance.no_persist?(ENV) || arg_acceptance.no_persist?(ARGV)
end

#accepted_silent?Boolean

persist but be silent like no-persist

Returns:

  • (Boolean)


98
99
100
# File 'lib/license_acceptance/acceptor.rb', line 98

def accepted_silent?
  env_acceptance.silent?(ENV) || arg_acceptance.silent?(ARGV)
end

#check_and_persist(product_name, version) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/license_acceptance/acceptor.rb', line 40

def check_and_persist(product_name, version)
  if accepted_no_persist?
    logger.debug("Chef License accepted with no persistence")
    return true
  end

  product_reader.read
  product_relationship = product_reader.lookup(product_name, version)

  missing_licenses = file_acceptance.accepted?(product_relationship)

  # They have already accepted all licenses and stored their acceptance in the persistent files
  if missing_licenses.empty?
    logger.debug("All licenses present")
    return true
  end

  if accepted? || accepted_silent?
    if config.persist
      errs = file_acceptance.persist(product_relationship, missing_licenses)
      if errs.empty?
        output_num_persisted(missing_licenses.size) unless accepted_silent?
      else
        output_persist_failed(errs)
      end
    end
    return true
  elsif config.output.isatty && prompt_acceptance.request(missing_licenses) do
      if config.persist
        file_acceptance.persist(product_relationship, missing_licenses)
      else
        []
      end
    end
    return true
  else
    raise LicenseNotAcceptedError.new(missing_licenses)
  end
end

#check_and_persist!(product_name, version) ⇒ Object

For applications that just need simple logic to handle a failed license acceptance flow we include this small wrapper. Apps with more complex logic (like logging to a logging engine) should call the non-bang version and handle the exception.



33
34
35
36
37
38
# File 'lib/license_acceptance/acceptor.rb', line 33

def check_and_persist!(product_name, version)
  check_and_persist(product_name, version)
rescue LicenseNotAcceptedError
  output.puts "#{product_name} cannot execute without accepting the license"
  exit 172
end

#output_num_persisted(count) ⇒ Object

In the case where users accept with a command line argument or environment variable we still want to output the fact that the filesystem was changed.



104
105
106
107
108
109
110
111
# File 'lib/license_acceptance/acceptor.rb', line 104

def output_num_persisted(count)
  s = count > 1 ? "s": ""
  output.puts <<~EOM
  #{PromptAcceptance::BORDER}
  #{PromptAcceptance::CHECK} #{count} product license#{s} accepted.
  #{PromptAcceptance::BORDER}
  EOM
end

#output_persist_failed(errs) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/license_acceptance/acceptor.rb', line 113

def output_persist_failed(errs)
  output.puts <<~EOM
  #{PromptAcceptance::BORDER}
  #{PromptAcceptance::CHECK} Product license accepted.
  Could not persist acceptance:\n\t* #{errs.map(&:message).join("\n\t* ")}
  #{PromptAcceptance::BORDER}
  EOM
end