kbsecret 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 726c2a148f51589eeef719b170d7f5102532b0f1
4
- data.tar.gz: 2500fec63f6f4a1f843197fc2e5e69be64232723
3
+ metadata.gz: ae1880e11964e5822acd83b77193d94a961780c2
4
+ data.tar.gz: ab15ecaf1b1b2eaddcd6542fd70c701356b56b31
5
5
  SHA512:
6
- metadata.gz: cec6a334486cf1286c2a11e4192b834a477c4472535192a1ecaf136dc54068fd64d3bae22e90340e5463580d2202a5a4fb2a9d5cff103a21165a9bba015673d1
7
- data.tar.gz: 0acfe368d0e5891a58d905917885980449cd38f9c9d87b0c634d16f2b4b019140d63d152c5f22a052aebbe0837b86c19c3f49c5924ec90869460649ef428edc9
6
+ metadata.gz: cd4857ae11dd7b1675fe6610783aec5f44f7a4bebf4c964d6eab993d9c95cf7acef20c8efcb9534120bc69e86c96b992d5679f75fa6514d22eff3c7f2e20a9e3
7
+ data.tar.gz: 40a89055c675a42aac6edd0f591785e0704d6c271b3ced718e27c817b5e45727d628dc31c201b301fa5cbf26335a62b1642d14878fb8b95608eb1e67f9bf2dc0
data/bin/kbsecret CHANGED
@@ -120,7 +120,7 @@ def types_help
120
120
  end
121
121
 
122
122
  def conf(*_args)
123
- abort("You need to set $EDITOR!") unless ENV["EDITOR"]
123
+ KBSecret::CLI.die "You need to set $EDITOR!" unless ENV["EDITOR"]
124
124
  Process.spawn("#{ENV["EDITOR"]} #{KBSecret::Config::CONFIG_FILE}")
125
125
  end
126
126
 
@@ -27,18 +27,12 @@ opts = Slop.parse suppress_errors: true do |o|
27
27
  end
28
28
  end
29
29
 
30
- sess_name = opts[:session]
31
-
32
- unless KBSecret::Config.session? sess_name
33
- abort "Fatal: Unknown session: '#{sess_name}'."
34
- end
35
-
36
- session = KBSecret::Session.new label: sess_name
30
+ session = KBSecret::CLI.ensure_session opts[:session]
37
31
 
38
32
  label = opts.args.shift
39
33
  record = session.records.find { |r| r.label == label }
40
34
 
41
- abort("Fatal: No such record.") unless record
35
+ KBSecret::CLI.die "No such record." unless record
42
36
 
43
37
  field_values = record.class.data_fields.map { |f| record.send f }
44
38
  field_pairs = record.class.data_fields.zip(field_values)
data/bin/kbsecret-env CHANGED
@@ -31,13 +31,7 @@ opts = Slop.parse suppress_errors: true do |o|
31
31
  end
32
32
  end
33
33
 
34
- sess_name = opts[:session]
35
-
36
- unless KBSecret::Config.session? sess_name
37
- abort "Fatal: Unknown session: '#{sess_name}'."
38
- end
39
-
40
- session = KBSecret::Session.new label: sess_name
34
+ session = KBSecret::CLI.ensure_session opts[:session]
41
35
 
42
36
  env_records = session.records :environment
43
37
 
data/bin/kbsecret-list CHANGED
@@ -27,13 +27,7 @@ opts = Slop.parse suppress_errors: true do |o|
27
27
  end
28
28
  end
29
29
 
30
- sess_name = opts[:session]
31
-
32
- unless KBSecret::Config.session? sess_name
33
- abort "Fatal: Unknown session: '#{sess_name}'."
34
- end
35
-
36
- session = KBSecret::Session.new label: sess_name
30
+ session = KBSecret::CLI.ensure_session opts[:session]
37
31
 
38
32
  records = session.records opts[:type]
39
33
 
data/bin/kbsecret-login CHANGED
@@ -32,13 +32,7 @@ opts = Slop.parse suppress_errors: true do |o|
32
32
  end
33
33
  end
34
34
 
35
- sess_name = opts[:session]
36
-
37
- unless KBSecret::Config.session? sess_name
38
- abort "Fatal: Unknown session: '#{sess_name}'."
39
- end
40
-
41
- session = KBSecret::Session.new label: sess_name
35
+ session = KBSecret::CLI.ensure_session opts[:session]
42
36
 
43
37
  login_records = session.records :login
44
38
 
@@ -55,7 +49,7 @@ end
55
49
 
56
50
  selected_records.each do |record|
57
51
  if opts.terse?
58
- fields = [:label, :username, :password].map { |m| record.send(m) }
52
+ fields = %i[label username password].map { |m| record.send(m) }
59
53
  puts fields.join(opts[:ifs])
60
54
  else
61
55
  puts <<~EOS
data/bin/kbsecret-new CHANGED
@@ -39,26 +39,21 @@ opts = Slop.parse suppress_errors: true do |o|
39
39
  end
40
40
  end
41
41
 
42
- sess_name = opts[:session]
42
+ session = KBSecret::CLI.ensure_session opts[:session]
43
43
 
44
- unless KBSecret::Config.session? sess_name
45
- abort "Fatal: Unknown session: '#{sess_name}'."
46
- end
47
-
48
- abort "Fatal: Not enough arguments." if opts.args.size < 2
44
+ KBSecret::CLI.die "Not enough arguments." if opts.args.size < 2
49
45
 
50
- session = KBSecret::Session.new label: sess_name
51
46
  type, label = opts.args.shift 2
52
47
 
53
- type = TYPE_ALIASES[type]
48
+ resolved_type = TYPE_ALIASES[type]
54
49
 
55
50
  if session.record?(label) && !opts.force?
56
- abort "Fatal: Refusing to overwrite an existing record without --force."
51
+ KBSecret::CLI.die "Refusing to overwrite an existing record without --force."
57
52
  end
58
53
 
59
- unless KBSecret::Record.type?(type)
60
- abort <<~EOS
61
- Fatal: Unknown record type: '#{type}'.
54
+ unless KBSecret::Record.type?(resolved_type)
55
+ KBSecret::CLI.die <<~EOS
56
+ Unknown record type: '#{type}'.
62
57
  Known types are: #{KBSecret::Record.record_types.join(", ")}.
63
58
  EOS
64
59
  end
@@ -67,7 +62,7 @@ fields = []
67
62
 
68
63
  if opts.interactive?
69
64
  prompt = TTY::Prompt.new
70
- klass = KBSecret::Record.class_for(type)
65
+ klass = KBSecret::Record.class_for(resolved_type)
71
66
  klass.data_fields.each do |field|
72
67
  fields << prompt.ask("#{field.capitalize}?", echo: opts.echo?)
73
68
  end
@@ -76,7 +71,7 @@ else
76
71
  end
77
72
 
78
73
  begin
79
- session.add_record(type, label, *fields)
74
+ session.add_record(resolved_type, label, *fields)
80
75
  rescue => e
81
- abort "Fatal: #{e}."
76
+ KBSecret::CLI.die "#{e}."
82
77
  end
@@ -35,7 +35,7 @@ end
35
35
  session_label = opts[:label]
36
36
 
37
37
  if KBSecret::Config.session?(session_label) && !opts.force?
38
- abort "Fatal: Refusing to overwrite an existing session without --force."
38
+ KBSecret::CLI.die "Refusing to overwrite an existing session without --force."
39
39
  end
40
40
 
41
41
  session_hash = {
data/bin/kbsecret-pass CHANGED
@@ -31,18 +31,12 @@ opts = Slop.parse suppress_errors: true do |o|
31
31
  end
32
32
  end
33
33
 
34
- sess_name = opts[:session]
35
-
36
- unless KBSecret::Config.session? sess_name
37
- abort "Fatal: Unknown session: '#{sess_name}'."
38
- end
39
-
40
- session = KBSecret::Session.new label: sess_name
41
- label = opts.args.shift
34
+ session = KBSecret::CLI.ensure_session opts[:session]
35
+ label = opts.args.shift
42
36
  login_records = session.records :login
43
- record = login_records.find { |r| r.label == label }
37
+ record = login_records.find { |r| r.label == label }
44
38
 
45
- abort("Fatal: No such login record.") unless record
39
+ KBSecret::CLI.die "No such login record." unless record
46
40
 
47
41
  if opts.clipboard?
48
42
  Clipboard.copy record.password
@@ -28,21 +28,11 @@ opts = Slop.parse suppress_errors: true do |o|
28
28
  end
29
29
  end
30
30
 
31
- sess_name = opts[:session]
31
+ session = KBSecret::CLI.ensure_session opts[:session]
32
+ label = opts.args.shift
33
+ record = session.records.find { |r| r.label == label }
32
34
 
33
- abort "Fatal: $EDITOR is not set." unless ENV["EDITOR"]
35
+ KBSecret::CLI.die "No such record." unless record
34
36
 
35
- unless KBSecret::Config.session? sess_name
36
- abort "Fatal: Unknown session: '#{sess_name}'."
37
- end
38
-
39
- session = KBSecret::Session.new label: sess_name
40
- label = opts.args.shift
41
- record = session.records.find { |r| r.label == label }
42
-
43
- if record
44
- Process.spawn("#{ENV["EDITOR"]} #{record.path}")
45
- record.sync! # just to bump the timestamp
46
- else
47
- abort "Fatal: No such record."
48
- end
37
+ Process.spawn("#{ENV["EDITOR"]} #{record.path}")
38
+ record.sync! # just to bump the timestamp
data/bin/kbsecret-rm CHANGED
@@ -29,17 +29,11 @@ opts = Slop.parse suppress_errors: true do |o|
29
29
  end
30
30
  end
31
31
 
32
- sess_name = opts[:session]
33
-
34
- unless KBSecret::Config.session? sess_name
35
- abort "Fatal: Unknown session: '#{sess_name}'."
36
- end
37
-
38
- session = KBSecret::Session.new label: sess_name
32
+ session = KBSecret::CLI.ensure_session opts[:session]
39
33
 
40
34
  label = opts.args.shift
41
35
 
42
- abort "Fatal: I need the label of a record to delete." unless label
36
+ KBSecret::CLI.die "I need the label of a record to delete." unless label
43
37
 
44
38
  tty = TTY::Prompt.new
45
39
  confirm = true
@@ -31,15 +31,9 @@ end
31
31
 
32
32
  label = opts.args.shift
33
33
 
34
- abort "Fatal: I need the label of a session to deconfigure." unless label
34
+ KBSecret::CLI.die "I need the label of a session to deconfigure." unless label
35
35
 
36
- unless KBSecret::Config.session? label
37
- abort "Fatal: Unknown session: '#{label}'."
38
- end
39
-
40
- if opts.delete?
41
- session = KBSecret::Session.new label: label
42
- session.unlink!
43
- end
36
+ session = KBSecret::CLI.ensure_session label
37
+ session.unlink! if opts.delete?
44
38
 
45
39
  KBSecret::Config.deconfigure_session label
@@ -29,23 +29,17 @@ opts = Slop.parse suppress_errors: true do |o|
29
29
  end
30
30
  end
31
31
 
32
- sess_name = opts[:session]
33
-
34
- unless KBSecret::Config.session? sess_name
35
- abort "Fatal: Unknown session: '#{sess_name}'."
36
- end
37
-
38
- session = KBSecret::Session.new label: sess_name
32
+ session = KBSecret::CLI.ensure_session opts[:session]
39
33
  label, filename = opts.args.shift 2
40
34
 
41
- abort("Fatal: Missing a record label to create.") unless label
35
+ KBSecret::CLI.die "Missing a record label to create." unless label
42
36
 
43
37
  contents = if filename.nil? || filename == "-"
44
38
  STDIN.read
45
39
  elsif File.file?(filename)
46
40
  File.read(filename)
47
41
  else
48
- abort "Fatal: That file doesn't exist."
42
+ KBSecret::CLI.die "That file doesn't exist."
49
43
  end
50
44
 
51
45
  session.add_record(:unstructured, label, contents)
data/bin/kbsecret-todo CHANGED
@@ -32,35 +32,29 @@ opts = Slop.parse suppress_errors: true do |o|
32
32
  end
33
33
  end
34
34
 
35
- sess_name = opts[:session]
36
-
37
- unless KBSecret::Config.session? sess_name
38
- abort "Fatal: Unknown session: '#{sess_name}'."
39
- end
40
-
41
- session = KBSecret::Session.new label: sess_name
35
+ session = KBSecret::CLI.ensure_session opts[:session]
42
36
  command, label = opts.args.shift 2
43
37
 
44
- abort("Fatal: Missing subcommand.") unless command && label
38
+ KBSecret::CLI.die "Missing subcommand." unless command && label
45
39
 
46
40
  todo_records = session.records :todo
47
41
  todo = todo_records.find { |r| r.label == label }
48
42
 
49
- abort("Fatal: No such todo record.") unless todo
43
+ KBSecret::CLI.die("Fatal: No such todo record.") unless todo
50
44
 
51
45
  case command
52
46
  when "start"
53
- abort("That task is already started!") if todo.started?
47
+ KBSecret::CLI.die "That task is already started!" if todo.started?
54
48
  todo.start!
55
49
  puts "#{todo.label}: '#{todo.todo}' marked as started at #{todo.start}"
56
50
  when "suspend"
57
- abort("That task is already suspended!") if todo.suspended?
51
+ KBSecret::CLI.die "That task is already suspended!" if todo.suspended?
58
52
  todo.suspend!
59
53
  puts "#{todo.label}: '#{todo.todo}' marked as suspended at #{todo.stop}"
60
54
  when "complete"
61
- abort("That task is already completed!") if todo.completed?
55
+ KBSecret::CLI.die "That task is already completed!" if todo.completed?
62
56
  todo.complete!
63
57
  puts "#{todo.label}: '#{todo.todo}' marked as completed at #{todo.stop}"
64
58
  else
65
- abort("Fatal: Unknown action: #{command}.")
59
+ KBSecret::CLI.die "Unknown action: #{command}."
66
60
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pastel"
4
+
5
+ module KBSecret
6
+ # An encapsulation of useful methods for kbsecret's CLI.
7
+ module CLI
8
+ class << self
9
+ # The pastel object used to generate colorful output.
10
+ PASTEL = Pastel.new
11
+
12
+ # Print an error message and terminate.
13
+ # @param msg [String] the message to print
14
+ # @return [void]
15
+ # @note This method does not return!
16
+ def die(msg)
17
+ pretty = "#{PASTEL.bright_red("Fatal")}: #{msg}"
18
+ abort pretty
19
+ end
20
+
21
+ # Instantiate a session if it exists, and terminate otherwise.
22
+ # @param sess_label [String, Symbol] the session label to instantiate
23
+ # @return [void]
24
+ # @note This method does not return if the given session is not configured!
25
+ def ensure_session(sess_label)
26
+ die "Unknown session: '#{sess_label}'." unless Config.session? sess_label
27
+
28
+ Session.new label: sess_label
29
+ end
30
+ end
31
+ end
32
+ end
@@ -28,17 +28,17 @@ module KBSecret
28
28
  }.freeze
29
29
 
30
30
  # Retrieve a configured value.
31
- # @param key [String]
31
+ # @param key [String] the configuration key to retrieve
32
32
  # @return [Object] the corresponding configuration
33
33
  def self.[](key)
34
34
  @config[key]
35
35
  end
36
36
 
37
37
  # Retrieve a session's configuration.
38
- # @param sess [Symbol] the session's label
38
+ # @param sess [String, Symbol] the session's label
39
39
  # @return [Hash] the session configuration
40
40
  def self.session(sess)
41
- @config[:sessions][sess]
41
+ @config[:sessions][sess.to_sym]
42
42
  end
43
43
 
44
44
  # @return [Array<Symbol>] all configured session labels
@@ -46,7 +46,7 @@ module KBSecret
46
46
  @config[:sessions].keys
47
47
  end
48
48
 
49
- # @param sess [Symbol] the session label
49
+ # @param sess [String, Symbol] the session label
50
50
  # @return [Boolean] whether or not the given session is configured
51
51
  def self.session?(sess)
52
52
  session_labels.include?(sess.to_sym)
@@ -31,6 +31,7 @@ module KBSecret
31
31
  # @param type [String, Symbol] the record type
32
32
  # @return [Boolean] whether a record class exists of the given type
33
33
  def self.type?(type)
34
+ return false unless type
34
35
  record_types.include?(type.to_sym)
35
36
  end
36
37
 
data/lib/kbsecret.rb CHANGED
@@ -6,11 +6,12 @@ require_relative "kbsecret/config"
6
6
  require_relative "kbsecret/exceptions"
7
7
  require_relative "kbsecret/record"
8
8
  require_relative "kbsecret/session"
9
+ require_relative "kbsecret/cli"
9
10
 
10
11
  # The primary namespace for kbsecret.
11
12
  module KBSecret
12
13
  # kbsecret's current version
13
- VERSION = "0.3.0"
14
+ VERSION = "0.3.1"
14
15
 
15
16
  # fail very early if the user doesn't have keybase and KBFS running
16
17
  raise Keybase::KeybaseNotRunningError unless Keybase.running?
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.3.0
4
+ version: 0.3.1
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-05-30 00:00:00.000000000 Z
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: keybase-unofficial
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.10.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pastel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: clipboard
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +118,7 @@ files:
104
118
  - bin/kbsecret-stash-file
105
119
  - bin/kbsecret-todo
106
120
  - lib/kbsecret.rb
121
+ - lib/kbsecret/cli.rb
107
122
  - lib/kbsecret/config.rb
108
123
  - lib/kbsecret/exceptions.rb
109
124
  - lib/kbsecret/record.rb