Class: KubsCLI::Pull

Inherits:
Object
  • Object
show all
Defined in:
lib/kubs_cli/pull.rb

Overview

Used to pull items into your config-files repo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = KubsCLI.configuration) ⇒ Pull

Returns a new instance of Pull.



10
11
12
13
# File 'lib/kubs_cli/pull.rb', line 10

def initialize(config = KubsCLI.configuration)
  @config = config
  @fh = FileHelper.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/kubs_cli/pull.rb', line 8

def config
  @config
end

Instance Method Details

#copy_files(orig_file, new_file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kubs_cli/pull.rb', line 32

def copy_files(orig_file, new_file)
  if File.directory?(new_file) || File.directory?(new_file)
    Dir.each_child(orig_file) do |o_file|
      Dir.each_child(new_file) do |n_file|
        next unless o_file == n_file

        o_file = File.join(File.expand_path(orig_file), o_file)
        n_file = File.expand_path(new_file)

        FileUtils.cp_r(o_file, n_file)
      end

    end
  else
    FileUtils.cp(orig_file, new_file)
  end
end

#pull_allObject



17
18
19
20
# File 'lib/kubs_cli/pull.rb', line 17

def pull_all
  pull_dotfiles
  pull_gnome_terminal_settings
end

#pull_dotfilesObject

Pulls dotfiles into your dotfiles inside your repo



23
24
25
26
27
28
29
30
# File 'lib/kubs_cli/pull.rb', line 23

def pull_dotfiles
  dotfiles = @config.dotfiles
  local_dir = @config.local_dir

  shared_dotfiles(dotfiles, local_dir) do |remote, local|
    copy_files(local, remote)
  end
end

#pull_gnome_terminal_settingsObject

Pulls gnome_terminal_settings into your dotfiles inside your repo



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kubs_cli/pull.rb', line 64

def pull_gnome_terminal_settings
  unless @config.gnome_terminal_settings
    return
  end
  # This is where dconf stores gnome terminal
  gnome_dconf = '/org/gnome/terminal/'

  orig_remote_contents = File.read(@config.gnome_terminal_settings)

  Rake.sh("dconf dump #{gnome_dconf} > #{@config.gnome_terminal_settings}")
rescue RuntimeError => e
  KubsCLI.add_error(e: e, msg: 'Ran into issues dumping gnome terminal settings')

  # if dconf errors, it will erase the config file contents
  # So this protects against that
  File.write(@config.gnome_terminal_settings, orig_remote_contents)
end

#shared_dotfiles(dotfiles, local_dir) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kubs_cli/pull.rb', line 50

def shared_dotfiles(dotfiles, local_dir)
  Dir.each_child(dotfiles) do |remote_file|
    Dir.each_child(local_dir) do |local_file|
      next unless local_file == ".#{remote_file}"

      remote_file = File.join(dotfiles, remote_file)
      local_file = File.join(local_dir, local_file)
      yield(remote_file, local_file)
    end
  end
end