ops_team 0.12.3 → 0.13.0

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
  SHA256:
3
- metadata.gz: 444384ee0931455838357c9bc2f720534e8fbda90d5d27a15c35b313cf676b4e
4
- data.tar.gz: 03ff8c264a9083987aac65d9a7ba78fdb8d118e757599c66267725178b8fc809
3
+ metadata.gz: 9656333af7b4f103645f9f27af758b4cc11e749c0e9770474ec249af8db85983
4
+ data.tar.gz: d1a69bbc44b1626bbcd754fa8484547f2bfa3beede615d8ebcd951e00432008a
5
5
  SHA512:
6
- metadata.gz: f596c660d5b7943258c3615a603f36f74ea5d98488d9171562c75593f3e5d5cbe93a349d687a417649dcb0be40de13c9c7d3c33c94b3d7686a43c3f2c94ea2cb
7
- data.tar.gz: 486531d3b6c027986cde18d88f3d43e6ffc7db44b4688a8528ccd33b1b0dcb0b883ff6b8048f0b168d3b9fa3d58d8f018722c12a95cdb3edf3e8820c5c3e4b02
6
+ metadata.gz: 73e177e194c2f3e7415612bd23aea779502182929fbb5264c00a1516a52869056bb045e75ff5aaca4c546b92f03481f5346eb24958f4a6ccdd247d0d3cdc06dc
7
+ data.tar.gz: 53c224dd9162d269c0154c842ec36d4e748dd59b401efb1a959199332e3ee99489a1de4cf88d3e2d2bff7c04a37906c687d542ac2bc4852d7fa38a0f4e0ad9c6
data/bin/ops CHANGED
@@ -5,4 +5,6 @@ require_relative "../loader"
5
5
 
6
6
  require 'ops'
7
7
 
8
+ require 'pry-byebug' if ENV['environment'] == 'dev' || ENV['environment'].nil?
9
+
8
10
  Ops.new(ARGV).run
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActionList
4
+ class UnknownActionError < StandardError; end
5
+
6
+ def initialize(actions_list, args)
7
+ @actions_list = actions_list
8
+ @args = args
9
+
10
+ process_action_list
11
+ end
12
+
13
+ def get(name)
14
+ @actions[name]
15
+ end
16
+
17
+ def get_by_alias(name)
18
+ @aliases[name]
19
+ end
20
+
21
+ def names
22
+ @actions.keys
23
+ end
24
+
25
+ def aliases
26
+ @aliases.keys
27
+ end
28
+
29
+ private
30
+
31
+ def process_action_list
32
+ @actions = {}
33
+ @aliases = {}
34
+
35
+ @actions_list.each do |name, config|
36
+ action = Action.new(config, @args)
37
+
38
+ @actions[name] = action
39
+ @aliases[action.alias] = action
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActionSuggester
4
+ def initialize(dictionary)
5
+ @dictionary = dictionary
6
+ end
7
+
8
+ def check(word)
9
+ spellchecker.correct(word)
10
+ end
11
+
12
+ def spellchecker
13
+ @spellchecker ||= DidYouMean::SpellChecker.new(dictionary: @dictionary)
14
+ end
15
+ end
data/lib/ops.rb CHANGED
@@ -11,6 +11,9 @@ require 'output'
11
11
  require 'options'
12
12
  require 'environment'
13
13
  require 'version'
14
+ require 'action_list'
15
+ require 'action_suggester'
16
+
14
17
  require_rel "builtins"
15
18
 
16
19
  # executes commands based on local `ops.yml`
@@ -38,13 +41,14 @@ class Ops
38
41
  end
39
42
 
40
43
  def run
41
- # "return" is here to allow specs to stub "exit"
44
+ # "return" is here to allow specs to stub "exit" without executing everything after it
42
45
  return exit(INVALID_SYNTAX_EXIT_CODE) unless syntax_valid?
43
46
  return exit(MIN_VERSION_NOT_MET_EXIT_CODE) unless min_version_met?
44
47
 
45
48
  run_action
46
49
  rescue UnknownActionError => e
47
- Output.error("Error: #{e}")
50
+ Output.error(e.to_s)
51
+ print_did_you_mean
48
52
  exit(UNKNOWN_ACTION_EXIT_CODE)
49
53
  end
50
54
 
@@ -59,6 +63,16 @@ class Ops
59
63
  end
60
64
  end
61
65
 
66
+ def print_did_you_mean
67
+ suggestions = did_you_mean.check(@action_name)
68
+
69
+ Output.out("Did you mean '#{suggestions.join(", ")}'?") if suggestions.any?
70
+ end
71
+
72
+ def did_you_mean
73
+ ActionSuggester.new(action_list.names + action_list.aliases + builtin_names)
74
+ end
75
+
62
76
  def min_version_met?
63
77
  return true unless min_version
64
78
 
@@ -80,7 +94,6 @@ class Ops
80
94
  return builtin.run if builtin
81
95
 
82
96
  do_before_action
83
-
84
97
  Output.notice("Running '#{action}' from #{CONFIG_FILE} in environment '#{ENV['environment']}'...")
85
98
  action.run
86
99
  rescue AppConfig::ParsingError => e
@@ -111,24 +124,22 @@ class Ops
111
124
  nil
112
125
  end
113
126
 
127
+ def builtin_names
128
+ Builtins.constants.select { |c| Builtins.const_get(c).is_a? Class }.map(&:downcase)
129
+ end
130
+
114
131
  def action
115
- return actions[@action_name] if actions[@action_name]
116
- return aliases[@action_name] if aliases[@action_name]
132
+ return action_list.get(@action_name) if action_list.get(@action_name)
133
+ return action_list.get_by_alias(@action_name) if action_list.get_by_alias(@action_name)
117
134
 
118
135
  raise UnknownActionError, "Unknown action: #{@action_name}"
119
136
  end
120
137
 
121
- def actions
122
- @actions ||= begin
123
- if config["actions"]
124
- config["actions"]&.transform_values do |config|
125
- Action.new(config, @args)
126
- end
127
- else
128
- # only print this error if ops.yml had something in it
129
- Output.warn("'ops.yml' has no 'actions' defined.") if config.any?
130
- {}
131
- end
138
+ def action_list
139
+ @action_list ||= begin
140
+ Output.warn("'ops.yml' has no 'actions' defined.") if config.any? && config["actions"].nil?
141
+
142
+ ActionList.new(config["actions"], @args)
132
143
  end
133
144
  end
134
145
 
@@ -142,14 +153,6 @@ class Ops
142
153
  end
143
154
  end
144
155
 
145
- def aliases
146
- @aliases ||= begin
147
- actions.each_with_object({}) do |(_name, action), alias_hash|
148
- alias_hash[action.alias] = action if action.alias
149
- end
150
- end
151
- end
152
-
153
156
  def env_vars
154
157
  @config.dig("options", "environment") || {}
155
158
  end
@@ -157,10 +160,6 @@ class Ops
157
160
  def environment
158
161
  @environment ||= Environment.new(env_vars)
159
162
  end
160
-
161
- def app_config
162
- @app_config ||= AppConfig.new
163
- end
164
163
  end
165
164
 
166
165
  Ops.new(ARGV).run if $PROGRAM_NAME == __FILE__
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'ops_team'
5
- s.version = '0.12.3'
5
+ s.version = '0.13.0'
6
6
  s.authors = [
7
7
  'nickthecook@gmail.com'
8
8
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ops_team
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.3
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nickthecook@gmail.com
@@ -146,6 +146,8 @@ files:
146
146
  - etc/ruby.template.yml
147
147
  - etc/terraform.template.yml
148
148
  - lib/action.rb
149
+ - lib/action_list.rb
150
+ - lib/action_suggester.rb
149
151
  - lib/app_config.rb
150
152
  - lib/builtin.rb
151
153
  - lib/builtins/background.rb