summaryrefslogtreecommitdiff
path: root/lib/leap_cli/init.rb
blob: bebede7f67dee71a8aac6261aaba67cc5bf17b1f (plain)
1
2
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'fileutils'

module LeapCli
  #
  # creates new provider directory
  #
  def self.init(directory)
    dirs = [directory]
    mkdirs(dirs, false, false)

    Dir.chdir(directory) do
      dirs = ["nodes", "services", "keys", "tags"]
      mkdirs(dirs, false, false)

      #puts "Creating .provider"
      #FileUtils.touch('.provider')

      mkfile("provider.json", PROVIDER_CONTENT)
      mkfile("common.json", COMMON_CONTENT)
    end
  end

  def self.mkfile(filename, content)
    puts "Creating #{filename}"
    File.open(filename, 'w') do |f|
      f.write content
    end
  end

  def self.mkdirs(dirs,force,dry_run)
    exists = false
    if !force
      dirs.each do |dir|
        if File.exist? dir
          raise "#{dir} exists; use --force to override"
          exists = true
        end
      end
    end
    if !exists
      dirs.each do |dir|
        puts "Creating #{dir}/"
        if dry_run
          puts "dry-run; #{dir} not created"
        else
          FileUtils.mkdir_p dir
        end
      end
    else
      puts "Exiting..."
      return false
    end
    true
  end

  PROVIDER_CONTENT = <<EOS
#
# Global provider definition file.
#
{
  "domain": "example.org"
}
EOS

  COMMON_CONTENT = <<EOS
#
# Options put here are inherited by all nodes.
#
{
  "domain": "example.org"
}
EOS

end