ronin-core 0.1.3 → 0.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/ChangeLog.md +25 -0
  4. data/Gemfile +1 -1
  5. data/README.md +2 -2
  6. data/gemspec.yml +1 -1
  7. data/lib/ronin/core/class_registry.rb +1 -1
  8. data/lib/ronin/core/cli/banner.rb +60 -0
  9. data/lib/ronin/core/cli/command.rb +1 -1
  10. data/lib/ronin/core/cli/command_shell/command.rb +1 -1
  11. data/lib/ronin/core/cli/command_shell.rb +12 -1
  12. data/lib/ronin/core/cli/completion_command.rb +204 -0
  13. data/lib/ronin/core/cli/generator/options/author.rb +1 -1
  14. data/lib/ronin/core/cli/generator/options/description.rb +1 -1
  15. data/lib/ronin/core/cli/generator/options/reference.rb +1 -1
  16. data/lib/ronin/core/cli/generator/options/summary.rb +1 -1
  17. data/lib/ronin/core/cli/generator.rb +1 -1
  18. data/lib/ronin/core/cli/help/banner.rb +49 -0
  19. data/lib/ronin/core/cli/logging.rb +3 -3
  20. data/lib/ronin/core/cli/options/param.rb +1 -1
  21. data/lib/ronin/core/cli/options/values/arches.rb +1 -1
  22. data/lib/ronin/core/cli/options/values/oses.rb +1 -1
  23. data/lib/ronin/core/cli/printing/arch.rb +1 -1
  24. data/lib/ronin/core/cli/printing/metadata.rb +33 -8
  25. data/lib/ronin/core/cli/printing/os.rb +1 -1
  26. data/lib/ronin/core/cli/printing/params.rb +30 -1
  27. data/lib/ronin/core/cli/ruby_shell.rb +6 -2
  28. data/lib/ronin/core/cli/shell.rb +6 -1
  29. data/lib/ronin/core/git.rb +1 -1
  30. data/lib/ronin/core/home.rb +1 -1
  31. data/lib/ronin/core/metadata/authors/author.rb +55 -15
  32. data/lib/ronin/core/metadata/authors.rb +4 -1
  33. data/lib/ronin/core/metadata/description.rb +1 -1
  34. data/lib/ronin/core/metadata/id.rb +1 -1
  35. data/lib/ronin/core/metadata/references.rb +1 -1
  36. data/lib/ronin/core/metadata/summary.rb +1 -1
  37. data/lib/ronin/core/metadata/version.rb +1 -1
  38. data/lib/ronin/core/output_formats/csv.rb +58 -0
  39. data/lib/ronin/core/output_formats/json.rb +90 -0
  40. data/lib/ronin/core/output_formats/ndjson.rb +55 -0
  41. data/lib/ronin/core/output_formats/output_dir.rb +93 -0
  42. data/lib/ronin/core/output_formats/output_file.rb +89 -0
  43. data/lib/ronin/core/output_formats/output_format.rb +48 -0
  44. data/lib/ronin/core/output_formats/txt.rb +50 -0
  45. data/lib/ronin/core/output_formats.rb +89 -0
  46. data/lib/ronin/core/params/exceptions.rb +1 -1
  47. data/lib/ronin/core/params/mixin.rb +1 -1
  48. data/lib/ronin/core/params/param.rb +1 -1
  49. data/lib/ronin/core/params/types/boolean.rb +1 -1
  50. data/lib/ronin/core/params/types/enum.rb +1 -1
  51. data/lib/ronin/core/params/types/float.rb +1 -1
  52. data/lib/ronin/core/params/types/integer.rb +1 -1
  53. data/lib/ronin/core/params/types/numeric.rb +1 -1
  54. data/lib/ronin/core/params/types/regexp.rb +1 -1
  55. data/lib/ronin/core/params/types/string.rb +1 -1
  56. data/lib/ronin/core/params/types/type.rb +1 -1
  57. data/lib/ronin/core/params/types/uri.rb +1 -1
  58. data/lib/ronin/core/params/types.rb +1 -1
  59. data/lib/ronin/core/params.rb +1 -1
  60. data/lib/ronin/core/system.rb +144 -0
  61. data/lib/ronin/core/version.rb +2 -2
  62. metadata +16 -4
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -61,6 +61,35 @@ module Ronin
61
61
  indent do
62
62
  print_table(rows,header: PARAM_TABLE_HEADER, border: :line)
63
63
  end
64
+
65
+ puts
66
+ end
67
+
68
+ #
69
+ # Returns a placeholder usage value for the given param.
70
+ #
71
+ # @param [Core::Params::Param] param
72
+ # The param.
73
+ #
74
+ # @return [String]
75
+ # The placeholder usage value.
76
+ #
77
+ # @note
78
+ # This method is primarily used to help build example commands
79
+ # that accept certain params.
80
+ #
81
+ # @since 0.2.0
82
+ #
83
+ def param_usage(param)
84
+ case param.type
85
+ when Core::Params::Types::Boolean then 'BOOL'
86
+ when Core::Params::Types::Integer then 'NUM'
87
+ when Core::Params::Types::Float then 'FLOAT'
88
+ when Core::Params::Types::Regexp then '/REGEX/'
89
+ when Core::Params::Types::URI then 'URL'
90
+ else
91
+ param.name.upcase
92
+ end
64
93
  end
65
94
  end
66
95
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -16,8 +16,9 @@
16
16
  # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
17
  #
18
18
 
19
- require 'command_kit/colors'
19
+ require 'ronin/core/cli/banner'
20
20
 
21
+ require 'command_kit/colors'
21
22
  require 'irb'
22
23
 
23
24
  module Ronin
@@ -30,6 +31,7 @@ module Ronin
30
31
  #
31
32
  class RubyShell
32
33
 
34
+ include Banner
33
35
  include CommandKit::Colors
34
36
 
35
37
  # The console name.
@@ -104,6 +106,8 @@ module Ronin
104
106
  # [irb]: https://github.com/ruby/irb#readme
105
107
  #
106
108
  def start
109
+ print_banner if STDOUT.tty?
110
+
107
111
  configure
108
112
 
109
113
  workspace = if @context then IRB::WorkSpace.new(@context)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -16,6 +16,8 @@
16
16
  # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
17
  #
18
18
 
19
+ require 'ronin/core/cli/banner'
20
+
19
21
  require 'command_kit/printing'
20
22
  require 'command_kit/colors'
21
23
  require 'reline'
@@ -28,6 +30,7 @@ module Ronin
28
30
  #
29
31
  class Shell
30
32
 
33
+ include Banner
31
34
  include CommandKit::Printing
32
35
  include CommandKit::Colors
33
36
 
@@ -89,6 +92,8 @@ module Ronin
89
92
  prev_completion_proc = Reline.completion_proc
90
93
  Reline.completion_proc = shell.method(:complete)
91
94
 
95
+ shell.print_banner if shell.stdout.tty?
96
+
92
97
  begin
93
98
  loop do
94
99
  line = Reline.readline("#{shell.prompt} ", true)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -65,6 +65,13 @@ module Ronin
65
65
  # @return [String, nil]
66
66
  attr_reader :twitter
67
67
 
68
+ # The author's Mastodon handle.
69
+ #
70
+ # @return [String, nil]
71
+ #
72
+ # @since 0.2.0
73
+ attr_reader :mastodon
74
+
68
75
  # The author's Discord handle.
69
76
  #
70
77
  # @return [String, nil]
@@ -97,27 +104,32 @@ module Ronin
97
104
  # @param [String, nil] twitter
98
105
  # The author's Twitter handle.
99
106
  #
107
+ # @param [String, nil] mastodon
108
+ # The author's Mastodon handle.
109
+ #
100
110
  # @param [String, nil] discord
101
111
  # The author's Discord handle.
102
112
  #
103
- def initialize(name, email: nil,
104
- pgp: nil,
105
- website: nil,
106
- blog: nil,
107
- github: nil,
108
- gitlab: nil,
109
- twitter: nil,
110
- discord: nil)
113
+ def initialize(name, email: nil,
114
+ pgp: nil,
115
+ website: nil,
116
+ blog: nil,
117
+ github: nil,
118
+ gitlab: nil,
119
+ twitter: nil,
120
+ mastodon: nil,
121
+ discord: nil)
111
122
  @name = name
112
123
  @email = email
113
124
  @pgp = pgp
114
125
 
115
- @website = website
116
- @blog = blog
117
- @github = github
118
- @gitlab = gitlab
119
- @twitter = twitter
120
- @discord = discord
126
+ @website = website
127
+ @blog = blog
128
+ @github = github
129
+ @gitlab = gitlab
130
+ @twitter = twitter
131
+ @mastodon = mastodon
132
+ @discord = discord
121
133
  end
122
134
 
123
135
  #
@@ -183,6 +195,17 @@ module Ronin
183
195
  @twitter != nil
184
196
  end
185
197
 
198
+ #
199
+ # Determines if the author has a {#mastodon} handle set.
200
+ #
201
+ # @return [Boolean]
202
+ #
203
+ # @since 0.2.0
204
+ #
205
+ def mastodon?
206
+ @mastodon != nil
207
+ end
208
+
186
209
  #
187
210
  # Determines if the author has a {#discord} handle set.
188
211
  #
@@ -225,6 +248,23 @@ module Ronin
225
248
  "https://twitter.com/#{@twitter.sub(/\A@/,'')}" if @twitter
226
249
  end
227
250
 
251
+ #
252
+ # Returns the URL to the author's Mastodon profile.
253
+ #
254
+ # @return [String, nil]
255
+ # Returns the URL to the author's Mastodon profile, or `nil`
256
+ # if no {#mastodon} user name has been set.
257
+ #
258
+ # @since 0.2.0
259
+ #
260
+ def mastodon_url
261
+ if @mastodon
262
+ username, host = @mastodon.sub(/\A@/,'').split('@',2)
263
+
264
+ "https://#{host}/@#{username}"
265
+ end
266
+ end
267
+
228
268
  #
229
269
  # Converts the author to a String.
230
270
  #
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -102,6 +102,9 @@ module Ronin
102
102
  # @option kwargs [String, nil] :twitter
103
103
  # The author's Twitter handle.
104
104
  #
105
+ # @option kwargs [String, nil] :mastodon
106
+ # The author's Mastodon handle.
107
+ #
105
108
  # @option kwargs [String, nil] :discord
106
109
  # The author's Discord handle.
107
110
  #
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
- # Copyright (c) 2021-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
4
  #
5
5
  # ronin-core is free software: you can redistribute it and/or modify
6
6
  # it under the terms of the GNU Lesser General Public License as published
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
+ #
5
+ # ronin-core is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # ronin-core is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ require 'ronin/core/output_formats/output_file'
20
+
21
+ require 'csv'
22
+
23
+ module Ronin
24
+ module Core
25
+ module OutputFormats
26
+ #
27
+ # Represents a CSV (`.csv`) output format.
28
+ #
29
+ # @api semipublic
30
+ #
31
+ # @since 0.2.0
32
+ #
33
+ class CSV < OutputFile
34
+
35
+ #
36
+ # Appends a value to the CSV stream.
37
+ #
38
+ # @param [#to_csv] value
39
+ # The value to append.
40
+ #
41
+ # @raise [NotImplementedError]
42
+ # The given output value does not define a `to_csv` method.
43
+ #
44
+ def <<(value)
45
+ unless value.respond_to?(:to_csv)
46
+ raise(NotImplementedError,"output value must define a #to_csv method: #{value.inspect}")
47
+ end
48
+
49
+ @io.write(value.to_csv)
50
+ @io.flush
51
+
52
+ return self
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
+ #
5
+ # ronin-core is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # ronin-core is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ require 'ronin/core/output_formats/output_file'
20
+
21
+ require 'json'
22
+
23
+ module Ronin
24
+ module Core
25
+ module OutputFormats
26
+ #
27
+ # Represents a JSON (`.json`) output format.
28
+ #
29
+ # @api semipublic
30
+ #
31
+ # @since 0.2.0
32
+ #
33
+ class JSON < OutputFile
34
+
35
+ #
36
+ # Initializes the JSON output format.
37
+ #
38
+ # @param [IO] io
39
+ # The JSON output stream.
40
+ #
41
+ def initialize(io)
42
+ super(io)
43
+
44
+ @first_value = true
45
+ end
46
+
47
+ #
48
+ # Appends a value to the JSON stream.
49
+ #
50
+ # @param [#to_json] value
51
+ # The value to append.
52
+ #
53
+ # @return [self]
54
+ #
55
+ # @raise [NotImplementedError]
56
+ # The given value object does not define a `to_json` method.
57
+ #
58
+ def <<(value)
59
+ unless value.respond_to?(:to_json)
60
+ raise(NotImplementedError,"output value does not define a #to_json method: #{value.inspect}")
61
+ end
62
+
63
+ if @first_value
64
+ @io.write("[#{value.to_json}")
65
+ @first_value = false
66
+ else
67
+ @io.write(",#{value.to_json}")
68
+ end
69
+
70
+ @io.flush
71
+ return self
72
+ end
73
+
74
+ #
75
+ # Closes the JSON output stream.
76
+ #
77
+ def close
78
+ if @first_value
79
+ @io.write("[]")
80
+ else
81
+ @io.write("]")
82
+ end
83
+
84
+ super
85
+ end
86
+
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
+ #
5
+ # ronin-core is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # ronin-core is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ require 'ronin/core/output_formats/output_file'
20
+
21
+ require 'json'
22
+
23
+ module Ronin
24
+ module Core
25
+ module OutputFormats
26
+ #
27
+ # Represents a newline deliminated JSON (`.ndjson`) output format.
28
+ #
29
+ # @api semipublic
30
+ #
31
+ # @since 0.2.0
32
+ #
33
+ class NDJSON < OutputFile
34
+
35
+ #
36
+ # Appends a value to the NDJSON stream.
37
+ #
38
+ # @param [#to_json] value
39
+ # The value to append.
40
+ #
41
+ # @return [self]
42
+ #
43
+ # @raise [NotImplementedError]
44
+ # The given value object does not define a `to_json` method.
45
+ #
46
+ def <<(value)
47
+ @io.puts(value.to_json)
48
+ @io.flush
49
+ return self
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
+ #
5
+ # ronin-core is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # ronin-core is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ require 'ronin/core/output_formats/output_format'
20
+
21
+ require 'fileutils'
22
+
23
+ module Ronin
24
+ module Core
25
+ module OutputFormats
26
+ #
27
+ # Base class that represents an output directory.
28
+ #
29
+ # @api semipublic
30
+ #
31
+ # @since 0.2.0
32
+ #
33
+ class OutputDir < OutputFormat
34
+
35
+ # The path to the output directory.
36
+ #
37
+ # @return [String]
38
+ attr_reader :path
39
+
40
+ #
41
+ # Initializes the output directory.
42
+ #
43
+ # @param [String] path
44
+ # The path to the output directory.
45
+ #
46
+ def initialize(path)
47
+ super()
48
+
49
+ @path = File.expand_path(path)
50
+ FileUtils.mkdir_p(@path)
51
+ end
52
+
53
+ #
54
+ # Opens an output directory.
55
+ #
56
+ # @param [String] path
57
+ # The path to the new output directory.
58
+ #
59
+ # @yield [output_dir]
60
+ # If a block is given, it will be passed the initialized output
61
+ # directory. Once the block returns, the output format will
62
+ # automatically be closed.
63
+ #
64
+ # @yieldparam [Dir] output_dir
65
+ # The newly opened output directory.
66
+ #
67
+ # @return [Dir]
68
+ # If no block is given, the newly initialized output directory will be
69
+ # returned.
70
+ #
71
+ def self.open(path)
72
+ output_dir = new(path)
73
+
74
+ if block_given?
75
+ yield output_dir
76
+ output_dir.close
77
+ else
78
+ return output_dir
79
+ end
80
+ end
81
+
82
+ #
83
+ # "Closes" the output directory.
84
+ #
85
+ # @note This method is mainly for compatibility with {OutputFile}.
86
+ #
87
+ def close
88
+ end
89
+
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright (c) 2021-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
4
+ #
5
+ # ronin-core is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published
7
+ # by the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # ronin-core is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with ronin-core. If not, see <https://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ require 'ronin/core/output_formats/output_format'
20
+
21
+ module Ronin
22
+ module Core
23
+ module OutputFormats
24
+ #
25
+ # Base class for all output file formats.
26
+ #
27
+ # @api semipublic
28
+ #
29
+ # @since 0.2.0
30
+ #
31
+ class OutputFile < OutputFormat
32
+
33
+ # The output stream to write to.
34
+ #
35
+ # @return [IO]
36
+ attr_reader :io
37
+
38
+ #
39
+ # Initializes the output format.
40
+ #
41
+ # @param [IO] io
42
+ # The output stream to write to.
43
+ #
44
+ def initialize(io)
45
+ super()
46
+
47
+ @io = io
48
+ end
49
+
50
+ #
51
+ # Opens an output file.
52
+ #
53
+ # @param [String] path
54
+ # The path to the new output file.
55
+ #
56
+ # @yield [output_format]
57
+ # If a block is given, it will be passed the initialized output
58
+ # format. Once the block returns, the output format will automatically
59
+ # be closed.
60
+ #
61
+ # @yieldparam [OutputFormat] output_format
62
+ # The newly opened output format.
63
+ #
64
+ # @return [OutputFormat]
65
+ # If no block is given, the newly initialized output format will be
66
+ # returned.
67
+ #
68
+ def self.open(path)
69
+ output = new(File.open(path,'w'))
70
+
71
+ if block_given?
72
+ yield output
73
+ output.close
74
+ else
75
+ return output
76
+ end
77
+ end
78
+
79
+ #
80
+ # Closes the output file.
81
+ #
82
+ def close
83
+ @io.close unless @io == $stdout
84
+ end
85
+
86
+ end
87
+ end
88
+ end
89
+ end