Sha256: 48465b5da718ff4b407c6c721e8b2bb156c1381fb9c177227dd9de938dd2c9e3

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

#!/usr/bin/env ruby

require 'optparse'

CONF_DIR = File.expand_path('~/.ssh/config.d')
SSH_CONFIG = File.expand_path('~/.ssh/config')
MAGIC_LINE = "# Generated by #{File.basename(__FILE__)}"

options = {:verbose => false, :with => [], :only => []}

optparse = OptionParser.new do|opts|
  opts.on('-h', '--help', 'Display this screen') do
    puts opts
    exit
  end
  opts.on('-v', '--verbose', 'Be verbose') do |file|
    options[:verbose] = true
  end
  opts.on('-w', '--with CONFIG', 'Include an otherwise disabled config file') do |file|
    options[:with] += file.split(',')
    options[:with].each{|file| puts "Including #{file} even if disabled..."}
  end
  opts.on('-o', '--only CONFIG', 'Use only given config files') do |file|
    options[:only] += file.split(',')
    options[:only].each{|file| puts "Using only #{file}..."}
  end
end
optparse.parse!

if !File.directory?(CONF_DIR)
  puts "#{CONF_DIR} does not exist or is not a directory"
  Process.exit!(1)
end

if File.exists?(SSH_CONFIG) && File.new(SSH_CONFIG).gets == "#{MAGIC_LINE}\n"
  puts "Found generated ssh_config under #{SSH_CONFIG}. Overwriting..."
elsif File.exists?(SSH_CONFIG)
  puts "Found hand-crafted ssh_config under #{SSH_CONFIG}. Please move it out of the way."
  Process.exit!(2)
end

File.open(SSH_CONFIG, 'w') do |ssh_config|
  ssh_config.puts(MAGIC_LINE)
  ssh_config.puts("# DO NOT EDIT THIS FILE")
  ssh_config.puts("# Create or modify files under #{CONF_DIR}/ instead")
end

if options[:only].empty?
  files = Dir["#{CONF_DIR}/**"].reject do |file|
    file =~ /\.disabled$/ && !options[:with].include?("#{File.basename(file, '.disabled')}")
  end
else
  files = Dir["#{CONF_DIR}/**"].select do |file|
    options[:only].include?(File.basename(file, '.disabled'))
  end
end

files.sort.each do |file|
  puts "Concatenating #{file}" if options[:verbose]
  %x(/bin/cat #{file} >> #{SSH_CONFIG})
  %x(echo >> #{SSH_CONFIG})
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
poet-0.0.1 bin/poet