Sha256: 5a2fa433903d67c2906272c5c2220b250dddd6b0373c9adb21ec783910695af9

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Cloud
  module Sh
    module Helpers
      module Commands

        def command_chain(base)
          CmdChain.new(base)
        end

        class CmdChain
          def initialize(base)
            @cmd = [base]
          end

          def with(val)
            @cmd << val
            self
          end

          def method_missing(name, *args)
            if args.empty?
              @cmd << name.to_s.tr("_", "-")
            elsif args.first.is_a?(TrueClass)
              @cmd << "--#{name.to_s.tr("_", "-")}"
            else
              @cmd << "--#{name.to_s.tr("_", "-")}=#{args.first}"
            end
            self
          end

          def map(*fields)
            execute.lines.map do |line|
              values = line.split.first(fields.size)
              OpenStruct.new(fields.zip(values).to_h)
            end
          end

          def replace_current_process
            exec(@cmd.join(" "))
          end

          def execute
            cloud_sh_exec(@cmd)
          end

          def to_s
            @cmd.join(" ")
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cloud-sh-1.0.6 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.5 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.4 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.3 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.2 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.1 lib/cloud/sh/helpers/commands.rb
cloud-sh-1.0.0 lib/cloud/sh/helpers/commands.rb