Sha256: 20ccbecbe35d3f745d2ceb3ddf4e5eddbe293667b3251a855baad4b9f7afc602

Contents?: true

Size: 1.73 KB

Versions: 10

Compression:

Stored size: 1.73 KB

Contents

module Oxidized
  class Input
    module CLI
      attr_reader :node

      def initialize
        @post_login = []
        @pre_logout = []
        @username, @password, @exec = nil
      end

      def get
        connect_cli
        d = node.model.get
        disconnect
        d
      rescue PromptUndetect
        disconnect
        raise
      end

      def connect_cli
        Oxidized.logger.debug "lib/oxidized/input/cli.rb: Running post_login commands at #{node.name}"
        @post_login.each do |command, block|
          Oxidized.logger.debug "lib/oxidized/input/cli.rb: Running post_login command: #{command.inspect}, block: #{block.inspect} at #{node.name}"
          block ? block.call : (cmd command)
        end
      end

      def disconnect_cli
        Oxidized.logger.debug "lib/oxidized/input/cli.rb Running pre_logout commands at #{node.name}"
        @pre_logout.each { |command, block| block ? block.call : (cmd command, nil) }
      end

      def post_login(cmd = nil, &block)
        return if @exec

        @post_login << [cmd, block]
      end

      def pre_logout(cmd = nil, &block)
        return if @exec

        @pre_logout << [cmd, block]
      end

      def username(regex = /^(Username|login)/)
        @username || (@username = regex)
      end

      def password(regex = /^Password/)
        @password || (@password = regex)
      end

      def login
        match_re = [@node.prompt]
        match_re << @username if @username
        match_re << @password if @password
        until (match = expect(match_re)) == @node.prompt
          cmd(@node.auth[:username], nil) if match == @username
          cmd(@node.auth[:password], nil) if match == @password
          match_re.delete match
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
oxidized-0.30.1 lib/oxidized/input/cli.rb
oxidized-0.30.0 lib/oxidized/input/cli.rb
oxidized-0.29.1 lib/oxidized/input/cli.rb
oxidized-0.29.0 lib/oxidized/input/cli.rb
oxidized-0.28.0 lib/oxidized/input/cli.rb
oxidized-0.27.0 lib/oxidized/input/cli.rb
oxidized-0.26.3 lib/oxidized/input/cli.rb
oxidized-0.26.2 lib/oxidized/input/cli.rb
oxidized-0.26.1 lib/oxidized/input/cli.rb
oxidized-0.26.0 lib/oxidized/input/cli.rb