ballantine 0.1.2 → 0.1.4.pre.beta

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: a2188ad3272546c25226bdbb1209075baadee05fb7f011cafe13874b1af45777
4
- data.tar.gz: 0fe5889c616c93c7f62ce8353671543750604fc4c6def9958abdfe3a0f6e5c42
3
+ metadata.gz: 36f79167ee934830b2a9d8a59273ffb68970b81a2a48ba21b28f11779227722b
4
+ data.tar.gz: 37c6726d842dc7dd153c6b8545200ab31fd97586ee1c07e1270bd318a745dd92
5
5
  SHA512:
6
- metadata.gz: ab52789a4ae2fb510d48f34cd7cf8c2920610e5f341fdbbd94f687fee7ace7ff1f1ff7cbce32fe2e4ce78a783762aa29cf1c9053be5f042ef5b12f84f9101b28
7
- data.tar.gz: bda07819f4146ac05b3ab1c39f4debe647f9c72a3a1dc50cfac433bf3381ecc6b96e96c92559775c08ecbf4f4e14b39a235a5a23d8636d2e956b6eddee33ad60
6
+ metadata.gz: acb862515fde0497f8b7c592c29610c79368063594d8f0f3daa38ed252beabdad2c1ce8fbb2970ee7f30d81e88c2a8d6b95f4158ca67d878ccea0a10bf290f9d
7
+ data.tar.gz: 0a3b6021c754125ba74bba213df06e860e0a793b3b0027640ae3ae559adce7544c61e4e2ef70832f0fcadc07207cd51428223f4083aa85fe40db8e1b725ca5e5
@@ -1,63 +1,67 @@
1
- #!/usr/bin/env ruby
1
+ # frozen_string_literal: true
2
2
 
3
- class Author
4
- attr_accessor :name, :commits
3
+ module Ballantine
4
+ class Author
5
+ attr_accessor :name, :commits
6
+
7
+ class << self
8
+ # @param [String] name
9
+ # @return [Author] author
10
+ def find_or_create_by(name)
11
+ @_collections = {} unless defined?(@_collections)
12
+ return @_collections[name] unless @_collections[name].nil?
13
+
14
+ @_collections[name] = new(name)
15
+ end
16
+
17
+ # @return [Array<Author>] authors
18
+ def all
19
+ return [] unless defined?(@_collections)
20
+
21
+ @_collections.sort.map(&:last) # sort and take values
22
+ end
23
+ end
5
24
 
6
- class << self
7
25
  # @param [String] name
8
- # @return [Author] author
9
- def find_or_create_by(name)
10
- @@_collections = {} unless defined?(@@_collections)
11
- return @@_collections[name] unless @@_collections[name].nil?
12
- @@_collections[name] = new(name)
26
+ def initialize(name)
27
+ @name = name
28
+ @commits = {}
13
29
  end
14
30
 
15
- # @return [Array<Author>] authors
16
- def all
17
- return [] unless defined?(@@_collections)
18
- @@_collections.sort.map(&:last) # sort and take values
31
+ # @return [NilClass] nil
32
+ def print_commits
33
+ puts "\n" + "@#{name}".green
34
+ @commits.each do |repo, lists|
35
+ count, word = retrieve_count_and_word(lists)
36
+ puts " > #{repo.blue}: #{count} new #{word}\n"
37
+ puts lists
38
+ end
39
+ nil
19
40
  end
20
- end
21
41
 
22
- # @param [String] name
23
- def initialize(name)
24
- @name = name
25
- @commits = {}
26
- end
42
+ # returns an array to use slack attachments field
43
+ # reference: https://api.slack.com/messaging/composing/layouts#building-attachments
44
+ # @return [Hash] result
45
+ def serialize_commits
46
+ message = @commits.map do |repo, lists|
47
+ count, word = retrieve_count_and_word(lists)
48
+ "*#{repo}*: #{count} new #{word}\n#{lists.join("\n")}"
49
+ end.join("\n")
27
50
 
28
- # @return [NilClass] nil
29
- def print_commits
30
- puts "\n" + "@#{name}".green
31
- @commits.each do |repo, lists|
32
- count, word = retrieve_count_and_word(lists)
33
- puts " > #{repo.blue}: #{count} new #{word}\n"
34
- puts lists
51
+ {
52
+ "text" => "- <@#{name}>\n#{message}",
53
+ "color" => "#00B86A", # green
54
+ }
35
55
  end
36
- nil
37
- end
38
56
 
39
- # returns an array to use slack attachments field
40
- # reference: https://api.slack.com/messaging/composing/layouts#building-attachments
41
- # @return [Hash] result
42
- def serialize_commits
43
- message = @commits.map do |repo, lists|
44
- count, word = retrieve_count_and_word(lists)
45
- "*#{repo}*: #{count} new #{word}\n#{lists.join("\n")}"
46
- end.join("\n")
47
-
48
- results = {
49
- 'text' => "- <@#{name}>\n#{message}",
50
- 'color' => '#00B86A', # green
51
- }
52
- end
57
+ private
53
58
 
54
- private
55
-
56
- # @param [Array<String>] lists
57
- # @param [Array(Integer, String)] count, word
58
- def retrieve_count_and_word(lists)
59
- count = lists.size
60
- word = count == 1 ? 'commit' : 'commits'
61
- [count, word]
59
+ # @param [Array<String>] lists
60
+ # @param [Array(Integer, String)] count, word
61
+ def retrieve_count_and_word(lists)
62
+ count = lists.size
63
+ word = count == 1 ? "commit" : "commits"
64
+ [count, word]
65
+ end
62
66
  end
63
67
  end
@@ -1,11 +1,6 @@
1
- #!/usr/bin/env ruby
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Ballantine
4
- require 'thor'
5
- require 'json'
6
- require_relative './author'
7
- require_relative './string'
8
-
9
4
  class CLI < Thor
10
5
  # reference: https://github.com/desktop/desktop/blob/a7bca44088b105a04714dc4628f4af50f6f179c3/app/src/lib/remote-parsing.ts#L27-L44
11
6
  GITHUB_REGEXES = [
@@ -18,141 +13,145 @@ module Ballantine
18
13
  '^ssh://git@(.+)/(.+)/(.+)\.git$', # protocol: ssh -> ssh://git@github.com/oohyun15/ballantine.git
19
14
  ].freeze
20
15
 
21
- FILE_GITMODULES = '.gitmodules'
22
- FILE_BALLANTINE_CONFIG = '.ballantine.json'
16
+ FILE_GITMODULES = ".gitmodules"
23
17
 
24
- TYPE_TERMINAL = 'terminal'
25
- TYPE_SLACK = 'slack'
18
+ TYPE_TERMINAL = "terminal"
19
+ TYPE_SLACK = "slack"
26
20
 
27
21
  DEFAULT_LJUST = 80
28
22
 
29
- AVAILABLE_CONFIG = ['slack_webhook'].freeze
30
-
31
- attr_reader :app_name, :main_path, :sub_path, :slack_webhook, :send_type
23
+ attr_reader :app_name, :main_path, :sub_path, :send_type
32
24
 
33
- package_name 'Ballantine'
25
+ class << self
26
+ def exit_on_failure?; exit(1) end
27
+ end
34
28
 
35
- desc 'init', 'Initialize ballantine configuration'
29
+ package_name "Ballantine"
30
+ option "force", type: :boolean, aliases: "-f", default: false, desc: "Initialize forcely if already initialized."
31
+ desc "init", "Initialize ballantine"
36
32
  def init
37
- puts "🥃 Init ballantine configuration"
38
- slack_webhook = ask("Q. Set slack webhook\n> ")
33
+ conf.init_file(force: options["force"])
39
34
 
40
- config = {
41
- slack_webhook: slack_webhook
42
- }
43
- File.write('./' + FILE_BALLANTINE_CONFIG, JSON.dump(config))
35
+ puts "🥃 Initialized ballantine."
36
+
37
+ true
44
38
  end
45
39
 
46
- desc 'config', 'Describe ballantine configuration'
47
- def config
48
- load_config
49
- AVAILABLE_CONFIG.each do |key|
50
- puts "#{key}: #{instance_variable_get('@' + key)}"
40
+ Config::AVAILABLE_ENVIRONMENTS.each { |env| option env, type: :boolean, default: false, desc: "Set envirment to `#{env}'." }
41
+ desc "config [--env] [KEY] [VALUE]", "Set ballantine's configuration"
42
+ def config(key = nil, value = nil)
43
+ # check environment value
44
+ if Config::AVAILABLE_ENVIRONMENTS.map { |key| !options[key] }.reduce(:&)
45
+ raise NotAllowed, "Set environment value (#{Config::AVAILABLE_ENVIRONMENTS.map { |key| "`--#{key}'" }.join(", ")})"
46
+ elsif Config::AVAILABLE_ENVIRONMENTS.map { |key| !!options[key] }.reduce(:&)
47
+ raise NotAllowed, "Environment value must be unique."
51
48
  end
52
- end
53
49
 
54
- desc 'diff [TARGET] [SOURCE]', 'Diff commits between TARGET and SOURCE'
55
- option TYPE_SLACK, type: :boolean, aliases: '-s', default: false, desc: 'send to slack using slack webhook URL.'
56
- def diff(from, to = `git rev-parse --abbrev-ref HEAD`.chomp)
57
- load_config
58
- preprocess(from, to, **options)
50
+ @env = Config::AVAILABLE_ENVIRONMENTS.find { |key| options[key] }
51
+ raise AssertionFailed, "Environment value must exist: #{@env}" if @env.nil?
59
52
 
60
- # check argument is tag
61
- from = check_tag(from)
62
- to = check_tag(to)
53
+ value ? conf.set_data(key, value) : conf.print_data(key)
54
+ end
55
+
56
+ desc "diff [TARGET] [SOURCE]", "Diff commits between TARGET and SOURCE"
57
+ option TYPE_SLACK, type: :boolean, aliases: "-s", default: false, desc: "Send to slack using slack webhook URL."
58
+ def diff(target, source = %x(git rev-parse --abbrev-ref HEAD).chomp)
59
+ # validate arguments
60
+ validate(target, source, **options)
63
61
 
64
62
  # check commits are newest
65
- system 'git pull -f &> /dev/null'
63
+ system("git pull -f &> /dev/null")
66
64
 
67
- # set instance variables
68
- @send_type = options[TYPE_SLACK] ? TYPE_SLACK : TYPE_TERMINAL
69
- @app_name = File.basename(`git config --get remote.origin.url`.chomp, '.git')
70
- @main_path = Dir.pwd
71
- @sub_path = if Dir[FILE_GITMODULES].any?
72
- file = File.open(FILE_GITMODULES)
73
- lines = file.readlines.map(&:chomp)
74
- file.close
75
- lines.grep(/path =/).map{ |line| line[/(?<=path \=).*/, 0].strip }.sort
76
- else
77
- []
78
- end
65
+ # init instance variables
66
+ init_variables(**options)
79
67
 
80
68
  # find github url, branch
81
- main_url = github_url(`git config --get remote.origin.url`.chomp)
82
- current_branch = `git rev-parse --abbrev-ref HEAD`.chomp
69
+ url = github_url(%x(git config --get remote.origin.url).chomp)
70
+ current_revision = %x(git rev-parse --abbrev-ref HEAD).chomp
83
71
 
84
72
  # get commit hash
85
- main_from, sub_from = commit_hash(from)
86
- main_to, sub_to = commit_hash(to)
87
- system "git checkout #{current_branch} -f &> /dev/null"
73
+ from, sub_from = commit_hash(target)
74
+ to, sub_to = commit_hash(source)
75
+ system("git checkout #{current_revision} -f &> /dev/null")
88
76
 
89
77
  # check commits
90
- check_commits(main_from, main_to, main_url)
91
- @sub_path.each_with_index do |path, idx|
78
+ check_commits(from, to, url)
79
+ sub_path.each_with_index do |path, idx|
92
80
  next if sub_from[idx] == sub_to[idx]
81
+
93
82
  Dir.chdir(path)
94
- sub_url = github_url(`git config --get remote.origin.url`.chomp)
83
+ sub_url = github_url(%x(git config --get remote.origin.url).chomp)
95
84
  check_commits(sub_from[idx], sub_to[idx], sub_url)
96
- Dir.chdir(@main_path)
85
+ Dir.chdir(main_path)
97
86
  end
98
87
 
99
- send_commits(from, to, main_url)
88
+ # send commits
89
+ send_commits(target, source, from, to, url)
100
90
 
101
- exit 0
91
+ exit(0)
102
92
  end
103
93
 
104
- desc 'version', 'Display version information about ballntine'
94
+ desc "version", "Display version information about ballntine"
105
95
  def version
106
96
  puts "ballantine version #{Ballantine::VERSION}"
107
- end
108
-
109
- private
110
97
 
111
- def self.exit_on_failure?
112
- exit 1
98
+ Ballantine::VERSION
113
99
  end
114
100
 
115
- def load_config
116
- return if Dir[FILE_BALLANTINE_CONFIG].empty?
101
+ private
117
102
 
118
- JSON.parse(File.read('./' + FILE_BALLANTINE_CONFIG)).each do |key, value|
119
- next unless AVAILABLE_CONFIG.include?(key)
120
- instance_variable_set('@' + key, value)
121
- end
122
- end
103
+ def conf; @conf ||= Config.new(@env) end
123
104
 
124
- # @param [String] from
125
- # @param [String] to
105
+ # @param [String] target
106
+ # @param [String] source
126
107
  # @param [Hash] options
127
108
  # @return [NilClass] nil
128
- def preprocess(from, to, **options)
129
- if Dir['.git'].empty?
130
- raise SystemCallError, "ERROR: There is no \".git\" in #{Dir.pwd}."
109
+ def validate(target, source, **options)
110
+ if Dir[".git"].empty?
111
+ raise NotAllowed, "ERROR: There is no \".git\" in #{Dir.pwd}."
131
112
  end
132
113
 
133
- if (uncommitted = `git diff HEAD --name-only`.split("\n")).any?
134
- raise SystemCallError, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n#{uncommitted.join("\n")}"
114
+ if (uncommitted = %x(git diff HEAD --name-only).split("\n")).any?
115
+ raise NotAllowed, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n#{uncommitted.join("\n")}"
135
116
  end
136
117
 
137
- if from == to
138
- raise ArgumentError, "ERROR: target(#{from}) and source(#{to}) can't be equal."
118
+ if target == source
119
+ raise NotAllowed, "ERROR: target(#{target}) and source(#{source}) can't be equal."
139
120
  end
140
121
 
141
- if options[TYPE_SLACK] && !@slack_webhook
142
- raise ArgumentError, "ERROR: Can't find any slack webhook. Set slack webhook using `ballantine init`."
122
+ if options[TYPE_SLACK] && !conf.get_data(Config::KEY_SLACK_WEBHOOK)
123
+ raise NotAllowed, "ERROR: Can't find any slack webhook. Set slack webhook using `ballantine config --#{Config::ENV_LOCAL} slack_webhook [YOUR_WEBHOOK]'."
143
124
  end
144
125
 
145
126
  nil
146
127
  end
147
128
 
129
+ # @param [Hash] options
130
+ # @return [Boolean]
131
+ def init_variables(**options)
132
+ @send_type = options[TYPE_SLACK] ? TYPE_SLACK : TYPE_TERMINAL
133
+ @app_name = File.basename(%x(git config --get remote.origin.url).chomp, ".git")
134
+ @main_path = Dir.pwd
135
+ @sub_path =
136
+ if Dir[FILE_GITMODULES].any?
137
+ file = File.open(FILE_GITMODULES)
138
+ lines = file.readlines.map(&:chomp)
139
+ file.close
140
+ lines.grep(/path =/).map { |line| line[/(?<=path \=).*/, 0].strip }.sort
141
+ else
142
+ []
143
+ end
144
+ true
145
+ end
146
+
148
147
  # @param [String] name
149
148
  # @return [String] hash
150
149
  def check_tag(name)
151
- list = `git tag -l`.split("\n")
150
+ list = %x(git tag -l).split("\n")
152
151
  return name unless list.grep(name).any?
153
152
 
154
- system "git fetch origin tag #{name} -f &> /dev/null"
155
- `git rev-list -n 1 #{name}`.chomp[0...7]
153
+ system("git fetch origin tag #{name} -f &> /dev/null")
154
+ %x(git rev-list -n 1 #{name}).chomp[0...7]
156
155
  end
157
156
 
158
157
  # @param [String] from
@@ -160,13 +159,17 @@ module Ballantine
160
159
  # @param [String] url
161
160
  # @return [NilClass] nil
162
161
  def check_commits(from, to, url)
163
- repo = File.basename(`git config --get remote.origin.url`.chomp, '.git')
164
- names = `git --no-pager log --pretty=format:"%an" #{from}..#{to}`.split("\n").uniq.sort
165
- authors = names.map{ |name| Author.find_or_create_by(name) }
162
+ repo = File.basename(%x(git config --get remote.origin.url).chomp, ".git")
163
+ names = %x(git --no-pager log --pretty=format:"%an" #{from}..#{to}).split("\n").uniq.sort
164
+ authors = names.map { |name| Author.find_or_create_by(name) }
166
165
  authors.each do |author|
167
166
  format = commit_format(url, ljust: DEFAULT_LJUST - 10)
168
- commits = `git --no-pager log --reverse --no-merges --author="#{author.name}" --format="#{format}" --abbrev=7 #{from}..#{to}`.gsub('"', '\"').gsub(/[\u0080-\u00ff]/, '')
167
+ commits =
168
+ %x(git --no-pager log --reverse --no-merges --author="#{author.name}" --format="#{format}" --abbrev=7 #{from}..#{to})
169
+ .gsub('"', '\"')
170
+ .gsub(/[\u0080-\u00ff]/, "")
169
171
  next if commits.empty?
172
+
170
173
  author.commits[repo] = commits.split("\n")
171
174
  end
172
175
  nil
@@ -185,17 +188,20 @@ module Ballantine
185
188
  end
186
189
 
187
190
  # @param [String] hash
188
- # @param [Array<String>] sub_path
189
191
  # @return [Array(String, Array<String>)] main, sub's hash
190
192
  def commit_hash(hash)
191
- system "git checkout #{hash} -f &> /dev/null"
192
- system 'git pull &> /dev/null'
193
- main_hash = `git --no-pager log -1 --format='%h'`.chomp
194
- sub_hash = if @sub_path.any?
195
- `git ls-tree HEAD #{@sub_path.join(' ')}`.split("\n").map{ |line| line.split(' ')[2] }
196
- else
197
- []
198
- end
193
+ # check argument is tag
194
+ hash = check_tag(hash)
195
+
196
+ system("git checkout #{hash} -f &> /dev/null")
197
+ system("git pull &> /dev/null")
198
+ main_hash = %x(git --no-pager log -1 --format='%h').chomp
199
+ sub_hash =
200
+ if sub_path.any?
201
+ %x(git ls-tree HEAD #{@sub_path.join(" ")}).split("\n").map { |line| line.split(" ")[2] }
202
+ else
203
+ []
204
+ end
199
205
 
200
206
  [main_hash, sub_hash]
201
207
  end
@@ -204,52 +210,58 @@ module Ballantine
204
210
  # @param [String] format
205
211
  # @param [Integer] ljust
206
212
  def commit_format(url, ljust: DEFAULT_LJUST)
207
- case @send_type
213
+ case send_type
208
214
  when TYPE_TERMINAL
209
- " - "+ "%h".yellow + " %<(#{ljust})%s " + "#{url}/commit/%H".gray
215
+ " - " + "%h".yellow + " %<(#{ljust})%s " + "#{url}/commit/%H".gray
210
216
  when TYPE_SLACK
211
217
  "\\\`<#{url}/commit/%H|%h>\\\` %s - %an"
218
+ else raise AssertionFailed, "Unknown send type: #{send_type}"
212
219
  end
213
220
  end
214
221
 
222
+ # @param [String] target
223
+ # @param [String] source
215
224
  # @param [String] from
216
225
  # @param [String] to
217
226
  # @param [String] url
218
227
  # @return [NilClass] nil
219
- def send_commits(from, to, url)
228
+ def send_commits(target, source, from, to, url)
220
229
  authors = Author.all
221
230
  if authors.empty?
222
- raise ArgumentError, "ERROR: There is no commits between \"#{from}\" and \"#{to}\""
231
+ raise ArgumentError, "ERROR: There is no commits between \"#{target}\" and \"#{source}\""
223
232
  end
233
+
224
234
  number = authors.size
225
- last_commit = `git --no-pager log --reverse --format="#{commit_format(url, ljust: DEFAULT_LJUST - 22)}" --abbrev=7 #{from}..#{to} -1`.strip
235
+ last_commit = %x(git --no-pager log --reverse --format="#{commit_format(url, ljust: DEFAULT_LJUST - 22)}" --abbrev=7 #{from}..#{to} -1).strip
226
236
 
227
- case @send_type
237
+ case send_type
228
238
  when TYPE_TERMINAL
229
- puts "Check commits before #{@app_name.red} deployment. (#{from.cyan} <- #{to.cyan})".ljust(DEFAULT_LJUST + 34) + " #{url}/compare/#{from}...#{to}".gray
239
+ puts "Check commits before #{app_name.red} deployment. (#{target.cyan} <- #{source.cyan})".ljust(DEFAULT_LJUST + 34) + " #{url}/compare/#{from}...#{to}".gray
230
240
  puts "Author".yellow + ": #{number}"
231
241
  puts "Last commit".blue + ": #{last_commit}"
232
242
  authors.map(&:print_commits)
233
243
  when TYPE_SLACK
234
244
  # set message for each author
235
245
  messages = authors.map(&:serialize_commits)
236
- actor = `git config user.name`
246
+ actor = %x(git config user.name).chomp
237
247
 
238
248
  # send message to slack
239
- require 'net/http'
240
- require 'uri'
241
- uri = URI.parse(@slack_webhook)
249
+ require "net/http"
250
+ require "uri"
251
+ uri = URI.parse(conf.get_data(Config::KEY_SLACK_WEBHOOK))
242
252
  request = Net::HTTP::Post.new(uri)
243
- request.content_type = 'application/json'
253
+ request.content_type = "application/json"
244
254
  request.body = JSON.dump({
245
- 'text' => ":check: *#{@app_name}* deployment request by <@#{actor}> (\`<#{url}/tree/#{from}|#{from}>\` <- \`<#{url}/tree/#{to}|#{to}>\` <#{url}/compare/#{from}...#{to}|compare>)\n:technologist: Author: #{number}\nLast commit: #{last_commit}",
246
- 'attachments' => messages
255
+ "text" => ":white_check_mark: *#{app_name}* deployment request by <@#{actor}> (\`<#{url}/tree/#{from}|#{target}>\` <- \`<#{url}/tree/#{to}|#{source}>\` <#{url}/compare/#{from}...#{to}|compare>)\n:technologist: Author: #{number}\nLast commit: #{last_commit}",
256
+ "attachments" => messages,
247
257
  })
248
- req_options = { use_ssl: uri.scheme == 'https' }
258
+ req_options = { use_ssl: uri.scheme == "https" }
249
259
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
250
260
  http.request(request)
251
261
  end
252
262
  puts response.message
263
+ else
264
+ raise AssertionFailed, "Unknown send type: #{send_type}"
253
265
  end
254
266
  end
255
267
  end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ballantine
4
+ class Config
5
+ ENV_LOCAL = "local"
6
+ ENV_GLOBAL = "global"
7
+ AVAILABLE_ENVIRONMENTS = [
8
+ ENV_LOCAL,
9
+ ENV_GLOBAL,
10
+ ].freeze
11
+
12
+ KEY_SLACK_WEBHOOK = "slack_webhook"
13
+ AVAILABLE_KEYS = [
14
+ KEY_SLACK_WEBHOOK,
15
+ ].freeze
16
+
17
+ FILE_BALLANTINE_CONFIG = ".ballantine.json"
18
+
19
+ attr_reader :env, :data, :loaded
20
+
21
+ def initialize(env)
22
+ @env = env || ENV_LOCAL
23
+ @data = {}
24
+ @loaded = false
25
+ end
26
+
27
+ # @param [Hash] options
28
+ # @return [Boolean] result
29
+ def init_file(**options)
30
+ raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && !options[:force]
31
+
32
+ File.write(file_path, {})
33
+ @loaded = false
34
+ end
35
+
36
+ # @param [Hash] options
37
+ # @return [Boolean] result
38
+ def load_file(**options)
39
+ return false if @loaded
40
+ raise NotAllowed, "Can't find #{FILE_BALLANTINE_CONFIG}" if Dir[file_path].empty?
41
+
42
+ JSON.parse(File.read(file_path)).each do |key, value|
43
+ next unless AVAILABLE_KEYS.include?(key)
44
+
45
+ @data[key] = value
46
+ end
47
+
48
+ @loaded = true
49
+ end
50
+
51
+ # @param [String] key
52
+ # @param [Hash] options
53
+ # @return [Boolean] result
54
+ def print_data(key, **options)
55
+ load_file unless @loaded
56
+
57
+ if key
58
+ raise InvalidParameter, "Key must be within #{AVAILABLE_KEYS}" unless AVAILABLE_KEYS.include?(key)
59
+
60
+ puts @data[key]
61
+ else
62
+ @data.each do |key, value|
63
+ puts "#{key}: #{value}"
64
+ end
65
+ end
66
+
67
+ true
68
+ end
69
+
70
+ # @param [String] key
71
+ # @param [String] value
72
+ # @param [Hash] options
73
+ # @return [Stirng] value
74
+ def set_data(key, value, **options)
75
+ load_file unless @loaded
76
+
77
+ @data[key] = value
78
+ File.write(file_path, JSON.dump(@data))
79
+ value
80
+ end
81
+
82
+ # @param [String] key
83
+ # @param [Hash] options
84
+ # @return [Stirng] value
85
+ def get_data(key, **options)
86
+ load_file unless @loaded
87
+ @data[key]
88
+ end
89
+
90
+ private
91
+
92
+ def file_path(env = @env)
93
+ case env
94
+ when ENV_LOCAL then "./#{FILE_BALLANTINE_CONFIG}"
95
+ when ENV_GLOBAL then "#{Dir.home}/#{FILE_BALLANTINE_CONFIG}"
96
+ else raise AssertionFailed, "Unknown environment: #{env}"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ballantine
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4-beta"
5
5
  end
data/lib/ballantine.rb CHANGED
@@ -1,7 +1,17 @@
1
1
  # frozen_string_literal: true
2
- require_relative "ballantine/cli"
2
+
3
+ require "thor"
4
+ require "json"
5
+ require_relative "string"
3
6
  require_relative "ballantine/version"
7
+ require_relative "ballantine/config"
8
+ require_relative "ballantine/author"
9
+ require_relative "ballantine/cli"
4
10
 
5
11
  module Ballantine
6
12
  class Error < StandardError; end
13
+ class NotAllowed < Error; end
14
+ class InvalidParameter < Error; end
15
+ class AssertionFailed < Error; end
16
+ class NotImplemented < Error; end
7
17
  end
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ # frozen_string_literal: true
2
2
 
3
3
  # @override
4
4
  class String
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ballantine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4.pre.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - oohyun15
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-15 00:00:00.000000000 Z
11
+ date: 2023-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -51,13 +51,14 @@ files:
51
51
  - lib/ballantine.rb
52
52
  - lib/ballantine/author.rb
53
53
  - lib/ballantine/cli.rb
54
- - lib/ballantine/string.rb
54
+ - lib/ballantine/config.rb
55
55
  - lib/ballantine/version.rb
56
+ - lib/string.rb
56
57
  homepage: https://github.com/oohyun15/ballantine
57
58
  licenses:
58
59
  - MIT
59
60
  metadata: {}
60
- post_install_message:
61
+ post_install_message:
61
62
  rdoc_options: []
62
63
  require_paths:
63
64
  - lib
@@ -65,15 +66,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
66
  requirements:
66
67
  - - ">="
67
68
  - !ruby/object:Gem::Version
68
- version: 2.7.6
69
+ version: '3.1'
69
70
  required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  requirements:
71
- - - ">="
72
+ - - ">"
72
73
  - !ruby/object:Gem::Version
73
- version: '0'
74
+ version: 1.3.1
74
75
  requirements: []
75
- rubygems_version: 3.3.7
76
- signing_key:
76
+ rubygems_version: 3.3.26
77
+ signing_key:
77
78
  specification_version: 4
78
79
  summary: Describe your commits.
79
80
  test_files: []