From 6e4d63fd03a938569512d5ce386810a164b00524 Mon Sep 17 00:00:00 2001 From: Gene Liverman Date: Sat, 3 Jun 2017 21:51:25 -0700 Subject: [PATCH] got linking working --- bin/dots.rb | 26 ++++++++++++++++++-------- bin/dotutils.rb | 32 ++++++++++++++++++++++++++++++++ link/{nix => }/ssh/config | 0 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 bin/dotutils.rb rename link/{nix => }/ssh/config (100%) diff --git a/bin/dots.rb b/bin/dots.rb index bd7d7f0..71a1b45 100755 --- a/bin/dots.rb +++ b/bin/dots.rb @@ -3,15 +3,17 @@ require 'os' require 'tty-command' require 'tty-file' require 'tty-prompt' +require_relative 'dotutils.rb' -cmd = TTY::Command.new -prompt = TTY::Prompt.new(help_color: :magenta) +cmd = TTY::Command.new +@prompt = TTY::Prompt.new(help_color: :magenta) @home = File.expand_path('~') @dotroot = File.dirname(File.dirname(File.expand_path($PROGRAM_NAME))) -@excludes = %w[mac nix] +@excludes = %w[mac nix ssh] @files_copy = Dir.glob("#{@dotroot}/copy/*") @files_link = Dir.glob("#{@dotroot}/link/*") +@ssh_link = Dir.glob("#{@dotroot}/link/ssh/*") if OS.posix? @files_copy.concat Dir.glob("#{@dotroot}/copy/nix/*") @@ -47,10 +49,10 @@ else abort("I'm not sure what to do with this OS...") unless OS.posix? end -task = prompt.select('What would you like to do?', %w[copy link install]) +task = @prompt.select('What would you like to do?', %w[copy link install]) case task when 'copy' - if prompt.yes?('Are you sure you want to copy these files?') + if @prompt.yes?('Are you sure you want to copy these files?') @files_copy.each do |file| unless @excludes.include?(File.basename(file)) puts "Copying #{file} to #{@home}/.#{File.basename(file)}" @@ -61,18 +63,26 @@ when 'copy' end when 'link' - if prompt.yes?('Are you sure you want to link your dot files?') + if @prompt.yes?('Are you sure you want to link your dot files?') @files_link.each do |file| unless @excludes.include?(File.basename(file)) - puts "Linking #{@home}/.#{File.basename(file)} to #{file}" + # puts "Linking #{@home}/.#{File.basename(file)} to #{file}" + link_file(file, "#{@home}/.#{File.basename(file)}") end end + + # rubocop:disable Style/NumericLiteralPrefix + Dir.mkdir("#{@home}/.ssh", 0700) unless File.directory?("#{@home}/.ssh") + # rubocop:enable Style/NumericLiteralPrefix + @ssh_link.each do |file| + link_file(file, "#{@home}/.ssh/#{File.basename(file)}") + end else puts 'no linking' end when 'install' - if prompt.yes?('Are you sure you want to install your base packages?') + if @prompt.yes?('Are you sure you want to install your base packages?') puts 'Here is where Puppet should be run.' cmd.run('bundle exec puppet --version') end diff --git a/bin/dotutils.rb b/bin/dotutils.rb new file mode 100644 index 0000000..fbc0f4b --- /dev/null +++ b/bin/dotutils.rb @@ -0,0 +1,32 @@ +def existing_symlink(source, destination) + return if File.readlink(destination).eql?(source) + # rubocop:disable Metrics/LineLength + if @prompt.yes?("#{destination} currently points to #{File.readlink(destination)}, do you want point it at #{source}?") + File.unlink(destination) + puts "Linking #{destination} to #{source}" + File.symlink(source, destination) + puts 'link replaced' + else + puts "#{destination} is unchanged" + end + # rubocop:enable Metrics/LineLength +end + +def rename_file(source, destination) + puts "#{destination} exists, renaming to #{destination}.predots" + File.rename(destination, "#{destination}.predots") + puts "Linking #{destination} to #{source}" + File.symlink(source, destination) +end + +def link_file(source, destination) + if File.exist?(destination) && File.symlink?(destination) + existing_symlink(source, destination) + elsif File.exist?(destination) + # this catches anything that is not a symlink + rename_file(source, destination) + else + puts "Linking #{destination} to #{source}" + File.symlink(source, destination) + end +end diff --git a/link/nix/ssh/config b/link/ssh/config similarity index 100% rename from link/nix/ssh/config rename to link/ssh/config