Class: Ufo::CLI::Central::Update

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/cli/central/update.rb

Instance Method Summary collapse

Methods inherited from Base

#execute, #initialize, #log

Methods included from Utils::Sure

#sure?

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Constructor Details

This class inherits a constructor from Ufo::CLI::Central::Base

Instance Method Details

#central_folderObject



90
91
92
# File 'lib/ufo/cli/central/update.rb', line 90

def central_folder
  ENV['UFO_CENTRAL_FOLDER']
end

#central_repoObject



86
87
88
# File 'lib/ufo/cli/central/update.rb', line 86

def central_repo
  ENV['UFO_CENTRAL_REPO']
end

#check_gitignoreObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ufo/cli/central/update.rb', line 98

def check_gitignore
  ok = false
  if File.exist?('.gitignore')
    lines = IO.readlines('.gitignore')
    ok = lines.detect do |line|
      line =~ %r{/?.ufo/?$}
    end
  end
  return if ok
  log "No .ufo found in your .gitignore file".color(:yellow)
  log <<~EOL
    It's recommended to add .ufo to the .gitignore
    When using ufo in a central fashion
  EOL
end

#orgObject

Assume github.com:org/repo. May not work for private “ssh://host:repo” style repos See: terraspace.cloud/docs/terrafile/sources/ssh/ Will consider PRs.

org is used for path to ~/.ufo/central/org/repo



76
77
78
79
80
# File 'lib/ufo/cli/central/update.rb', line 76

def org
  base = central_repo.split('/')[-2] # 1. [email protected]:org/repo 2. repo (for case of https://github.com/org/repo)
  base.gsub!(/.*:/,'') # [email protected]:org/repo => org/repo
  base
end

#pullObject



14
15
16
17
18
19
20
21
22
# File 'lib/ufo/cli/central/update.rb', line 14

def pull
  Dir.chdir(tmp_area) do
    if File.exist?(repo)
      execute "cd #{repo} && git pull"
    else
      execute "git clone #{central_repo}"
    end
  end
end

#repoObject



82
83
84
# File 'lib/ufo/cli/central/update.rb', line 82

def repo
  File.basename(central_repo)
end


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ufo/cli/central/update.rb', line 43

def report_broken_symlink
  return unless File.symlink?('.ufo')

  message =<<~EOL.color(:red)
    ERROR: The .ufo symlink appears to pointing to a missing folder.
    Please double check that the folder exist in the repo/
  EOL
  begin
    target = File.readlink('.ufo')
    unless File.exist?(target)
      logger.error message
      logger.error "Symlink: .ufo -> #{target}"
      exit 1
    end
  rescue Errno::EEXIST
    logger.error message
    exit 1
  end
end

#runObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/ufo/cli/central/update.rb', line 3

def run
  validate!
  action = File.exist?(".ufo") ? "update" : "create"
  sure?("Will #{action} the .ufo symlink") # IE: Will create the .ufo symlink
  log "Updating .ufo with #{central_repo}"
  FileUtils.mkdir_p(tmp_area)
  pull
  symlink
  check_gitignore
end

Always update the symlink in case use changes UFO_CENTRAL_REPO



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ufo/cli/central/update.rb', line 25

def symlink
  src = "#{tmp_area}/#{repo}"
  src += "/#{central_folder}" if central_folder

  FileUtils.mv(".ufo", ".ufo.bak") if File.exist?(".ufo") && File.directory?(".ufo")

  # FileUtils.ln_s(target, link, options)
  # ~/.ufo/central/repo -> .ufo
  src.sub!(/\.git$/,'')
  FileUtils.ln_sf(src, ".ufo", verbose: false) # force in case of existing broken symlink
  FileUtils.rm_rf(".ufo.bak")

  report_broken_symlink

  log "The .ufo symlink has been updated"
  log "Symlink: .ufo -> #{pretty_home(src)}"
end

#tmp_areaObject



94
95
96
# File 'lib/ufo/cli/central/update.rb', line 94

def tmp_area
  "#{ENV['HOME']}/.ufo/central/#{org}"
end

#validate!Object



63
64
65
66
67
68
# File 'lib/ufo/cli/central/update.rb', line 63

def validate!
  return if central_repo
  log "ERROR: Please set the env var: UFO_CENTRAL_REPO".color(:red)
  log "The ufo central update command requires it."
  exit 1
end