Class: Evertils::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/evertils/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_instance) ⇒ Router

Create the router object Params:

config_instance

An instance of Evertils::Cfg



6
7
8
# File 'lib/evertils/router.rb', line 6

def initialize(config_instance)
  @config = config_instance
end

Instance Method Details

#overwrite_controller_with(new_controller) ⇒ Object



80
81
82
# File 'lib/evertils/router.rb', line 80

def overwrite_controller_with(new_controller)
  @request.controller = new_controller
end

#pre_execObject

Prepare for routing



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
# File 'lib/evertils/router.rb', line 11

def pre_exec
  @request = Request.new

  begin
    update_config if uses_local_configuration?

    require "evertils/controllers/#{@request.controller}" unless uses_local_configuration?

    # perform all required checks
    must_pass = Helper::Results.new
    @config.get(:required).each do |key, _|
      must_pass.add(send("verify_#{key}"))
    end

    raise RequiredCheckException unless must_pass.should_eval_to(true)
  rescue YubikeyException
    Notify.error('Check failed: Yubikey is not inserted')
  rescue GpgException
    Notify.error('Check failed: GPG key not found or not imported into your keychain')
  rescue RequiredCheckException
    Notify.error('One or more required checks failed')
  rescue LoadError => e
    Notify.error(e || "Controller not found: #{@request.controller}")
  end
end

#routeObject

Perform command routing



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/evertils/router.rb', line 38

def route
  pre_exec

  # Create object context and pass it the required command line arguments
  begin
    unless @request.controller.nil?
      controller = Evertils::Controller.const_get @request.controller.capitalize

      # create an instance of the requested controller
      context = controller.new(@config, @request)

      if context.can_exec? @request.command, @config
        # Set things up
        context.pre_exec

        # Run the requested action
        context.exec

        # Run cleanup commands
        context.post_exec
      end
    end
  rescue NoMethodError => e
    Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
  rescue RuntimeError => e
    Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
  rescue NameError => e
    Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
  end
end

#update_configObject



74
75
76
77
78
# File 'lib/evertils/router.rb', line 74

def update_config
  additional_config = { path: @config_file_path }.merge(YAML.safe_load(File.read(@config_file_path)))
  @config.merge(additional_config).symbolize!
  overwrite_controller_with :render
end

#uses_local_configuration?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/evertils/router.rb', line 69

def uses_local_configuration?
  @config_file_path = File.expand_path("~/.evertils/templates/type/#{@request.command}.yml")
  File.exist? @config_file_path
end

#verify_gpg_keyObject

checks output of gpg –list-keys for the presence of a specific GPG key

Raises:



85
86
87
88
89
90
91
# File 'lib/evertils/router.rb', line 85

def verify_gpg_key
  # TODO: replace with Open3
  res = system("gpg --list-keys  #{@config.get(:required, :gpg_key)} 2>/dev/null >/dev/null")

  raise GpgException unless res
  res
end

#verify_yubikey_serialObject

checks output of ykman list to determine if the correct key is inserted

Raises:



94
95
96
97
98
99
100
# File 'lib/evertils/router.rb', line 94

def verify_yubikey_serial
  # TODO: replace with Open3
  res = system("ykman list | grep #{@config.get(:required, :yubikey_serial)} 2>/dev/null >/dev/null")

  raise YubikeyException unless res
  res
end