Class: Rgc::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/rgc/init.rb

Instance Method Summary collapse

Constructor Details

#initialize(global_options, options, args) ⇒ Init

Returns a new instance of Init.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rgc/init.rb', line 3

def initialize(global_options, options, args)
  abort "Key file must be given!" if (@key_file = args.first).nil?

  begin
    @secret = File.read(@key_file)
  rescue
    abort "Cannot read key file #{@key_file}"
  end

  begin
    stdout, stderr, status = Open3.capture3("git status  -uno --porcelain")
  rescue Errno::ENOENT
    abort "Cannot run `git status  -uno --porcelain`."
  end

  unless status.exitstatus == 0
    abort "git status failed - is this a git repository?"
  end

  unless stdout.length == 0
    $stderr.puts "Working directory not clean"
    abort "Please commit your changes or 'git stash' them."
  end

  init_rgc_config_file

  unless File.exist?('.gitattributes')
    begin
      File.open('.gitattributes', 'w') {}
    rescue Errno::EACCES
      abort "Cannot open .gitattributes for writing."
    end
  end

  add_git_config_options
end

Instance Method Details

#add_git_config_optionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rgc/init.rb', line 58

def add_git_config_options
  smudge = "git config filter.smudge \"rgc smudge\""
  stdout, stderr, status_smudge = Open3.capture3(smudge)

  clean = "git config filter.clean \"rgc clean\""
  stdout, stderr, status_clean = Open3.capture3(clean)

  if [0, 0] != [status_smudge, status_clean]
    abort "Cannot configure git."
  end
rescue Errno::ENOENT
  abort "Cannot configure git."
end

#init_rgc_config_fileObject



40
41
42
43
44
45
46
47
48
# File 'lib/rgc/init.rb', line 40

def init_rgc_config_file
  @config = Rgc::Encrypt::CONFIG

  if File.exists?(@config)
    validate_config_file(@config)
  else
    File.open(@config, 'w') { |f| YAML::dump({ rgc_key_file: @key_file }, f) }
  end
end

#validate_config_fileObject



50
51
52
53
54
55
56
# File 'lib/rgc/init.rb', line 50

def validate_config_file
  begin
    raise unless YAML.load_file(@config).is_a?(Hash)
  rescue
    abort("Invalid rgc config file (#{@config}). Delete it or fix it.")
  end
end