mac_setup 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32bfd8387f06a6e39a2fe649c573ada644381af0
4
- data.tar.gz: d81c0672a8f3f4ce45b63c3b031e089d5a8ad38c
3
+ metadata.gz: f57f9af7d99a2694983bf5d0b09a700eae37b249
4
+ data.tar.gz: 06f8d752ef6a7103c1b829d98a5d0e140dd44739
5
5
  SHA512:
6
- metadata.gz: c48aca54cefb1d8c2284ecf2e39ae38d30a58784756bed95405077dc38aecd4e6eaf49b514f6e717a50c179cf2919fa620110926d1c83d173fa8fddfaf1e765f
7
- data.tar.gz: 8c40807b907e4c2d240edc298bae37187b3e6f9e64724cba6afdf8d2b831309a5fd7acc4022542c0fcf970d59ab7b0d82fcdd92cd450d0d67421c840de9e2049
6
+ metadata.gz: d5ecd9dd676671ea407cd222aca03577a1fa11c670a62edd600619c77dd171020531d3632b4d1a4465ad6f989a53fc0ab9c67b66e3b50435cee9ed986813a204
7
+ data.tar.gz: 4110cb066e342f2b4f048b8d6609143cec308eb59789bc04231dc1602cc55a0466540280f87ae315f5d869b0d49652a3bb49ae8c6b3773fda9ad09d8562d14f5
data/exe/mac_setup CHANGED
@@ -22,4 +22,5 @@ when "encrypt"
22
22
  MacSetup.encrypt
23
23
  else
24
24
  puts "Unknown command: #{command}"
25
+ puts "Version: #{MacSetup::VERSION}"
25
26
  end
@@ -2,6 +2,8 @@ require_relative "shell"
2
2
 
3
3
  module MacSetup
4
4
  class GitRepoInstaller
5
+ attr_reader :repo, :install_path, :tracking_key, :status
6
+
5
7
  def self.run(config, _status)
6
8
  repos = config.git_repos
7
9
  return if repos.none?
@@ -10,39 +12,76 @@ module MacSetup
10
12
 
11
13
  repos.each do |repo_and_path|
12
14
  repo, install_path = repo_and_path.to_a.flatten
13
- install_repo(repo, File.expand_path(install_path))
15
+ new(repo, File.expand_path(install_path)).install_or_update
14
16
  end
15
17
  end
16
18
 
17
- def self.install_repo(repo, install_path)
19
+ def self.install_repo(repo, install_path, tracking_key: nil, status: nil)
20
+ new(repo, install_path, tracking_key: tracking_key, status: status).install_or_update
21
+ end
22
+
23
+ def initialize(repo, install_path, tracking_key: nil, status: nil)
24
+ @repo = repo
25
+ @install_path = install_path
26
+ @tracking_key = tracking_key
27
+ @status = status
28
+ end
29
+
30
+ def install_or_update
18
31
  if Dir.exist?(install_path)
19
- MacSetup.log "#{repo} Already Installed. Updating" do
20
- update_repo(install_path)
21
- end
32
+ MacSetup.log("#{repo} Already Installed. Updating") { update }
22
33
  else
23
- MacSetup.log "Installing #{repo}" do
24
- url = expand_url(repo)
25
- Shell.run(%(git clone --recursive #{url} "#{install_path}"))
26
- end
34
+ MacSetup.log("Installing #{repo}") { install }
27
35
  end
28
36
  end
29
37
 
30
- def self.update_repo(install_path)
31
- Dir.chdir(install_path) do
32
- if can_update?
33
- Shell.run("git pull && git submodule update --init --recursive")
34
- else
35
- puts "\nCan't update. Unstaged changes in #{install_path}"
36
- exit 1
37
- end
38
+ private
39
+
40
+ def install
41
+ clone_repo
42
+ track_install
43
+ end
44
+
45
+ def update
46
+ unless can_update?
47
+ puts "\nCan't update. Unstaged changes in #{install_path}"
48
+ return
49
+ end
50
+
51
+ in_install_path do
52
+ Shell.run("git fetch")
53
+ track_update
54
+ Shell.run("git merge origin && git submodule update --init --recursive")
38
55
  end
39
56
  end
40
57
 
41
- def self.can_update?
58
+ def clone_repo
59
+ Shell.run(%(git clone --recursive #{repo_url} "#{install_path}"))
60
+ end
61
+
62
+ def can_update?
42
63
  Shell.run("git status --porcelain").empty?
43
64
  end
44
65
 
45
- def self.expand_url(repo)
66
+ def track_install
67
+ return unless tracking_key
68
+
69
+ in_install_path do
70
+ status.git_changes(tracking_key, Shell.run("git ls-files").split("\n"))
71
+ end
72
+ end
73
+
74
+ def track_update
75
+ return unless tracking_key
76
+
77
+ status.git_changes(tracking_key, Shell.run("git diff --name-only origin").split("\n"))
78
+ end
79
+
80
+ def in_install_path
81
+ Dir.chdir(install_path) { yield }
82
+ end
83
+
84
+ def repo_url
46
85
  repo =~ %r{^[^/]+/[^/]+$} ? "https://github.com/#{repo}.git" : repo
47
86
  end
48
87
  end
@@ -1,17 +1,18 @@
1
1
  module MacSetup
2
2
  class Secrets
3
+ CRYPTO_LIB = "openssl"
3
4
  CIPHER = "aes-256-cbc"
4
5
  PLAINTEXT_EXT = "priv"
5
6
  CIPHERTEXT_EXT = "crypt"
6
7
 
7
- attr_reader :dir
8
+ attr_reader :files, :password
8
9
 
9
- def self.encrypt(dir)
10
- new(dir).encrypt
10
+ def self.encrypt(dir_or_files)
11
+ new(filter_files(dir_or_files, PLAINTEXT_EXT)).encrypt
11
12
  end
12
13
 
13
- def self.decrypt(dir)
14
- new(dir).decrypt
14
+ def self.decrypt(dir_or_files)
15
+ new(filter_files(dir_or_files, CIPHERTEXT_EXT)).decrypt
15
16
  end
16
17
 
17
18
  def self.encrypted?(file)
@@ -24,50 +25,117 @@ module MacSetup
24
25
  file.sub(/\.#{PLAINTEXT_EXT}$/, "")
25
26
  end
26
27
 
27
- def initialize(dir)
28
- @dir = File.expand_path(dir)
28
+ def self.filter_files(dir_or_files, extension)
29
+ if dir_or_files.is_a?(Array)
30
+ dir_or_files.select { |file| file.to_s.end_with?(extension) }
31
+ else
32
+ Dir.glob("#{File.expand_path(dir_or_files)}/**/*.#{extension}")
33
+ end
34
+ end
35
+
36
+ def initialize(files)
37
+ @files = files
29
38
  end
30
39
 
31
40
  def encrypt
32
- puts "Encrypting files:"
33
- files = Dir.glob("#{dir}/**/*.#{PLAINTEXT_EXT}")
34
- do_crypt(files, from: PLAINTEXT_EXT, to: CIPHERTEXT_EXT, overwrite: true)
41
+ do_crypt("encrypt") { encrypt_files }
35
42
  end
36
43
 
37
44
  def decrypt
38
- puts "Decrypting files:"
39
- files = Dir.glob("#{dir}/**/*.#{CIPHERTEXT_EXT}")
40
-
41
- do_crypt(files, from: CIPHERTEXT_EXT, to: PLAINTEXT_EXT, args: "-d") do |command|
42
- unless Shell.success?(command + %W(-in #{files.first}))
43
- puts "Wrong password!"
44
- exit 1
45
- end
45
+ if files.any?
46
+ MacSetup.log "Decrypting files:"
47
+ else
48
+ MacSetup.log "No files to decrypt. Skipping"
46
49
  end
50
+
51
+ list_files
52
+ get_password
53
+ decrypt_files
47
54
  end
48
55
 
49
56
  private
50
57
 
51
- def do_crypt(files, options)
52
- old_ext = options.fetch(:from)
53
- new_ext = options.fetch(:to)
54
- overwrite = options.fetch(:overwrite, false)
55
- args = Array(options.fetch(:args, []))
58
+ def do_crypt(type)
59
+ if files.any?
60
+ MacSetup.log "#{titleized(type)}ing files:"
61
+ list_files
62
+ get_password
63
+ yield
64
+ else
65
+ MacSetup.log "No files to #{type}. Skipping"
66
+ end
67
+ end
56
68
 
57
- files.each { |file| puts " - #{file}" }
58
- command = base_command(Shell.password) + args
69
+ def titleized(str)
70
+ char, rest = str.split("", 2)
71
+ char.upcase + rest
72
+ end
59
73
 
60
- yield command if block_given?
74
+ def list_files
75
+ files.each { |file| puts " #{MacSetup.shorten_path(file)}" }
76
+ end
61
77
 
78
+ def get_password
79
+ @password = Shell.password
80
+ end
81
+
82
+ def encrypt_files
62
83
  files.each do |file|
63
- target_path = file.sub(/#{old_ext}$/, new_ext)
64
- next if !overwrite && File.exist?(target_path)
65
- Shell.run(command + %W(-in #{file} -out #{target_path}))
84
+ target_path = file.sub(/#{PLAINTEXT_EXT}$/, CIPHERTEXT_EXT)
85
+ do_encrypt(file, target_path)
86
+ end
87
+ end
88
+
89
+ def decrypt_files
90
+ if password_correct?
91
+ do_decrypt
92
+ else
93
+ puts "Wrong Password!"
94
+ get_password
95
+ decrypt_files
96
+ end
97
+ end
98
+
99
+ def password_correct?
100
+ Shell.success?(decrypt_command(files.first, files.first.sub(/#{CIPHERTEXT_EXT}$/, PLAINTEXT_EXT)))
101
+ end
102
+
103
+ def do_decrypt
104
+ files.each { |file| decrypt_file(file) }
105
+ end
106
+
107
+ def decrypt_file(file, log: true)
108
+ target_path = file.sub(/#{CIPHERTEXT_EXT}$/, PLAINTEXT_EXT)
109
+ command = -> { Shell.run(decrypt_command(file, target_path)) }
110
+
111
+ if log
112
+ MacSetup.log "Decrypting #{raw_file_path(file)}", &command
113
+ else
114
+ command.call
66
115
  end
67
116
  end
68
117
 
118
+ def do_encrypt(file, target_path)
119
+ MacSetup.log "Encrypting #{raw_file_path(file)}" do
120
+ Shell.run(encrypt_command(file, target_path))
121
+ end
122
+ end
123
+
124
+ def encrypt_command(file, target_path)
125
+ %W(openssl enc -aes-256-cbc -k testme -in #{file} -out #{target_path})
126
+ end
127
+
128
+ def decrypt_command(file, target_path)
129
+ %W(openssl enc -aes-256-cbc -k testme -d -in #{file} -out #{target_path})
130
+ end
131
+
69
132
  def base_command(password)
70
- %W(openssl enc -#{CIPHER} -k #{password})
133
+ %W(#{CRYPTO_LIB} enc -#{CIPHER} -k #{password})
134
+ end
135
+
136
+ def raw_file_path(file)
137
+ raw_file = file.sub(/\.#{CIPHERTEXT_EXT}|#{PLAINTEXT_EXT}$/, "")
138
+ MacSetup.shorten_path(raw_file)
71
139
  end
72
140
  end
73
141
  end
@@ -9,13 +9,12 @@ module MacSetup
9
9
  end
10
10
 
11
11
  def run
12
- install_openssl
13
- Secrets.decrypt(DOTFILES_PATH)
12
+ install_crypto
13
+ Secrets.decrypt(@status.git_changes(:dotfiles))
14
14
  end
15
15
 
16
- def install_openssl
17
- # Needed for encrypted files
18
- Shell.run("brew install openssl") unless @status.installed_formulas.include?("openssl")
16
+ def install_crypto
17
+ Shell.run("brew install #{SECRETS::CRYPTO_LIB}") unless @status.installed_formulas.include?(CRYPTO_LIB)
19
18
  end
20
19
  end
21
20
  end
@@ -2,6 +2,10 @@ require_relative "shell"
2
2
 
3
3
  module MacSetup
4
4
  class SystemStatus
5
+ def initialize
6
+ @git_changes = Hash.new { |hash, key| hash[key] = [] }
7
+ end
8
+
5
9
  def installed_taps
6
10
  @installed_taps ||= get_taps
7
11
  end
@@ -10,6 +14,14 @@ module MacSetup
10
14
  @installed_formulas ||= get_formulas
11
15
  end
12
16
 
17
+ def git_changes(key, changes = nil)
18
+ if changes
19
+ @git_changes[key] = changes
20
+ else
21
+ @git_changes[key]
22
+ end
23
+ end
24
+
13
25
  private
14
26
 
15
27
  def get_taps
@@ -1,3 +1,3 @@
1
1
  module MacSetup
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
data/lib/mac_setup.rb CHANGED
@@ -27,7 +27,7 @@ module MacSetup
27
27
  config = Configuration.new(File.expand_path(config_path))
28
28
  status = SystemStatus.new
29
29
 
30
- GitRepoInstaller.install_repo(config.dotfiles_repo, DOTFILES_PATH)
30
+ GitRepoInstaller.install_repo(config.dotfiles_repo, DOTFILES_PATH, status: status, track: :dotfiles)
31
31
  config.reload!
32
32
  INSTALLERS.each { |installer| installer.run(config, status) }
33
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac_setup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wean
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler