Sha256: b20954a3b7ea3054dda420c1d0b0fcf4a8ce8387d2d83121945895081234a21b

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require "fileutils"
require "open3"
require "shellwords"

module Rtprov
  class Initializer
    include FileUtils::Verbose

    attr_reader :name

    def self.run(name)
      new(name).run
    end

    def initialize(name)
      @name = name.dup.freeze
    end

    def run
      raise "Already exists #{name} directory" if File.exist?(name)

      mkdir name
      Dir.chdir(name) do
        mkdir "routers"
        touch "routers/.keep"

        mkdir "templates"
        touch "templates/.keep"

        puts "Create encryption_key"
        key = Encryption.generate_key
        File.write("encryption_key", key)

        exec "bundle init"
        exec "bundle add rtprov -v #{VERSION}"
        exec "bundle binstubs rtprov"

        puts "Create .gitignore"
        gitignore = <<~EOS
          /encryption_key
        EOS
        File.write(".gitignore", gitignore)

        exec "git init"
        exec "git add ."
      end

      puts <<~EOS

        ============================================================
        !!! Please remember `encryption_key`. Git ignores it. !!!
        ============================================================

        And do below.
        1. cd #{name}
        2. bin/rtprov edit YOUR_ROUTER_NAME
        3. bin/rtprov get YOUR_ROUTER_NAME > tempaltes/config.erb
        4. Extract credentials in templates/config.erb
      EOS
    end

    private
    def exec(cmd, *args)
      puts "#{cmd} #{args.shelljoin}"

      o, e, s = Open3.capture3(cmd, *args)

      unless s.success?
        warn e
        raise "`#{cmd} #{args.shelljoin}` failed"
      end

      o
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rtprov-0.1.3 lib/rtprov/initializer.rb
rtprov-0.1.2 lib/rtprov/initializer.rb
rtprov-0.1.1 lib/rtprov/initializer.rb
rtprov-0.1.0 lib/rtprov/initializer.rb