ora-cli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,96 @@
1
+ module Ora::Cli
2
+ class Bash
3
+ def initialize(target, from: nil, print: Print.new)
4
+ @target = target
5
+ @from = from
6
+ @print = print
7
+ end
8
+
9
+ def silent command
10
+ `#{move}#{command}#{capture_err}`
11
+ end
12
+
13
+ def run commands
14
+ unprocessed_commands = extract commands
15
+
16
+ outputs = []
17
+ while (command = complete unprocessed_commands.shift)
18
+ break unless call command do |output|
19
+ outputs.push output
20
+ end
21
+ end
22
+
23
+ handle_failed unprocessed_commands
24
+
25
+ join outputs
26
+ end
27
+
28
+ private
29
+ def move
30
+ "cd #{@from} && " if @from
31
+ end
32
+ def capture_err
33
+ " 2>&1"
34
+ end
35
+
36
+ def extract commands
37
+ commands.split("\n").map(&:strip).reject(&:empty?)
38
+ end
39
+ def join outputs
40
+ outputs.compact.map(&:strip).reject(&:empty?).join("\n")
41
+ end
42
+
43
+ def call_target method_name
44
+ @target.method(method_name).call
45
+ end
46
+
47
+ def complete unprocessed_command
48
+ return nil unless unprocessed_command
49
+
50
+ unprocessed_command.gsub(/#\{([\S]+)\}/) do
51
+ call_target Regexp.last_match[1]
52
+ end
53
+ end
54
+
55
+ def call command
56
+ @print.green command
57
+
58
+ success =
59
+ if method? command
60
+ call_method command
61
+ else
62
+ yield(shell command)
63
+ $?.success?
64
+ end
65
+
66
+ alert command unless success
67
+ success
68
+ end
69
+ def method? command
70
+ command.start_with? ":"
71
+ end
72
+ def method_name command
73
+ command.sub(':', '')
74
+ end
75
+ def call_method command
76
+ success_call? call_target(method_name command)
77
+ end
78
+ def success_call? method_return
79
+ method_return != false
80
+ end
81
+ def shell command
82
+ @print.plain `#{move}#{command}#{capture_err}`
83
+ end
84
+
85
+ def alert command
86
+ @print.red "\nProcess Failed! Please resolve the issue above and run commands below manually\n"
87
+ @print.red command
88
+ end
89
+
90
+ def handle_failed unprocessed_commands
91
+ unprocessed_commands.each do |unprocessed_command|
92
+ @print.red(complete unprocessed_command)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,27 @@
1
+ module Ora::Cli
2
+ class Print
3
+ def initialize(silent = false)
4
+ @silent = silent
5
+ end
6
+
7
+ def inline text
8
+ print text unless @silent
9
+ text
10
+ end
11
+
12
+ def plain text
13
+ puts text unless @silent
14
+ text
15
+ end
16
+
17
+ def green text
18
+ puts "\e[32m#{text}\e[0m" unless @silent
19
+ text
20
+ end
21
+
22
+ def red text
23
+ puts "\e[31m#{text}\e[0m" unless @silent
24
+ text
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ module Ora::Cli
2
+ class Stdin
3
+ def initialize(inputs = [], print: Print.new)
4
+ @inputs = inputs
5
+ @print = print
6
+ end
7
+
8
+ def gets(pattern = '')
9
+ input = nil
10
+
11
+ until input
12
+ input = @inputs.shift || STDIN.gets.chomp.strip
13
+ unless input.match(pattern)
14
+ @print.red "Please match #{pattern.inspect}"
15
+ input = nil
16
+ end
17
+ end
18
+
19
+ input
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
1
+ module Ora::Cli
2
+ class Task
3
+ attr_reader :branch, :stdin, :print
4
+
5
+ def initialize(from, inputs: [], print: Print.new)
6
+ @from = from
7
+ @bash = Bash.new(self, from: @from, print: print)
8
+ @branch = current_branch
9
+ @stdin = Stdin.new(inputs, print: print)
10
+ @print = print
11
+ end
12
+
13
+ def run
14
+ @bash.run commands
15
+ end
16
+
17
+ def commands
18
+ raise "Override this method in subclass"
19
+ end
20
+
21
+ private
22
+ def current_branch
23
+ @bash.silent('git branch | grep \\*').sub("*", "").strip
24
+ end
25
+
26
+ def feature_branch!
27
+ if %w{master develop staging uat}.include?(branch)
28
+ @print.red "Please checkout feature branch first!"
29
+ false
30
+ end
31
+ end
32
+
33
+ def clean_branch!
34
+ if dirty?
35
+ print.red "Please clean the feature branch '#{branch}'!"
36
+ return false
37
+ end
38
+ end
39
+ def dirty?
40
+ !@bash.silent('git status').include? 'nothing to commit'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ module Ora::Cli
2
+ class NewFeatureBranch < Task
3
+ attr_reader :branch_name
4
+
5
+ def commands
6
+ '
7
+ :set_branch_name
8
+ :clean_branch!
9
+ git checkout develop
10
+ git pull origin develop
11
+ git checkout -b #{branch_name}
12
+ '
13
+ end
14
+
15
+ private
16
+ def set_branch_name
17
+ print.inline 'Type new branch name: '
18
+ @branch_name = stdin.gets(/^[a-zA-Z0-9\/_-]+$/)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module Ora::Cli
2
+ class PushFeatureBranch < Task
3
+ def commands
4
+ '
5
+ :feature_branch!
6
+ :clean_branch!
7
+ git checkout develop
8
+ git pull origin develop
9
+ git checkout #{branch}
10
+ git merge develop
11
+ git push origin #{branch}
12
+ '
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ module Ora::Cli
2
+ class PushToMaster < Task
3
+ attr_reader :version
4
+
5
+ def commands
6
+ '
7
+ :feature_branch!
8
+ :clean_branch!
9
+ git checkout develop
10
+ git pull origin develop
11
+ git merge #{branch}
12
+ git push origin develop
13
+ git checkout master
14
+ git pull origin master
15
+ git merge develop
16
+ git push origin master
17
+ git fetch --tags
18
+ :set_version
19
+ git checkout #{branch}
20
+ git tag -a "#{version}" -m "#{branch}"
21
+ git push --tags
22
+ :slack_message_to_paste
23
+ '
24
+ end
25
+
26
+ private
27
+ def set_version
28
+ print.plain "Latest versions:"
29
+ print.plain latest_versions
30
+ print.plain "Enter to use #{recommend_version} or type new version:"
31
+ print.inline "New Version: "
32
+ @version = stdin.gets(/^(v[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)?$/)
33
+ @version = recommend_version if @version.empty?
34
+ end
35
+ def latest_versions
36
+ @latest_versions ||=
37
+ @bash.silent('git tag').split("\n").
38
+ select {|tag| tag.match(/^v[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/)}.
39
+ map {|tag| Gem::Version.create(tag.sub(/^v/, ''))}.sort.last(5).
40
+ map {|ver| "v#{ver}"}
41
+ end
42
+ def recommend_version
43
+ @recommend_version ||=
44
+ latest_versions.last.to_s.sub(/\.(\d+)$/, '.') + ($1.to_i + 1).to_s
45
+ end
46
+
47
+ def slack_message_to_paste
48
+ print.plain ":merge: #{branch} => develop\n:merge: develop => master\n:monorail: production"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ module Ora::Cli
2
+ class PushToStaging < Task
3
+
4
+ def commands
5
+ '
6
+ :feature_branch!
7
+ :clean_branch!
8
+ git checkout develop
9
+ git pull origin develop
10
+ git checkout #{branch}
11
+ git merge develop
12
+ git checkout staging
13
+ git pull origin staging
14
+ git merge #{branch}
15
+ git push origin staging
16
+ git checkout #{branch}
17
+ :slack_message_to_paste
18
+ '
19
+ end
20
+
21
+ private
22
+ def slack_message_to_paste
23
+ print.plain ":merge: #{branch} => staging\n:monorail: staging"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module Ora::Cli
2
+ class PushToUat < Task
3
+
4
+ def commands
5
+ '
6
+ :feature_branch!
7
+ :clean_branch!
8
+ git checkout develop
9
+ git pull origin develop
10
+ git checkout #{branch}
11
+ git merge develop
12
+ git checkout uat
13
+ git pull origin uat
14
+ git merge #{branch}
15
+ git push origin uat
16
+ git checkout #{branch}
17
+ :slack_message_to_paste
18
+ '
19
+ end
20
+
21
+ private
22
+ def slack_message_to_paste
23
+ print.plain ":merge: #{branch} => uat\n:monorail: uat"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Ora
2
+ module Cli
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/ora/cli.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Ora
2
+ module Cli
3
+ def self.run task, from
4
+ class_name = task.split('_').map(&:capitalize).join
5
+ Object.const_get("Ora::Cli::#{class_name}").new(from).run
6
+ end
7
+ end
8
+ end
9
+
10
+ Dir[File.dirname(__FILE__) + '/cli/**/*.rb'].each {|file| require file }
data/ora-cli.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ora/cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ora-cli"
8
+ spec.version = Ora::Cli::VERSION
9
+ spec.authors = ["Ducksan Cho"]
10
+ spec.email = ["ducktyper@gmail.com"]
11
+
12
+ spec.summary = "ORA CLI"
13
+ spec.description = "ORA CLI"
14
+ spec.homepage = "https://orahq.com"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ora-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ducksan Cho
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: ORA CLI
56
+ email:
57
+ - ducktyper@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/ora
69
+ - bin/ora_selecta
70
+ - bin/setup
71
+ - lib/ora/cli.rb
72
+ - lib/ora/cli/bash.rb
73
+ - lib/ora/cli/print.rb
74
+ - lib/ora/cli/stdin.rb
75
+ - lib/ora/cli/task.rb
76
+ - lib/ora/cli/tasks/new_feature_branch.rb
77
+ - lib/ora/cli/tasks/push_feature_branch.rb
78
+ - lib/ora/cli/tasks/push_to_master.rb
79
+ - lib/ora/cli/tasks/push_to_staging.rb
80
+ - lib/ora/cli/tasks/push_to_uat.rb
81
+ - lib/ora/cli/version.rb
82
+ - ora-cli.gemspec
83
+ homepage: https://orahq.com
84
+ licenses: []
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: ORA CLI
106
+ test_files: []
107
+ has_rdoc: