ops_team 0.8.10 → 0.9.4

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: caa9b859d0dac66be5cc981ed872b591a6c174a5c70dabdf145d415681960831
4
- data.tar.gz: '08beac67a93906cb88db284977d1a10863820de5fd73369fb782f530cc8ec601'
3
+ metadata.gz: eba253171a96866401be036ddbe987211414bd7f0ab1547efacc14880440effc
4
+ data.tar.gz: c223d51872392ffa6850af158c6c7611e737c9f346ea7f04f10236ea4548febe
5
5
  SHA512:
6
- metadata.gz: 472f261d6d94bc4024576b2a322fe69bd10b53b5892efbcc89027a825723901c9a1aede0cc393cd8fae372dba49bdeb95c703b912f4d83ecf6711de8f3541caa
7
- data.tar.gz: 2a3a3ae0d383b40bec9cfaa79d705520f46b885f510aaab3833e763114a9c10a8b90af5a6c6285942e53057508643da93ad32f629fc4812819e24e44969ba750
6
+ metadata.gz: 438fe1576dc499d1474a47f38fb47a27206c6337b0f98dfc446c8605789e61b0987dc028f78626213a6ac6df3693a7ec41a6d7cef9e04a3db0ededb11cc25982
7
+ data.tar.gz: b67faeea77ceea232adcadb7361d2070b2156e6a361d328c9981b5bbaf2aff61b78236d82ed6e719e98eff359fec20a83c7bbb796b2f4f5fe06494e147d76b82
@@ -1,10 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Builtin
4
+ attr_reader :args, :config
5
+
4
6
  class << self
5
7
  def description
6
8
  "no description"
7
9
  end
10
+
11
+ def class_for(name:)
12
+ Builtins.const_get(builtin_class_name_for(name: name), false)
13
+ end
14
+
15
+ private
16
+
17
+ def builtin_class_name_for(name:)
18
+ name.capitalize.to_sym
19
+ end
8
20
  end
9
21
 
10
22
  def initialize(args, config)
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'builtin'
4
+
5
+ module Builtins
6
+ class Background < Builtin
7
+ DEFAULT_LOG_FILE_PREFIX = "/tmp/ops_bglog_"
8
+
9
+ class << self
10
+ def description
11
+ "runs the given command in a background session"
12
+ end
13
+
14
+ def log_filename
15
+ Options.get("background.log_filename") || "#{DEFAULT_LOG_FILE_PREFIX}#{Ops.project_name}"
16
+ end
17
+ end
18
+
19
+ def run
20
+ subprocess = fork do
21
+ run_ops(args)
22
+ end
23
+
24
+ Process.detach(subprocess)
25
+ end
26
+
27
+ private
28
+
29
+ def run_ops(args)
30
+ Output.notice("Running '#{args.join(' ')}' with stderr and stdout redirected to '#{Background.log_filename}'")
31
+ $stdout.sync = $stderr.sync = true
32
+ $stdout.reopen(Background.log_filename, "w")
33
+ $stderr.reopen($stdout)
34
+
35
+ Ops.new(args).run
36
+ end
37
+ end
38
+
39
+ # set an alias
40
+ Bg = Background
41
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'builtin'
4
+ require 'builtins/background'
5
+
6
+ module Builtins
7
+ class BackgroundLog < Builtin
8
+ class << self
9
+ def description
10
+ "displays the log from the current or most recent background task from this project"
11
+ end
12
+ end
13
+
14
+ def run
15
+ unless File.exist?(Background.log_filename)
16
+ Output.warn("No background log found at '#{Background.log_filename}'.")
17
+ return 0
18
+ end
19
+
20
+ Output.notice("Displaying background log '#{Background.log_filename}'...")
21
+ display_file
22
+ end
23
+
24
+ private
25
+
26
+ def display_file
27
+ if args.any?
28
+ exec("tail #{args.join(' ')} '#{Background.log_filename}'")
29
+ else
30
+ exec("cat '#{Background.log_filename}'")
31
+ end
32
+ end
33
+ end
34
+
35
+ # set an alias
36
+ Bglog = BackgroundLog
37
+ end
@@ -23,14 +23,32 @@ module Builtins
23
23
  private
24
24
 
25
25
  def builtins
26
- builtin_class_names.map do |class_name|
27
- description = Builtins.const_get(class_name).description
28
- format("%<name>-35s %<desc>s", name: class_name.downcase.to_s.yellow, desc: description)
26
+ builtin_class_map.map do |klass, name|
27
+ format("%<name>-35s %<desc>s", name: name.downcase.to_s.yellow, desc: klass.description)
28
+ end
29
+ end
30
+
31
+ def builtin_class_map
32
+ builtin_class_names.each_with_object({}) do |name, hash|
33
+ # get the class reference for this name
34
+ constant = const_for(name)
35
+ # check hash for an existing entry for the same class
36
+ existing_name = hash[constant]
37
+
38
+ # if there is an existing key for the same class, and it's longer than the one we just found,
39
+ # skip adding this one one to avoid duplicates, leaving the shortest name for each class
40
+ next if existing_name && existing_name.length <= name.length
41
+
42
+ hash[constant] = name
29
43
  end
30
44
  end
31
45
 
32
46
  def builtin_class_names
33
- Builtins.constants.select { |c| Builtins.const_get(c).is_a? Class }
47
+ @builtin_class_names ||= Builtins.constants.select { |c| const_for(c).is_a?(Class) }.sort
48
+ end
49
+
50
+ def const_for(name)
51
+ Builtins.const_get(name, false)
34
52
  end
35
53
 
36
54
  def actions
@@ -39,7 +57,7 @@ module Builtins
39
57
  name: name.yellow,
40
58
  desc: value["description"] || value["command"]
41
59
  )
42
- end
60
+ end.sort
43
61
  end
44
62
  end
45
63
  end
@@ -57,8 +57,7 @@ module Dependencies
57
57
  end
58
58
 
59
59
  def key_comment
60
- # the current directory is usually named for the project
61
- File.basename(::Dir.pwd)
60
+ Ops.project_name
62
61
  end
63
62
 
64
63
  def dir_name
data/lib/ops.rb CHANGED
@@ -19,6 +19,12 @@ class Ops
19
19
  INVALID_SYNTAX_EXIT_CODE = 64
20
20
  UNKNOWN_ACTION_EXIT_CODE = 65
21
21
 
22
+ class << self
23
+ def project_name
24
+ File.basename(::Dir.pwd)
25
+ end
26
+ end
27
+
22
28
  def initialize(argv)
23
29
  @action_name = argv[0]
24
30
  @args = argv[1..-1]
@@ -27,7 +33,7 @@ class Ops
27
33
  end
28
34
 
29
35
  def run
30
- exit(INVALID_SYNTAX_EXIT_CODE) unless syntax_valid?
36
+ return exit(INVALID_SYNTAX_EXIT_CODE) unless syntax_valid?
31
37
 
32
38
  run_action
33
39
  rescue UnknownActionError => e
@@ -39,8 +45,7 @@ class Ops
39
45
 
40
46
  def syntax_valid?
41
47
  if @action_name.nil?
42
- # TODO: output to stderr
43
- puts "Usage: ops <action>"
48
+ Output.error("Usage: ops <action>")
44
49
  false
45
50
  else
46
51
  true
@@ -53,21 +58,17 @@ class Ops
53
58
 
54
59
  return builtin.run if builtin
55
60
 
56
- Output.warn("Running '#{action}' from #{CONFIG_FILE} in environment '#{ENV['environment']}'...")
61
+ Output.notice("Running '#{action}' from #{CONFIG_FILE} in environment '#{ENV['environment']}'...")
57
62
  action.run
58
63
  end
59
64
 
60
65
  def builtin
61
- @builtin ||= Builtins.const_get(builtin_class_name, false).new(@args, config)
66
+ @builtin ||= Builtin.class_for(name: @action_name).new(@args, config)
62
67
  rescue NameError
63
68
  # this means there isn't a builtin with that name in that module
64
69
  nil
65
70
  end
66
71
 
67
- def builtin_class_name
68
- @action_name.capitalize.to_sym
69
- end
70
-
71
72
  def action
72
73
  return actions[@action_name] if actions[@action_name]
73
74
  return aliases[@action_name] if aliases[@action_name]
@@ -40,6 +40,8 @@ class Output
40
40
  @err.puts(msg.yellow)
41
41
  end
42
42
 
43
+ alias_method :notice, :warn
44
+
43
45
  def error(msg)
44
46
  @err.puts(msg.red)
45
47
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'ops_team'
5
- s.version = '0.8.10'
5
+ s.version = '0.9.4'
6
6
  s.authors = [
7
7
  'nickthecook@gmail.com'
8
8
  ]
9
- s.date = '2020-08-11'
9
+ s.date = '2020-08-12'
10
10
  s.summary = 'ops_team handles basic operations tasks for your project, driven by YAML config'
11
11
  s.homepage = 'https://github.com/nickthecook/ops'
12
12
  s.files = Dir[
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ops_team
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.10
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - nickthecook@gmail.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2020-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt_pbkdf
@@ -148,6 +148,8 @@ files:
148
148
  - lib/action.rb
149
149
  - lib/app_config.rb
150
150
  - lib/builtin.rb
151
+ - lib/builtins/background.rb
152
+ - lib/builtins/background_log.rb
151
153
  - lib/builtins/down.rb
152
154
  - lib/builtins/env.rb
153
155
  - lib/builtins/exec.rb