Class: Keyrack::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/keyrack/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



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
# File 'lib/keyrack/runner.rb', line 3

def initialize(argv)
  opts = { :path => "~/.keyrack" }
  OptionParser.new do |optparse|
    optparse.on("-d", "--directory [PATH]", "Specify configuration path (Default: #{@config_path}") do |path|
      opts[:path] = path
    end
  end.parse(argv)
  @config_path = File.expand_path(opts[:path])
  @ui = UI::Console.new

  database_exists = false
  if Dir.exist?(@config_path)
    config_filename = File.join(@config_path, "config.yml")
    if File.exist?(config_filename)
      database_exists = true
      @options = YAML.load_file(config_filename)
      password = @ui.get_password
    end
  end

  if !database_exists
    FileUtils.mkdir_p(@config_path)
    @options = {}
    @ui.display_first_time_notice

    # Password
    password = @ui.password_setup

    # Store
    @options['store'] = @ui.store_setup

    # Write out config
    File.open(File.expand_path('config.yml', @config_path), 'w') { |f| f.print(@options.to_yaml) }
  end

  # Expand relative paths, using config_path as parent
  if @options['store']['type'] == 'filesystem' &&
        @options['store'].has_key?('path')

    @options['store']['path'] =
      File.expand_path(@options['store']['path'], @config_path)
  end

  store = Store[@options['store']['type']].new(@options['store'].reject { |k, _| k == 'type' })
  @database = Database.new(password, store)
  main_loop
end

Instance Method Details

#main_loopObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/keyrack/runner.rb', line 51

def main_loop
  group_tree = [@database.top_group]
  loop do
    current_group = group_tree.last
    menu_options = {
      :group => current_group,
      :at_top => at_top?(current_group),
      :dirty => @database.dirty?,
      :enable_up => group_tree.length > 2
    }
    choice = @ui.menu(menu_options)

    case choice
    when :new
      result = @ui.get_new_entry
      next if result.nil?

      new_site = Site.new(*result.values_at(:site, :username, :password))
      if site = current_group.sites.find { |s| s == new_site }
        if @ui.confirm_overwrite_entry(site)
          site.password = new_site.password
        end
      else
        current_group.add_site(new_site)
      end
    when :edit
      result = @ui.choose_entry_to_edit(current_group)
      next if result.nil?
      site = result[:site]

      loop do
        which = @ui.edit_entry(site)
        case which
        when :change_username
          new_username = @ui.change_username(site.username)
          if new_username
            site.username = new_username
          end
        when :change_password
          new_password = @ui.get_new_password
          if new_password
            site.password = new_password
          end
        when :delete
          if @ui.confirm_delete_entry(site)
            current_group.remove_site(site)
            break
          end
        when nil
          break
        end
      end
    when :new_group
      group_name = @ui.get_new_group
      group = Group.new(group_name)
      current_group.add_group(group)
      group_tree << group
    when :save
      password = @ui.get_password
      if !@database.save(password)
        @ui.display_invalid_password_notice
      end
    when :quit
      break
    when Hash
      if choice[:group]
        group_tree << choice[:group]
      end
    when :top
      while group_tree.length > 1
        group_tree.pop
      end
    when :up
      group_tree.pop
    end
  end
end