Sha256: a0c21e3c063aff9d4dd8fc8cd92fd2f5e6c142c372d27db9b73ca850ad9f3cce

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require_relative 'sapluuna'
begin
  require 'slop'
rescue LoadError
  warn "sudo gem install slop ## required by sapluuna CLI"
  exit 42
end



class Sapluuna
  class CLI
    ROOT = '.'
    attr_reader :debug

    def initialize
      @opts   = opts_parse
      args    = @opts.arguments
      @file   = args.shift
      @labels = @opts[:label].split(/[,\s]+/) if @opts[:label]
      @vars   = {}
      @disco  = @opts[:variables]
      args.each do |var|
        name, value = var.split '='
        @vars[name.to_sym] = value
      end
      if @opts.debug?
        @debug = true
        Log.level = Logger::DEBUG
      end
    end

    def run
      raise MissingOption, 'File is mandatory argument' unless @file
      sap = Sapluuna.new labels: @labels, variables: @vars,
                         discover_variables: @disco,
                         root_directory: (@opts[:root] or ROOT)
      cfg = sap.parse File.read(@file)
      puts @disco ? sap.discovered_variables.keys : cfg
    rescue => error
      crash error
      raise
    end

    private

    def opts_parse
      Slop.parse do |o|
        o.banner = 'Usage: sapluuna [OPTIONS] FILE [variables]'
        o.bool   '-d', '--debug',     'turn on debugging'
        o.string '-l', '--label',     'commma separated list of labels'
        o.bool   '-v', '--variables', 'displays required variables'
        o.string '-r', '--root',      'root directory for template import'
        o.on     '-h', '--help' do puts o; exit; end
      end
    end

    def crash error
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sapluuna-0.2.0 lib/sapluuna/cli.rb
sapluuna-0.1.7 lib/sapluuna/cli.rb
sapluuna-0.1.6 lib/sapluuna/cli.rb
sapluuna-0.1.5 lib/sapluuna/cli.rb
sapluuna-0.1.4 lib/sapluuna/cli.rb