lib/akamai_ccu/cli.rb in akamai_ccu-1.2.1 vs lib/akamai_ccu/cli.rb in akamai_ccu-1.3.0

- old
+ new

@@ -1,5 +1,6 @@ +require "logger" require "optparse" require "akamai_ccu/wrapper" module AkamaiCCU class CLI @@ -7,22 +8,24 @@ def initialize(args:, action:, io: STDOUT, wrapper_klass: Wrapper, secret_klass: Secret, endpoint_klass: Endpoint) @args = args @action = action @io = io + @logger = Logger.new(io) @wrapper_klass = wrapper_klass @secret_klass = secret_klass @endpoint_klass = endpoint_klass @network = Endpoint::Network::STAGING end def call parser.parse!(@args) - return @io.puts(%q{Specify contents to purge either by cp codes or by urls}) unless @objects - return @io.puts(%q{Specify path to the secret file either by edgerc or by txt}) unless @secret + return @logger.warn("specify contents to purge either by cp codes or by urls") unless @objects + return @logger.warn("specify path to the secret file either by edgerc or by txt") unless @secret + return @logger.warn("specified secret file does not exist") unless File.exist?(@secret) wrapper = @wrapper_klass.new(secret: secret, endpoint: endpoint, headers: Array(@headers)) - @io.puts wrapper.call(@objects) + @logger.info wrapper.call(@objects).to_s end private def secret return @secret_klass.by_txt(@secret) if File.extname(@secret) == ".txt" @secret_klass.by_edgerc(@secret) @@ -35,27 +38,39 @@ private def mode return Endpoint::Mode::CPCODE if @objects.all? { |o| o.is_a?(Integer) } Endpoint::Mode::URL end + private def bulk_objects(file) + return unless File.exist?(file) + File.readlines(file).map(&:strip).reject(&:empty?).map do |entry| + entry = entry.to_i unless entry.start_with?("http") + entry + end + end + private def parser OptionParser.new do |opts| opts.banner = %Q{Usage: #{@action} --edgerc=./.edgerc --production --cp="12345, 98765"} opts.on("-eEDGERC", "--edgerc=EDGERC", "Load secret by .edgerc file") do |secret| - @secret = secret + @secret = File.expand_path(secret) end opts.on("-tTXT", "--txt=TXT", "Load secret by TXT file") do |secret| - @secret = secret + @secret = File.expand_path(secret) end opts.on("-cCP", "--cp=CP", "Specify contents by provider (CP) codes") do |objects| @objects = objects.split(",").map(&:strip).map(&:to_i) end opts.on("-uURL", "--url=URL", "Specify contents by URLs") do |objects| @objects = objects.split(",").map(&:strip) + end + + opts.on("-bBULK", "--bulk=BULK", "Specify bulk contents in a file") do |bulk| + @objects = bulk_objects(bulk) end opts.on("--headers=HEADERS", "Specify HTTP headers to sign") do |headers| @headers = headers.split(",").map(&:strip) end