mirror of
https://github.com/genebean/dots.git
synced 2026-03-27 01:17:42 -04:00
got linking working
This commit is contained in:
parent
77fce59b08
commit
6e4d63fd03
3 changed files with 50 additions and 8 deletions
26
bin/dots.rb
26
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
|
||||
|
|
|
|||
32
bin/dotutils.rb
Normal file
32
bin/dotutils.rb
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue