Class: Zt::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/zt/cli.rb

Overview

Base CLI class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thor_args = [], thor_options = {}, thor_config = {}) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
# File 'lib/zt/cli.rb', line 11

def initialize(thor_args = [], thor_options = {}, thor_config = {})
  super(thor_args, thor_options, thor_config)
  @conf = Conf.instance.conf
end

Instance Attribute Details

#confObject

Returns the value of attribute conf.



9
10
11
# File 'lib/zt/cli.rb', line 9

def conf
  @conf
end

Instance Method Details

#auth(token) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/zt/cli.rb', line 78

def auth(token)
  token_regex = Constants::AUTH_TOKEN_REGEX
  if token.length != Constants::AUTH_TOKEN_LENGTH
    abort('Authentication token must be exactly 32 characters')
  elsif !token_regex.match?(token)
    abort('Authentication token contains invalid characters, only ' \
          'alphanumerics are permitted.')
  else
    conf.zt['token'] = token
    Zt::Conf.instance.save! :zt
  end
end

#export(format = :hosts) ⇒ Object



71
72
73
74
# File 'lib/zt/cli.rb', line 71

def export(format = :hosts)
  output = Zt::Exporters::Exporter.new.export_one_format :hosts
  puts output
end

#pullObject



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
# File 'lib/zt/cli.rb', line 23

def pull
  puts '≫ pull started'
  puts '  ≫ pulling'
  rapi = Zt::RemoteAPI::ZeroTierAPI.new
  print '    ≫ networks '
  rnetworks = rapi.networks
  column_width = rnetworks.map { |n| n['config']['name'].length }.max + 1
  print ' ' * (column_width + 9)
  puts ''
  rnodes = {}
  rnetworks.each do |net|
    netid = net['id']
    netname = net['config']['name']
    print "    ≫ nodes for network #{netname}"
    rnodes[netid] = rapi.network_members(netid)
    print ' ' * (column_width - netname.length)
    puts ''
  end
  puts '  ≫ processing'
  importer_results = Zt::Importers::Importer.new(rnetworks, rnodes).import
  lnetworks = {}
  ldomains = {}
  lnodes = {}
  importer_results.each do |importer_result|
    if importer_result.key?(:networks)
      importer_result[:networks].each_key do |netid|
        ldomains[netid] =
          importer_result[:networks][netid][:local][:dns_zone]
        lnetworks[netid] = importer_result[:networks][netid]
      end
    end

    next unless importer_result.key?(:nodes)

    importer_result[:nodes].each_key do |nodeid|
      lnodes[nodeid] = importer_result[:nodes][nodeid]
    end
  end
  puts '  ≫ saving'
  Zt::Conf.instance.conf.domains = ldomains
  Zt::Conf.instance.conf.networks = lnetworks
  Zt::Conf.instance.conf.nodes = lnodes
  Zt::Conf.instance.save!
  puts '≫ pull completed'
end

#versionObject



17
18
19
# File 'lib/zt/cli.rb', line 17

def version
  puts "zt version #{Zt::VERSION}"
end