Class: Grdn::CLI

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

Instance Method Summary collapse

Instance Method Details

#ask_passwordObject



31
32
33
# File 'lib/grdn/cli.rb', line 31

def ask_password
  IO::console.getpass 'Password: '
end

#cmd_getObject



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

def cmd_get
  if ARGV.empty?
    STDERR.puts "Path must contain at least one segment"
    exit
  end
  puts @storage.get(ARGV)
end

#cmd_helpObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/grdn/cli.rb', line 35

def cmd_help
  puts <<~HELP
                   Garden: a place for your seeds
                        _
                      _(_)_                          wWWWw   _
          @@@@       (_)@(_)   vVVVv     _     @@@@  (___) _(_)_
         @@()@@ wWWWw  (_)\\    (___)   _(_)_  @@()@@   Y  (_)@(_)
          @@@@  (___)     `|/    Y    (_)@(_)  @@@@   \\|/   (_)\\
           /      Y       \\|    \\|/    /(_)    \\|      |/      |
        \\ |     \\ |/       | / \\ | /  \\|/       |/    \\|      \\|/
        \\\\|//   \\\\|///  \\\\\\|//\\\\\\|/// \\|///  \\\\\\|//  \\\\|//  \\\\\\|// 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

      This tool stores seed phrases in a local directory (~/.grdn),
      and allows you to organize them hierarchically. Seeds are
      AES-128 encrypted with a password (the same one for all seeds),
      and stored in a tree structure. This allows organization by
      purpose, location, application, wallet, or anything else you
      can think of. They can be nested as deeply as you want. This
      tool is not recommended for security-critical usecases.

    =================================================================

    Usage: grdn <command> [args]

    Commands:
      list                              List your seeds
      set a [b c d e f ...]             Add a seed
      get a [b c d e f ...]             Retrieve a seed
      remove a [b c d e f ...]          Remove a seed

    Example:
      The following commands:
        $ grdn set commerce development my.email+test@domain.com
        $ grdn set commerce production my.email@domain.com
        $ grdn set commerce production my.other.email@domain.com
        $ grdn set phone coinbase-wallet username
        $ grdn set laptop electrum default_wallet
        $ grdn set random

      would product this output:
        $ grdn list

        commerce
          development
            my.email+test@domain.com
          production
            my.email@domain.com
            my.other.email@domain.com
        laptop
          electrum
            default_wallet
        phone
          coinbase-wallet
            username
        random
  HELP
end

#cmd_listObject



125
126
127
128
129
# File 'lib/grdn/cli.rb', line 125

def cmd_list
  puts
  print_tree @storage.list
  STDERR.puts
end

#cmd_removeObject



131
132
133
134
135
# File 'lib/grdn/cli.rb', line 131

def cmd_remove
  STDERR.puts "Printing here for safety: #{@storage.get(ARGV)}"
  @storage.remove(ARGV)
  STDERR.puts 'Seed removed!'
end

#cmd_setObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/grdn/cli.rb', line 102

def cmd_set
  if ARGV.empty?
    STDERR.puts "Path must contain at least one segment"
    exit
  end

  print 'Seed (type or paste, then hit enter three times): '
  $/ = "\n\n\n"
  input = STDIN.noecho(&:gets)
  $/ = "\n"
  seed = input.strip.split.join(' ')
  words = seed.split.size
  valid_seed_lengths = [12, 24]
  if !valid_seed_lengths.include? words
    STDERR.puts "Invalid seed. That one is #{words} words, must be " \
                "#{valid_seed_lengths.join('/')} words"
    exit 1
  end
  @storage.set(ARGV, seed)
  STDERR.puts 'Seed saved!'
  STDERR.puts "#{seed}"
end

#executeObject



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

def execute
  if ARGV.size == 0 || ['-h', '--help', 'help'].include?(ARGV[0])
    cmd_help
    exit
  end

  password = ask_password
  @storage = Storage.new(password)

  case ARGV.shift
  when 'get'
    cmd_get
  when 'set'
    cmd_set
  when 'list'
    cmd_list
  when 'remove'
    cmd_remove
  else
    raise Grdn::Error.new('Unknown command')
  end
rescue Grdn::Error => e
  STDERR.puts e.message
  exit 1
end