vaultez-cli 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: f2898cfe0e904ce07df993ad6cf78fd5c66b0843a6a90dccafe0fc21f94c462a
4
- data.tar.gz: 1792ede773e3d10f26b7a233719690ec660402b21ce1e086e7d1a8b47f7706d9
3
+ metadata.gz: 5691471c3bf1c3a9c144c9dc7a6a3acb621e786a3ff6c8dda909a37cb840f9cd
4
+ data.tar.gz: 33fa1a5e4031ec260923526bdb9cc4bc7b99f301fe163cc71407963d92ace921
5
5
  SHA512:
6
- metadata.gz: e920faa7ff226351174c5eb7ada3ce7c71e06a8cc0d7dabff2171d60bf7ce244afb52543763351c9705a9951474c3dfe66ae814c1af9b0a1bd089138eb573bea
7
- data.tar.gz: 5efac1f679ae408baaf10bef7de70dd0c76601419f3b2b48daccf60fc1e36c635ca5546561623b55628292653e525d23e49c21fd0fbb73ffe075b10eb6d3037f
6
+ metadata.gz: a2a50894e4fbcb228b1d65579d93689ca22740574b917502a1d91664a7cf63def38e047362a06fb30b323be78e95f3e154de1c9ba1d7b03c1ac6ae6aeadb75bb
7
+ data.tar.gz: 6f59a21a570e06772789c18b52772420427a81c58840e0db1fa7cb6750bc040c1f8eb9366ca536bd4807b831d5d5e5b8bc0d4895465f33838bae53da12d1c43f
data/lib/vaultez/cli.rb CHANGED
@@ -69,6 +69,7 @@ module Vaultez
69
69
  option :projects, type: :boolean, desc: "List projects in a company"
70
70
  option :project, type: :string, desc: "Project name"
71
71
  option :secret, type: :string, desc: "Secret name (returns value only)"
72
+ option :json, type: :boolean, desc: "Output as JSON (errors go to stderr)"
72
73
  def fetch; super; end
73
74
 
74
75
  desc "config", "Set default company or token"
@@ -1,3 +1,5 @@
1
+ require "json"
2
+
1
3
  module Vaultez
2
4
  module Commands
3
5
  module Fetch
@@ -15,33 +17,45 @@ module Vaultez
15
17
  elsif options[:project]
16
18
  fetch_secrets(client)
17
19
  else
18
- puts "Error: not enough options. See `vaultez help fetch`."
19
- exit 1
20
+ fail!("not enough options. See `vaultez help fetch`.")
20
21
  end
21
22
  rescue Vaultez::NotAuthenticatedError => error
22
- puts "Error: #{error.message}"
23
- exit 1
23
+ fail!(error.message)
24
24
  rescue Vaultez::NotFoundError => error
25
- puts "Error: #{error.message}"
26
- exit 1
25
+ fail!(error.message)
27
26
  rescue Vaultez::ApiError => error
28
- puts "Error: #{error.message}"
29
- exit 1
27
+ fail!(error.message)
30
28
  end
31
29
 
32
30
  private
33
31
 
32
+ def fail!(message)
33
+ if options[:json]
34
+ warn "Error: #{message}"
35
+ else
36
+ puts "Error: #{message}"
37
+ end
38
+ exit 1
39
+ end
40
+
34
41
  def fetch_with_project_token(client)
35
42
  if options[:secret]
36
43
  secrets = fetch_all_secrets_for_token(client)
37
44
  secret = secrets.find { |s| s["name"] == options[:secret] }
38
45
  unless secret
39
- puts "Error: secret \"#{options[:secret]}\" not found."
40
- exit 1
46
+ fail!("secret \"#{options[:secret]}\" not found.")
47
+ end
48
+ if options[:json]
49
+ puts secret.to_json
50
+ else
51
+ print secret["value"]
41
52
  end
42
- print secret["value"]
43
53
  else
44
54
  secrets = fetch_all_secrets_for_token(client)
55
+ if options[:json]
56
+ puts secrets.to_json
57
+ return
58
+ end
45
59
  if secrets.empty?
46
60
  puts "No secrets found."
47
61
  return
@@ -54,20 +68,22 @@ module Vaultez
54
68
  companies = client.companies
55
69
  company = companies.first
56
70
  unless company
57
- puts "Error: no company found for this token."
58
- exit 1
71
+ fail!("no company found for this token.")
59
72
  end
60
73
  projects = client.projects(company["id"])
61
74
  project = projects.first
62
75
  unless project
63
- puts "Error: no project found for this token."
64
- exit 1
76
+ fail!("no project found for this token.")
65
77
  end
66
78
  client.secrets(project["id"])
67
79
  end
68
80
 
69
81
  def fetch_companies(client)
70
82
  companies = client.companies
83
+ if options[:json]
84
+ puts companies.to_json
85
+ return
86
+ end
71
87
  if companies.empty?
72
88
  puts "No companies found."
73
89
  return
@@ -81,6 +97,10 @@ module Vaultez
81
97
  def fetch_projects(client)
82
98
  company = resolve_company(client)
83
99
  projects = client.projects(company["id"])
100
+ if options[:json]
101
+ puts projects.to_json
102
+ return
103
+ end
84
104
  if projects.empty?
85
105
  puts "No projects found in #{company["name"]}."
86
106
  return
@@ -95,6 +115,10 @@ module Vaultez
95
115
  company = resolve_company(client)
96
116
  project = resolve_project(client, company)
97
117
  secrets = client.secrets(project["id"])
118
+ if options[:json]
119
+ puts secrets.to_json
120
+ return
121
+ end
98
122
  if secrets.empty?
99
123
  puts "No secrets found in #{project["name"]}."
100
124
  return
@@ -111,11 +135,14 @@ module Vaultez
111
135
  secret = secrets.find { |s| s["name"] == options[:secret] }
112
136
 
113
137
  unless secret
114
- puts "Error: secret \"#{options[:secret]}\" not found in #{project["name"]}."
115
- exit 1
138
+ fail!("secret \"#{options[:secret]}\" not found in #{project["name"]}.")
116
139
  end
117
140
 
118
- print secret["value"]
141
+ if options[:json]
142
+ puts secret.to_json
143
+ else
144
+ print secret["value"]
145
+ end
119
146
  end
120
147
 
121
148
  def resolve_company(client)
@@ -124,8 +151,7 @@ module Vaultez
124
151
  if options[:company]
125
152
  company = companies.find { |c| c["name"] == options[:company] }
126
153
  unless company
127
- puts "Error: company \"#{options[:company]}\" not found."
128
- exit 1
154
+ fail!("company \"#{options[:company]}\" not found.")
129
155
  end
130
156
  return company
131
157
  end
@@ -138,16 +164,14 @@ module Vaultez
138
164
 
139
165
  return companies.first if companies.size == 1
140
166
 
141
- puts "Error: multiple companies found. Specify one with --company or set a default with `vaultez config --default-company`."
142
- exit 1
167
+ fail!("multiple companies found. Specify one with --company or set a default with `vaultez config --default-company`.")
143
168
  end
144
169
 
145
170
  def resolve_project(client, company)
146
171
  projects = client.projects(company["id"])
147
172
  project = projects.find { |p| p["name"] == options[:project] }
148
173
  unless project
149
- puts "Error: project \"#{options[:project]}\" not found in #{company["name"]}."
150
- exit 1
174
+ fail!("project \"#{options[:project]}\" not found in #{company["name"]}.")
151
175
  end
152
176
  project
153
177
  end
@@ -1,3 +1,3 @@
1
1
  module Vaultez
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vaultez-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nur Ketene