Sha256: 55f2fd46ddeb095e7dbfd50ab2f45a615592090bf4da73b6c042ac82b289bd74

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require "stringio"

module Nginxtra
  module Actions
    class Convert
      include Nginxtra::Action

      def convert
        @streams_to_close = []
        converter = Nginxtra::ConfigConverter.new output
        converter.convert :config => config, :binary_status => binary_status
        save_if_necessary!
      ensure
        close_streams!
      end

      private
      def output
        if @thor.options["output"]
          STDOUT
        elsif @thor.options["config"]
          @output = @thor.options["config"]
          @stringio = StringIO.new
        else
          @output = "nginxtra.conf.rb"
          @stringio = StringIO.new
        end
      end

      def save_if_necessary!
        return unless @output && @stringio
        @thor.create_file @output, @stringio.string
      end

      def config
        if @thor.options["input"]
          STDIN
        elsif @thor.options["nginx-conf"]
          open_file @thor.options["nginx-conf"]
        end
      end

      def binary_status
        return if @thor.options["ignore-nginx-bin"]

        if @thor.options["nginx-bin"]
          @thor.run "#{@thor.options["nginx-bin"]} -V 2>&1", :capture => true
        else
          # TODO: Figure out the nginx binary location and call -V
          raise "The auto detection of nginx binary is not yet implemented.  Please use the --nginx-bin option for now."
        end
      end

      def open_file(path)
        raise "Missing config file #{path}" unless File.exists? path

        File.open(path, "r").tap do |stream|
          @streams_to_close << stream
        end
      end

      def close_streams!
        @streams_to_close.each &:close
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nginxtra-1.2.1.2 lib/nginxtra/actions/convert.rb