kbsecret 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40118cac4e5abfe1a536fefb044f62266df790b8
4
- data.tar.gz: 52f20d03e5855f7609c41e51787e530fa01e7f38
3
+ metadata.gz: 95555b8ca2d5833158c8575fb15a5de20f03e954
4
+ data.tar.gz: 1bf659569930104b29a28057750260d8ae868b24
5
5
  SHA512:
6
- metadata.gz: 1b920a36a162c11b7b94b925ce495f6246a4645711b987ff5c9629845e881934918fc179cbfdd6d2452d858fc800dedbe220edd4b0723c42271f88c3faeb69b5
7
- data.tar.gz: 3102a1a4481fdd75a8dfebd4e56543a6fe079b95f5e4405800ff6b3532e0d2e4efb6b151d3c325b55f6edd4d4d1421713452337fe932b617f6a6bf42d12caf8e
6
+ metadata.gz: 595437bc9f394bfed347a23c05fdb480a329b40230aceeddeec4592bb2c1523cadace82c4f694b85722e3f6d29d7e46239ed9bef6db143db206617d5fd217723
7
+ data.tar.gz: ca76b4d7e0709eebe3b1260572ac3d4e84b872487532909cce88523d19e4a0f5e9a5e8375222d619caffa70c534ea28841a30ff7e868fecfb30434a47361b9fe
@@ -3,22 +3,28 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Dump all fields for the given record.
9
-
10
- Usage:
11
- kbsecret dump-fields [--session <name>] <label>
12
- EOS
13
-
14
- o.string "-s", "--session", "the session name", default: :default
15
- o.bool "-x", "--terse", "output in field:value format"
16
- o.string "-i", "--ifs", "separate terse pairs with this string", default: ":"
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Dump all fields for the given record.
10
+
11
+ Usage:
12
+ kbsecret dump-fields [--session <name>] <label>
13
+ EOS
14
+
15
+ o.string "-s", "--session", "the session name", default: :default
16
+ o.bool "-x", "--terse", "output in field:value format"
17
+ o.string "-i", "--ifs", "separate terse pairs with this string", default: ":"
18
+ end
19
+
20
+ dreck do
21
+ string :label
22
+ end
17
23
  end
18
24
 
19
- session = KBSecret::CLI.ensure_session opts[:session]
25
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
20
26
 
21
- label = opts.args.shift
27
+ label = cmd.args[:label]
22
28
  record = session[label]
23
29
 
24
30
  KBSecret::CLI.die "No such record." unless record
@@ -26,8 +32,8 @@ KBSecret::CLI.die "No such record." unless record
26
32
  field_values = record.class.data_fields.map { |f| record.send f }
27
33
  field_pairs = record.class.data_fields.zip(field_values)
28
34
 
29
- if opts.terse?
30
- puts field_pairs.map { |f, v| "#{f}#{opts[:ifs]}#{v}" }.join "\n"
35
+ if cmd.opts.terse?
36
+ puts field_pairs.map { |f, v| "#{f}#{cmd.opts[:ifs]}#{v}" }.join "\n"
31
37
  else
32
38
  puts field_pairs.map { |f, v| "#{f}: #{v}" }.join "\n"
33
39
  end
@@ -3,37 +3,42 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Retrieve environment records in a source-able format.
9
-
10
- Usage:
11
- kbsecret env [--session <name>] [--all] <label1 label2 ...>
12
-
13
- Examples:
14
- kbsecret env --all
15
- kbsecret env staging beta
16
- EOS
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Retrieve environment records in a source-able format.
10
+
11
+ Usage:
12
+ kbsecret env [--session <name>] [--all] <label1 label2 ...>
13
+
14
+ Examples:
15
+ kbsecret env --all
16
+ kbsecret env staging beta
17
+ EOS
18
+
19
+ o.string "-s", "--session", "the session name", default: :default
20
+ o.bool "-a", "--all", "retrieve all environment records, not just listed ones"
21
+ o.bool "-v", "--value-only", "print only the environment value, not the key"
22
+ end
17
23
 
18
- o.string "-s", "--session", "the session name", default: :default
19
- o.bool "-a", "--all", "retrieve all environment records, not just listed ones"
20
- o.bool "-v", "--value-only", "print only the environment value, not the key"
24
+ dreck do
25
+ list :string, :labels
26
+ end
21
27
  end
22
28
 
23
- session = KBSecret::CLI.ensure_session opts[:session]
24
-
29
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
25
30
  records = session.records :environment
26
31
 
27
- selected_records = if opts.all?
32
+ selected_records = if cmd.opts.all?
28
33
  records
29
34
  else
30
35
  records.select do |record|
31
- opts.args.include? record.label
36
+ cmd.args[:labels].include? record.label
32
37
  end
33
38
  end
34
39
 
35
40
  selected_records.each do |record|
36
- if opts.value_only?
41
+ if cmd.opts.value_only?
37
42
  puts record.value
38
43
  else
39
44
  puts record.to_export
@@ -3,27 +3,29 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- List all secrets known to the specified session (or the default session).
9
-
10
- Usage:
11
- kbsecret list [--session <name>] [--show-all]
12
- EOS
13
-
14
- o.string "-s", "--session", "the session name", default: :default
15
- o.string "-t", "--type", "the type of secrets to list", default: nil
16
- o.bool "-a", "--show-all", "show everything in each secret (i.e. metadata)"
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ List all secrets known to the specified session (or the default session).
10
+
11
+ Usage:
12
+ kbsecret list [--session <name>] [--show-all]
13
+ EOS
14
+
15
+ o.string "-s", "--session", "the session name", default: :default
16
+ o.string "-t", "--type", "the type of secrets to list", default: nil
17
+ o.bool "-a", "--show-all", "show everything in each secret (i.e. metadata)"
18
+ end
17
19
  end
18
20
 
19
- session = KBSecret::CLI.ensure_session opts[:session]
21
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
20
22
 
21
- records = session.records opts[:type]
23
+ records = session.records cmd.opts[:type]
22
24
 
23
25
  records.each do |record|
24
26
  puts record.label
25
27
 
26
- next unless opts.show_all?
28
+ next unless cmd.opts.show_all?
27
29
 
28
30
  puts <<~EOS
29
31
  \tType: #{record.type}
@@ -3,39 +3,45 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Retrieve login records.
9
-
10
- Usage:
11
- kbsecret login [--session <name>] <label1 label2 ...>
12
-
13
- Examples:
14
- kbsecret login --terse --ifs "*" gmail
15
- kbsecret login gmail netflix
16
- EOS
17
-
18
- o.string "-s", "--session", "the session name", default: :default
19
- o.bool "-a", "--all", "retrieve all login records, not just listed ones"
20
- o.bool "-x", "--terse", "output in label:username:password format"
21
- o.string "-i", "--ifs", "separate terse fields with this string", default: ":"
22
- end
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Retrieve login records.
10
+
11
+ Usage:
12
+ kbsecret login [--session <name>] <label1 label2 ...>
23
13
 
24
- session = KBSecret::CLI.ensure_session opts[:session]
14
+ Examples:
15
+ kbsecret login --terse --ifs "*" gmail
16
+ kbsecret login gmail netflix
17
+ EOS
18
+
19
+ o.string "-s", "--session", "the session name", default: :default
20
+ o.bool "-a", "--all", "retrieve all login records, not just listed ones"
21
+ o.bool "-x", "--terse", "output in label:username:password format"
22
+ o.string "-i", "--ifs", "separate terse fields with this string", default: ":"
23
+ end
25
24
 
25
+ dreck do
26
+ list :string, :labels
27
+ end
28
+ end
29
+
30
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
26
31
  records = session.records :login
27
- selected_records = if opts.all?
32
+
33
+ selected_records = if cmd.opts.all?
28
34
  records
29
35
  else
30
36
  records.select do |record|
31
- opts.args.include? record.label
37
+ cmd.args[:labels].include? record.label
32
38
  end
33
39
  end
34
40
 
35
41
  selected_records.each do |record|
36
- if opts.terse?
42
+ if cmd.opts.terse?
37
43
  fields = %i[label username password].map { |m| record.send(m) }
38
- puts fields.join(opts[:ifs])
44
+ puts fields.join(cmd.opts[:ifs])
39
45
  else
40
46
  puts <<~EOS
41
47
  Label: #{record.label}
@@ -8,34 +8,40 @@ require "tty-prompt"
8
8
  # allows for abbreviated types (e.g., `kbsecret new env ...`)
9
9
  TYPE_ALIASES = Abbrev.abbrev(KBSecret::Record.record_types).freeze
10
10
 
11
- opts = KBSecret::CLI.slop do |o|
12
- o.banner = <<~EOS
13
- Create a new secret record.
11
+ cmd = KBSecret::CLI.new do
12
+ slop do |o|
13
+ o.banner = <<~EOS
14
+ Create a new secret record.
14
15
 
15
- Usage:
16
- kbsecret new [options] <type> <label>
17
- kbsecret new [options] --args <type> <label> <fields>
16
+ Usage:
17
+ kbsecret new [options] <type> <label>
18
+ kbsecret new [options] --args <type> <label> <fields>
18
19
 
19
- Examples:
20
- kbsecret new login gmail
21
- kbsecret new environment foo-api
22
- EOS
23
-
24
- o.string "-s", "--session", "the session name", default: :default
25
- o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
26
- o.bool "-a", "--args", "use trailing arguments as fields, even with a tty"
27
- o.bool "-e", "--echo", "echo input to tty (only affects interactive input)"
28
- end
20
+ Examples:
21
+ kbsecret new login gmail
22
+ kbsecret new environment foo-api
23
+ EOS
29
24
 
30
- session = KBSecret::CLI.ensure_session opts[:session]
25
+ o.string "-s", "--session", "the session name", default: :default
26
+ o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
27
+ o.bool "-a", "--args", "use trailing arguments as fields, even with a tty"
28
+ o.bool "-e", "--echo", "echo input to tty (only affects interactive input)"
29
+ end
31
30
 
32
- KBSecret::CLI.die "Not enough arguments." if opts.args.size < 2
31
+ dreck do
32
+ string :type
33
+ string :label
34
+ list :string, :fields
35
+ end
36
+ end
33
37
 
34
- type, label = opts.args.shift 2
38
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
35
39
 
40
+ type = cmd.args[:type]
41
+ label = cmd.args[:label]
36
42
  resolved_type = TYPE_ALIASES[type]
37
43
 
38
- if session.record?(label) && !opts.force?
44
+ if session.record?(label) && !cmd.opts.force?
39
45
  KBSecret::CLI.die "Refusing to overwrite an existing record without --force."
40
46
  end
41
47
 
@@ -46,17 +52,15 @@ unless KBSecret::Record.type?(resolved_type)
46
52
  EOS
47
53
  end
48
54
 
49
- fields = []
50
-
51
- if $stdin.tty? && !opts.args?
52
- prompt = TTY::Prompt.new
53
- klass = KBSecret::Record.class_for(resolved_type)
54
- klass.data_fields.each do |field|
55
- fields << prompt.ask("#{field.capitalize}?", echo: opts.echo?)
56
- end
57
- else
58
- fields = opts.args
59
- end
55
+ fields = if $stdin.tty? && !cmd.opts.args?
56
+ prompt = TTY::Prompt.new
57
+ klass = KBSecret::Record.class_for(resolved_type)
58
+ klass.data_fields.map do |field|
59
+ prompt.ask("#{field.capitalize}?", echo: cmd.opts.echo?)
60
+ end
61
+ else
62
+ cmd.args[:fields]
63
+ end
60
64
 
61
65
  begin
62
66
  session.add_record(resolved_type, label, *fields)
@@ -4,32 +4,34 @@
4
4
  require "keybase"
5
5
  require "kbsecret"
6
6
 
7
- opts = KBSecret::CLI.slop do |o|
8
- o.banner = <<~EOS
9
- Create a new session.
10
-
11
- Usage:
12
- kbsecret new-session --label <label> --users <user1,...> --root <dir>
13
-
14
- Example:
15
- kbsecret new-session -l dev -u me,otherperson -r devsecrets
16
- EOS
17
-
18
- o.string "-l", "--label", "the session label", default: :default
19
- o.array "-u", "--users", "the keybase users", default: [Keybase.current_user]
20
- o.string "-r", "--root", "the secret root directory", default: "kbsecret"
21
- o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
7
+ cmd = KBSecret::CLI.new do
8
+ slop do |o|
9
+ o.banner = <<~EOS
10
+ Create a new session.
11
+
12
+ Usage:
13
+ kbsecret new-session --label <label> --users <user1,...> --root <dir>
14
+
15
+ Example:
16
+ kbsecret new-session -l dev -u me,otherperson -r devsecrets
17
+ EOS
18
+
19
+ o.string "-l", "--label", "the session label", default: :default
20
+ o.array "-u", "--users", "the keybase users", default: [Keybase.current_user]
21
+ o.string "-r", "--root", "the secret root directory", default: "kbsecret"
22
+ o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
23
+ end
22
24
  end
23
25
 
24
- session_label = opts[:label]
26
+ session_label = cmd.opts[:label]
25
27
 
26
- if KBSecret::Config.session?(session_label) && !opts.force?
28
+ if KBSecret::Config.session?(session_label) && !cmd.opts.force?
27
29
  KBSecret::CLI.die "Refusing to overwrite an existing session without --force."
28
30
  end
29
31
 
30
32
  session_hash = {
31
- users: opts[:users],
32
- root: opts[:root],
33
+ users: cmd.opts[:users],
34
+ root: cmd.opts[:root],
33
35
  }
34
36
 
35
37
  KBSecret::Config.configure_session(session_label, session_hash)
@@ -4,30 +4,36 @@
4
4
  require "kbsecret"
5
5
  require "clipboard"
6
6
 
7
- opts = KBSecret::CLI.slop do |o|
8
- o.banner = <<~EOS
9
- Retrieve a login record's password.
10
-
11
- Usage:
12
- kbsecret pass [--session <name>] [--clipboard] <label>
13
-
14
- Examples:
15
- kbsecret pass gmail | xclip
16
- kbsecret pass --clipboard gmail
17
- EOS
18
-
19
- o.string "-s", "--session", "the session name", default: :default
20
- o.bool "-c", "--clipboard", "dump the password in the clipboard"
7
+ cmd = KBSecret::CLI.new do
8
+ slop do |o|
9
+ o.banner = <<~EOS
10
+ Retrieve a login record's password.
11
+
12
+ Usage:
13
+ kbsecret pass [--session <name>] [--clipboard] <label>
14
+
15
+ Examples:
16
+ kbsecret pass gmail | xclip
17
+ kbsecret pass --clipboard gmail
18
+ EOS
19
+
20
+ o.string "-s", "--session", "the session name", default: :default
21
+ o.bool "-c", "--clipboard", "dump the password in the clipboard"
22
+ end
23
+
24
+ dreck do
25
+ string :label
26
+ end
21
27
  end
22
28
 
23
- session = KBSecret::CLI.ensure_session opts[:session]
24
- label = opts.args.shift
29
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
30
+ label = cmd.args[:label]
25
31
  record = session[label]
26
32
 
27
33
  KBSecret::CLI.die "No such record." unless record
28
34
  KBSecret::CLI.die "'#{record}' is not a login record." unless record.type == :login
29
35
 
30
- if opts.clipboard?
36
+ if cmd.opts.clipboard?
31
37
  Clipboard.copy record.password
32
38
  else
33
39
  puts record.password
@@ -3,22 +3,28 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Edit the raw JSON of a record in $EDITOR.
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Edit the raw JSON of a record in $EDITOR.
9
10
 
10
- Usage:
11
- kbsecret raw-edit [--session <name>] <label1...>
11
+ Usage:
12
+ kbsecret raw-edit [--session <name>] <label>
12
13
 
13
- Examples:
14
- kbsecret raw-edit gmail
15
- EOS
14
+ Examples:
15
+ kbsecret raw-edit gmail
16
+ EOS
16
17
 
17
- o.string "-s", "--session", "the session name", default: :default
18
+ o.string "-s", "--session", "the session name", default: :default
19
+ end
20
+
21
+ dreck do
22
+ string :label
23
+ end
18
24
  end
19
25
 
20
- session = KBSecret::CLI.ensure_session opts[:session]
21
- label = opts.args.shift
26
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
27
+ label = cmd.args[:label]
22
28
  record = session[label]
23
29
 
24
30
  KBSecret::CLI.die "No such record." unless record
@@ -6,28 +6,33 @@ require "tty-prompt"
6
6
 
7
7
  $VERBOSE = nil # tty-prompt blasts us with irrelevant warnings on 2.4
8
8
 
9
- opts = KBSecret::CLI.slop do |o|
10
- o.banner = <<~EOS
11
- Delete a record.
12
-
13
- Usage:
14
- kbsecret rm [--session <name>] [--interactive] <label>
15
- EOS
16
-
17
- o.string "-s", "--session", "the session name", default: :default
18
- o.bool "-i", "--interactive", "ask for confirmation before deleting"
9
+ cmd = KBSecret::CLI.new do
10
+ slop do |o|
11
+ o.banner = <<~EOS
12
+ Delete a record.
13
+
14
+ Usage:
15
+ kbsecret rm [--session <name>] [--interactive] <label>
16
+ EOS
17
+
18
+ o.string "-s", "--session", "the session name", default: :default
19
+ o.bool "-i", "--interactive", "ask for confirmation before deleting"
20
+ end
21
+
22
+ dreck do
23
+ string :label
24
+ end
19
25
  end
20
26
 
21
- session = KBSecret::CLI.ensure_session opts[:session]
22
-
23
- label = opts.args.shift
27
+ label = cmd.args[:label]
28
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
24
29
 
25
30
  KBSecret::CLI.die "I need the label of a record to delete." unless label
26
31
  KBSecret::CLI.die "I can't delete a nonexistent record." unless session.record? label
27
32
 
28
33
  tty = TTY::Prompt.new
29
34
 
30
- confirm = if opts.interactive?
35
+ confirm = if cmd.opts.interactive?
31
36
  tty.yes?("Delete '#{label}' from the #{session.label} session?")
32
37
  else true
33
38
  end
@@ -3,26 +3,30 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Deconfigure (and optionally delete) a session.
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Deconfigure (and optionally delete) a session.
9
10
 
10
- Usage:
11
- kbsecret rm-session --delete <label>
11
+ Usage:
12
+ kbsecret rm-session --delete <label>
12
13
 
13
- Example:
14
- kbsecret rm-session old-keys
15
- kbsecret rm-session --delete even-older-keys
16
- EOS
14
+ Example:
15
+ kbsecret rm-session old-keys
16
+ kbsecret rm-session --delete even-older-keys
17
+ EOS
17
18
 
18
- o.bool "-d", "--delete", "unlink the session in addition to deconfiguration"
19
- end
19
+ o.bool "-d", "--delete", "unlink the session in addition to deconfiguration"
20
+ end
20
21
 
21
- label = opts.args.shift
22
+ dreck do
23
+ string :label
24
+ end
25
+ end
22
26
 
23
- KBSecret::CLI.die "I need the label of a session to deconfigure." unless label
27
+ label = cmd.args[:label]
24
28
 
25
29
  session = KBSecret::CLI.ensure_session label
26
- session.unlink! if opts.delete?
30
+ session.unlink! if cmd.opts.delete?
27
31
 
28
32
  KBSecret::Config.deconfigure_session label
@@ -3,24 +3,27 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- List all available sessions.
9
- The sessions listed can be passed to other commands via the -s flag.
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ List all available sessions.
10
+ The sessions listed can be passed to other commands via the -s flag.
10
11
 
11
- Usage:
12
- kbsecret sessions [--show-all]
13
- EOS
12
+ Usage:
13
+ kbsecret sessions [--show-all]
14
+ EOS
14
15
 
15
- o.bool "-a", "--show-all", "show each session in depth (i.e. metadata)"
16
+ o.bool "-a", "--show-all", "show each session in depth (i.e. metadata)"
17
+ end
16
18
  end
17
19
 
18
20
  KBSecret::Config.session_labels.each do |sess_name|
19
21
  session_hash = KBSecret::Config.session(sess_name)
20
- session = KBSecret::Session.new label: sess_name
22
+ session = KBSecret::Session.new label: sess_name
23
+
21
24
  puts sess_name
22
25
 
23
- next unless opts.show_all?
26
+ next unless cmd.opts.show_all?
24
27
 
25
28
  puts <<~EOS
26
29
  \tUsers: #{session_hash[:users].join(", ")}
@@ -3,27 +3,35 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop do |o|
7
- o.banner = <<~EOS
8
- Stash the given file (or stdin) as an unstructured record.
9
-
10
- Usage:
11
- kbsecret stash-file <label> [file]
12
-
13
- Example:
14
- kbsecret stash-file foo ~/foo
15
- echo "using stdin" | kbsecret stash-file from-stdin
16
- EOS
17
-
18
- o.string "-s", "--session", "the session name", default: :default
6
+ cmd = KBSecret::CLI.new do
7
+ slop do |o|
8
+ o.banner = <<~EOS
9
+ Stash the given file (or stdin) as an unstructured record.
10
+
11
+ Usage:
12
+ kbsecret stash-file <label> [file]
13
+
14
+ Example:
15
+ kbsecret stash-file foo ~/foo
16
+ echo "using stdin" | kbsecret stash-file from-stdin
17
+ EOS
18
+
19
+ o.string "-s", "--session", "the session name", default: :default
20
+ end
21
+
22
+ dreck errors: false do
23
+ string :label
24
+ string :filename
25
+ end
19
26
  end
20
27
 
21
- session = KBSecret::CLI.ensure_session opts[:session]
22
- label, filename = opts.args.shift 2
28
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
29
+ label = cmd.args[:label]
30
+ filename = cmd.args[:filename]
23
31
 
24
32
  KBSecret::CLI.die "Missing a record label to create." unless label
25
33
 
26
- contents = if filename.nil? || filename == "-"
34
+ contents = if filename.empty? || filename == "-"
27
35
  STDIN.read
28
36
  elsif File.file?(filename)
29
37
  File.read(filename)
@@ -3,33 +3,38 @@
3
3
 
4
4
  require "kbsecret"
5
5
 
6
- opts = KBSecret::CLI.slop cmds: %w[start suspend complete] do |o|
7
- o.banner = <<~EOS
8
- Manage 'to do' records.
9
-
10
- Usage:
11
- kbsecret todo <start|suspend|complete> <label>
12
-
13
- Examples:
14
- kbsecret todo start unit-tests
15
- kbsecret todo suspend laundry
16
- kbsecret todo complete shopping
17
- EOS
18
-
19
- o.string "-s", "--session", "the session name", default: :default
6
+ cmd = KBSecret::CLI.new do
7
+ slop cmds: %w[start suspend complete] do |o|
8
+ o.banner = <<~EOS
9
+ Manage 'to do' records.
10
+
11
+ Usage:
12
+ kbsecret todo <start|suspend|complete> <label>
13
+
14
+ Examples:
15
+ kbsecret todo start unit-tests
16
+ kbsecret todo suspend laundry
17
+ kbsecret todo complete shopping
18
+ EOS
19
+
20
+ o.string "-s", "--session", "the session name", default: :default
21
+ end
22
+
23
+ dreck do
24
+ string :command
25
+ string :label
26
+ end
20
27
  end
21
28
 
22
- session = KBSecret::CLI.ensure_session opts[:session]
23
- command, label = opts.args.shift 2
24
-
25
- KBSecret::CLI.die "Missing subcommand." unless command && label
29
+ session = KBSecret::CLI.ensure_session cmd.opts[:session]
30
+ label = cmd.args[:label]
26
31
 
27
32
  todo = session[label]
28
33
 
29
34
  KBSecret::CLI.die "No such record." unless todo
30
35
  KBSecret::CLI.die "'#{todo}' is not a todo record." unless todo.type == :todo
31
36
 
32
- case command
37
+ case cmd.args[:command]
33
38
  when "start"
34
39
  KBSecret::CLI.die "That task is already started!" if todo.started?
35
40
  todo.start!
@@ -11,7 +11,7 @@ require_relative "kbsecret/cli"
11
11
  # The primary namespace for kbsecret.
12
12
  module KBSecret
13
13
  # kbsecret's current version
14
- VERSION = "0.4.4"
14
+ VERSION = "0.5.0"
15
15
 
16
16
  # fail very early if the user doesn't have keybase and KBFS running
17
17
  raise Keybase::KeybaseNotRunningError unless Keybase.running?
@@ -1,22 +1,83 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pastel"
3
+ require "colored2"
4
4
  require "slop"
5
+ require "dreck"
5
6
 
6
7
  module KBSecret
7
8
  # An encapsulation of useful methods for kbsecret's CLI.
8
- module CLI
9
- class << self
10
- # The pastel object used to generate colorful output.
11
- # @api private
12
- PASTEL = Pastel.new
9
+ class CLI
10
+ # @return [Slop::Result] the result of option parsing
11
+ attr_reader :opts
12
+
13
+ # @return [Dreck::Result] the result of trailing argument parsing
14
+ attr_reader :args
15
+
16
+ # Encapsulate both the options and trailing arguments passed to a `kbsecret` command.
17
+ # @example
18
+ # cmd = KBSecret::CLI.new do
19
+ # slop do |o|
20
+ # o.bool "-f", "--foo", "whatever"
21
+ # end
22
+ #
23
+ # dreck do
24
+ # string :name
25
+ # end
26
+ # end
27
+ #
28
+ # cmd.opts # => Slop::Result
29
+ # cmd.args # => Dreck::Result
30
+ def initialize(&block)
31
+ @trailing = ARGV
32
+ @opts = nil
33
+ @args = nil
34
+ instance_eval(&block)
35
+ end
36
+
37
+ # Parse options for a kbsecret utility, adding some default options for
38
+ # introspection and help output.
39
+ # @param cmds [Array<String>] additional commands to print in `--introspect-flags`
40
+ # @param errors [Boolean] whether or not to produce Slop errors
41
+ # @return [Slop::Result] the result of argument parsing
42
+ # @note This should be called within the block passed to {initialize}.
43
+ def slop(cmds: [], errors: false)
44
+ @opts = Slop.parse suppress_errors: !errors do |o|
45
+ yield o
46
+
47
+ o.on "-h", "--help" do
48
+ puts o
49
+ exit
50
+ end
51
+
52
+ o.on "--introspect-flags" do
53
+ comp = o.options.flat_map(&:flags) + cmds
54
+ puts comp.join "\n"
55
+ exit
56
+ end
57
+ end
58
+
59
+ @trailing = @opts.args
60
+ end
13
61
 
62
+ # Parse trailing arguments for a kbsecret utility, using the elements remaining
63
+ # after options have been removed and interpreted via {slop}.
64
+ # @param errors [Boolean] whether or not to produce (strict) Dreck errors
65
+ # @note *If* {slop} is called, it must be called before this.
66
+ def dreck(errors: true, &block)
67
+ @args = Dreck.parse @trailing, strict: errors do
68
+ instance_eval(&block)
69
+ end
70
+ rescue => e
71
+ KBSecret::CLI.die "#{e.to_s.capitalize}."
72
+ end
73
+
74
+ class << self
14
75
  # Print an error message and terminate.
15
76
  # @param msg [String] the message to print
16
77
  # @return [void]
17
78
  # @note This method does not return!
18
79
  def die(msg)
19
- pretty = "#{PASTEL.bright_red("Fatal")}: #{msg}"
80
+ pretty = "#{"Fatal".red}: #{msg}"
20
81
  abort pretty
21
82
  end
22
83
 
@@ -35,6 +96,7 @@ module KBSecret
35
96
  # @param cmds [Array<String>] additional commands to print in `--introspect-flags`
36
97
  # @param errors [Boolean] whether or not to produce Slop errors
37
98
  # @return [Slop::Result] the result of argument parsing
99
+ # @deprecated Use {#initialize} instead.
38
100
  def slop(cmds: [], errors: false)
39
101
  Slop.parse suppress_errors: !errors do |o|
40
102
  yield o
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kbsecret
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Woodruff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clipboard
@@ -25,33 +25,47 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: keybase-unofficial
28
+ name: colored2
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.8
33
+ version: '3.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.0.8
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pastel
42
+ name: dreck
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.7'
47
+ version: 0.0.4
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.7'
54
+ version: 0.0.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: keybase-unofficial
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.8
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.8
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: slop
57
71
  requirement: !ruby/object:Gem::Requirement