Skip to content

Instantly share code, notes, and snippets.

@ModProg
Last active August 22, 2021 11:35
Show Gist options
  • Save ModProg/54070e0d2ef4491592616e16977343e1 to your computer and use it in GitHub Desktop.
Save ModProg/54070e0d2ef4491592616e16977343e1 to your computer and use it in GitHub Desktop.
Enable Syncing Config Files
complete -c sconfig -x -a "(ls ~/.config)
#!/bin/env ruby
# frozen_string_literal: true
require 'pathname'
require 'fileutils'
require 'yaml'
config_file = Pathname.new("#{Dir.home}/.config/sconfig/config.yml")
unless config_file.exist?
puts "Please set the target and source directories in `#{config_file}`"
config = { 'from' => '~/.config', 'to' => '' }
config_file.open('w') { |file| file.write(config.to_yaml) }
exit
end
config = YAML.load_file(config_file)
from_dir = config['from'].to_s
to_dir = config['to'].to_s
if from_dir.empty?
puts "Plese set the `from` directory in `#{config_file}`"
exit
end
if to_dir.empty?
puts "Plese set the `to` directory in `#{config_file}`"
exit
end
from_dir = from_dir.gsub(/^~/, Dir.home)
to_dir = to_dir.gsub(/^~/, Dir.home)
from_dir = Pathname.new(from_dir.gsub(/^~/, Dir.home))
to_dir = Pathname.new(to_dir.gsub(/^~/, Dir.home))
if ARGV.length != 1
puts "You should pass the name of the config dir in `#{from_dir}`"
exit
end
folder = ARGV[0]
target_path = to_dir.join(folder)
if target_path.exist?
puts "Config folder at `#{target_path}` already exists"
exit
end
source_path = from_dir.join(folder)
unless source_path.exist?
puts "Config folder at `#{source_path}` does not exist"
exit
end
FileUtils.mv source_path, target_path
FileUtils.symlink target_path, source_path
puts "Starting syncing of `#{folder}` at `#{source_path}`"
@ModProg
Copy link
Author

ModProg commented Aug 22, 2021

To enable Fishcompletion add sconfig.fish to .config/fish/completions/sconfig.fish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment