brightpearl-cli 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/core/tools.rb CHANGED
@@ -7,15 +7,27 @@ module Brightpearl
7
7
  REPORT_WIDTH = 188
8
8
 
9
9
  # Check for internet access. Dies if not connected.
10
- # @return boolean|void
10
+ # @return void
11
11
  def self.verify_internet_access
12
- begin
13
- return true if open('http://www.google.com')
14
- rescue
15
- abort('No internet connection. You must be online to run this command.')
12
+ if ping('www.google.com') != 0
13
+ Brightpearl::Terminal::error('You are not connected to the internet', 'Please check your connection and try and again.')
14
+ end
15
+ end
16
+
17
+ def self.verify_vm_is_reachable
18
+ if ping(Brightpearl::Config.param(Brightpearl::Config::VM_IP)) != 0
19
+ Brightpearl::Terminal::error('Cannot reach VM', ["The remote host #{Brightpearl::Terminal::format_highlight(Brightpearl::Config.param(Brightpearl::Config::VM_IP))} cannot be reached.", 'Please make sure your VM is online and correctly configured.'])
16
20
  end
17
21
  end
18
22
 
23
+ # Ping a URL or IP and returns the exit status. 0 = success, anything else means it failed.
24
+ # @return Integer
25
+ def self.ping(ip_or_url, verbose = true)
26
+ Brightpearl::Terminal::output("Checking that #{Brightpearl::Terminal::format_highlight(ip_or_url)} is reachable") if verbose == true
27
+ `ping -t 1 -c 1 #{ip_or_url}`
28
+ $?.exitstatus
29
+ end
30
+
19
31
  # Returns time ago in human readable format.
20
32
  # @string
21
33
  def self.time_passed_since(time_stamp)
@@ -50,14 +62,6 @@ module Brightpearl
50
62
  end
51
63
  end
52
64
 
53
- # Returns the number of seconds that have elapsed between a timestamp (0000-00-00T00:00:00+00:00) and now
54
- # @return integer
55
- def self.seconds_since(time_stamp)
56
- time_stamp = DateTime.strptime(time_stamp, '%Y-%m-%dT%H:%M:%S%z')
57
- time_now = DateTime.now
58
- ((time_now - time_stamp) * 24 * 60 * 60).to_i
59
- end
60
-
61
65
  # Used to make sure all reports are the same width (for consistency).
62
66
  # @return void
63
67
  def self.validate_report_width(array)
@@ -73,6 +77,22 @@ module Brightpearl
73
77
  end
74
78
  end
75
79
 
80
+ # Returns TRUE if Mac, FALSE if Linux (or anything else for that matter)
81
+ # @return boolean
82
+ def self.this_is_a_mac
83
+ return Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_OS) == Brightpearl::Config::MAC
84
+ end
85
+
86
+ private
87
+
88
+ # Returns the number of seconds that have elapsed between a timestamp (0000-00-00T00:00:00+00:00) and now
89
+ # @return integer
90
+ def self.seconds_since(time_stamp)
91
+ time_stamp = DateTime.strptime(time_stamp, '%Y-%m-%dT%H:%M:%S%z')
92
+ time_now = DateTime.now
93
+ ((time_now - time_stamp) * 24 * 60 * 60).to_i
94
+ end
95
+
76
96
  end
77
97
 
78
98
  end
data/lib/routes/build.rb CHANGED
@@ -7,6 +7,7 @@ module BrightpearlCommand
7
7
  @opts = command_options
8
8
  @args = arguments
9
9
  @service_dir = nil
10
+ @is_lib = false
10
11
  opts_validate
11
12
  opts_routing
12
13
 
@@ -18,11 +19,24 @@ module BrightpearlCommand
18
19
  Brightpearl::Terminal::error('Must specify service name', "The script cannot build your service because you haven't told it what service to build.", true)
19
20
  end
20
21
 
22
+ # Set 'service only' to default.
23
+ if !@opts[:serviceOnly] && !@opts[:apiOnly] && !@opts[:both]
24
+ @opts[:serviceOnly] = true
25
+ end
26
+
27
+ if @options[:serviceOnly]
28
+ suffix = "/#{@args[0]}-service"
29
+ elsif @opts[:apiOnly]
30
+ suffix = "/#{@args[0]}-api"
31
+ else
32
+ suffix = ''
33
+ end
34
+
21
35
  service_dir = Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_PATH_TO_BP_CODE)
22
- dir_dev_support = "#{service_dir}/services/dev-support/#{@args[0]}/#{@args[0]}-service"
23
- dir_functional = "#{service_dir}/services/functional/#{@args[0]}/#{@args[0]}-service"
24
- dir_infrastructure = "#{service_dir}/services/infrastructure/#{@args[0]}/#{@args[0]}-service"
25
- dir_integration = "#{service_dir}/services/integration/#{@args[0]}/#{@args[0]}-service"
36
+ dir_dev_support = "#{service_dir}/services/dev-support/#{@args[0]}#{suffix}"
37
+ dir_functional = "#{service_dir}/services/functional/#{@args[0]}#{suffix}"
38
+ dir_infrastructure = "#{service_dir}/services/infrastructure/#{@args[0]}#{suffix}"
39
+ dir_integration = "#{service_dir}/services/integration/#{@args[0]}#{suffix}"
26
40
  dir_lib = "#{service_dir}/services/lib/#{@args[0]}"
27
41
 
28
42
  if File.directory?(dir_dev_support)
@@ -39,9 +53,10 @@ module BrightpearlCommand
39
53
  end
40
54
  if File.directory?(dir_lib)
41
55
  @service_dir = dir_lib
56
+ @is_lib = true
42
57
  end
43
58
  if @service_dir.nil?
44
- Brightpearl::Terminal::error('Cannot find service', "The script is unable to find a service called: \x1B[38;5;202m#{@args[0].downcase.capitalize}\x1B[0m\nPlease check your spelling and try again.")
59
+ Brightpearl::Terminal::error('Cannot find service', "The script is unable to find a service called: \x1B[38;5;202m#{@args[0].downcase}\x1B[0m\nPlease check your spelling and try again.")
45
60
  end
46
61
 
47
62
  end
@@ -54,26 +69,84 @@ module BrightpearlCommand
54
69
 
55
70
  def build_service
56
71
 
57
- commands = [
58
- # 'git status'
59
- 'mvn clean install | grep "Building war"',
60
- # 'mvn deploy'
61
- ]
62
- output = Brightpearl::Terminal::command_capture(commands, @service_dir)
72
+ Brightpearl::Tools::verify_vm_is_reachable if @is_lib == false
73
+
74
+ if Brightpearl::Tools::this_is_a_mac
75
+
76
+ args = @args.dup
77
+ args.shift
78
+
79
+ commands = [
80
+ "mvn clean install#{@opts[:deploy] ? ' deploy' : ''}#{args.any? ? " #{args.join(' ')}" : ''}",
81
+ ]
82
+ if @is_lib == false
83
+
84
+ output = Brightpearl::Terminal::command_capture(commands, @service_dir)
85
+
86
+ lines = output[0].chomp
87
+ lines = lines.split("\n")
88
+
89
+ war_file_ws = nil
90
+ build_failed = false
91
+ build_fail_url = nil
63
92
 
64
- puts output
93
+ lines.each do |line|
94
+ if line =~ /\A\[INFO\] Building war: (.*).war\z/
95
+ war_file_ws = line
96
+ end
97
+ if line =~ /\A\[INFO\] BUILD FAILURE\z/
98
+ build_failed = true
99
+ end
100
+ if line =~ /\[ERROR\] Please refer to (.*)surefire-reports/
101
+ build_fail_url = line
102
+ build_fail_url = build_fail_url.split('Please refer to ')
103
+ build_fail_url = build_fail_url[1].split(' for the individual')
104
+ build_fail_url = build_fail_url[0]
105
+ end
106
+ end
65
107
 
66
- war_file = output[0].split('war: ')
67
- war_file = war_file[1].chomp
108
+ if build_failed
109
+ Brightpearl::Terminal::error('Build failed', nil, false)
110
+ puts output[0]
111
+ puts
68
112
 
69
- puts war_file
113
+ # If a build fail URL was found, launch it in default browser.
114
+ unless build_fail_url.nil?
115
+ if Brightpearl::Terminal::prompt_yes_no("Would you like to #{Brightpearl::Terminal::format_action('open a stack-trace')} in your default browser?", build_fail_url)
116
+ system("open -a Google\\ Chrome #{build_fail_url}")
117
+ end
118
+ end
119
+
120
+ exit
121
+ end
122
+
123
+ war_file_ws = war_file_ws.split('war: ')
124
+ war_file_ws = war_file_ws[1].chomp
125
+
126
+ war_file_vm = war_file_ws.split('/')
127
+ war_file_vm = war_file_vm[war_file_vm.length - 1]
128
+
129
+
130
+ commands = [
131
+ "scp #{war_file_ws} #{Brightpearl::Config.param(Brightpearl::Config::VM_USER)}@#{Brightpearl::Config.param(Brightpearl::Config::VM_IP)}:/tmp",
132
+ "sshpass -p#{Brightpearl::Config.param(Brightpearl::Config::VM_USER_PASSWORD)} ssh #{Brightpearl::Config.param(Brightpearl::Config::VM_USER)}@#{Brightpearl::Config.param(Brightpearl::Config::VM_IP)} 'cd /brightpearl-source/dev-vm-control-scripts/scm && ~/generated-scripts/deploy-uploaded-service #{war_file_vm}'"
133
+
134
+ ]
135
+ Brightpearl::Terminal::command(commands)
136
+
137
+ else
138
+
139
+ Brightpearl::Terminal::command(commands, @service_dir)
140
+
141
+ end
142
+
143
+ else
70
144
 
71
- if Brightpearl::Config.param(Brightpearl::Config::WORKSTATION_OS) == Brightpearl::Config::MAC
72
145
  commands = [
73
- "scp #{war_file} #{Brightpearl::Config.param(Brightpearl::Config::VM_USER)}@#{Brightpearl::Config.param(Brightpearl::Config::VM_IP)}:/tmp"
146
+ 'mvn clean install -P deploy-service',
74
147
  ]
75
- puts commands
76
- Brightpearl::Terminal::command(commands)
148
+ Brightpearl::Terminal::command(commands, @service_dir)
149
+
77
150
  end
78
151
 
79
152
  end
@@ -7,6 +7,7 @@ module BrightpearlCommand
7
7
  @opts = command_options
8
8
  @args = arguments
9
9
  @git = Brightpearl::Git.new
10
+
10
11
  opts_validate
11
12
  opts_routing
12
13
 
@@ -16,7 +17,7 @@ module BrightpearlCommand
16
17
 
17
18
  unless @args.any?
18
19
  if @opts[:branch] || @opts[:branch_origin]
19
- puts 'You must specify a branch name.'
20
+ Brightpearl::Terminal::error('You must specify a branch name')
20
21
  else
21
22
  puts "\n\x1B[41m ERROR \x1B[0m You must specify a branch to checkout. Here is a list of your \x1B[35mLOCAL BRANCHES\x1B[0m"
22
23
  Brightpearl::Git.new.show_branches(Brightpearl::Git::SORT_REFNAME, Brightpearl::Git::LOCAL)
@@ -45,20 +46,15 @@ module BrightpearlCommand
45
46
  new_branch_name = @args[0]
46
47
  source_branch_name = @args[1].nil? ? Brightpearl::Git::MASTER : @args[1]
47
48
 
48
- Brightpearl::Terminal::info("Checking that branch #{Brightpearl::Terminal::format_branch(new_branch_name)} doesn't already exist")
49
- if @git.branch_exists_anywhere(new_branch_name)
50
- Brightpearl::Terminal::error('Target branch already exists', "Cannot create branch because a branch named #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m is already in use somewhere.", true)
51
- end
49
+ # Validate that new branch doesn't already exist.
50
+ @git.check_branch_does_not_exist_anywhere(new_branch_name)
52
51
 
53
52
  # Validate that source branch (if not MASTER) exists locally.
54
53
  if source_branch_name != Brightpearl::Git::MASTER
55
- # Check branch exists anywhere.
56
- unless @git.branch_exists_anywhere(source_branch_name)
57
- Brightpearl::Terminal::error("Source branch doesn't exist", "Cannot cut from branch #{Brightpearl::Terminal::format_branch(source_branch_name)}\x1B[38;5;240m because it doesn't exist.", true)
58
- end
54
+ @git.check_branch_exists_somewhere(source_branch_name)
59
55
  end
60
56
 
61
- @git.stash_staged_changes
57
+ @git.check_for_uncommitted_files
62
58
  if Brightpearl::Terminal::prompt_yes_no('Create new branch?', "You're about to cut branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m from #{Brightpearl::Terminal::format_branch(source_branch_name)}\x1B[38;5;240m#{push_to_origin ? ' and push it to origin.' : ''}")
63
59
  source_branch_final = []
64
60
  source_branch_data = @git.branch_data(source_branch_name)
@@ -91,7 +87,7 @@ module BrightpearlCommand
91
87
  else
92
88
  source_branch_final = "#{source_branch_final[0]}/#{source_branch_final[1]}"
93
89
  end
94
- Brightpearl::Terminal::success('Branch created!', "Branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m has been successfully cut from #{Brightpearl::Terminal::format_branch(source_branch_final)}")
90
+ Brightpearl::Terminal::success('Branch created', "Branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m has been successfully cut from #{Brightpearl::Terminal::format_branch(source_branch_final)}")
95
91
  else
96
92
  Brightpearl::Terminal::abort(nil, nil, true, false)
97
93
  end
@@ -101,10 +97,6 @@ module BrightpearlCommand
101
97
  # Checks out a branch for both /code & /db.
102
98
  # @return void
103
99
  def checkout_branch
104
- # todo
105
- puts
106
- puts "\x1B[31mTODO!\x1B[0m Must alert user that if DB branch doesn't exist, A) Create it? and B) If not, inform you are on 2 different branches."
107
- puts
108
100
  branch_to_checkout = @args[0]
109
101
  branch_data = @git.branch_data(branch_to_checkout)
110
102
  # If branch is already checked out.
@@ -116,13 +108,14 @@ module BrightpearlCommand
116
108
  @git.check_for_stash
117
109
  exit
118
110
  end
119
- @git.stash_staged_changes
120
111
  # If branch doesn't exist.
121
112
  if branch_data[0][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false && branch_data[1][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
122
- Brightpearl::Terminal::error("The branch you're trying to checkout #{Brightpearl::Terminal::format_branch(branch_to_checkout)} doesn't exist", 'Please check your spelling and try again.', true, false)
113
+ Brightpearl::Terminal::error("The branch you're trying to checkout #{Brightpearl::Terminal::format_branch(branch_to_checkout)} doesn't exist", 'Please check your spelling and try again.', true)
123
114
  end
115
+ @git.check_for_uncommitted_files
124
116
  count = 0
125
117
  branch_data.each do |branch_data_inner|
118
+ repo_dir = branch_data_inner[:"#{Brightpearl::Git::BRANCH_LOCATION}"]
126
119
  if branch_data_inner[:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
127
120
  next
128
121
  end
@@ -138,16 +131,17 @@ module BrightpearlCommand
138
131
  if branch_data[count][:"#{Brightpearl::Git::BRANCH_HAS_UPSTREAM}"]
139
132
  commands << 'git pull'
140
133
  else
141
- Brightpearl::Terminal::warning('Branch has no upstream', ["You can push to origin by running: #{Brightpearl::Terminal::format_command("git push --set-upstream origin #{branch_data_inner[:"#{Brightpearl::Git::BRANCH_NAME}"]}")}"])
134
+ @git.ask_to_setup_remote_tracking(@git.current_branch_for_repo(repo_dir), repo_dir)
142
135
  end
143
136
  commands << "git merge #{Brightpearl::Git::MASTER}"
144
137
  end
145
138
  if @opts[:updatePush]
146
139
  commands << 'git push'
147
140
  end
148
- Brightpearl::Terminal::command(commands, branch_data_inner[:"#{Brightpearl::Git::BRANCH_LOCATION}"])
141
+ Brightpearl::Terminal::command(commands, repo_dir)
149
142
  count = count + 1
150
143
  end
144
+ @git.check_for_same_branch
151
145
  @git.check_for_stash
152
146
  end
153
147
  end
@@ -46,11 +46,11 @@ module BrightpearlCommand
46
46
  end
47
47
 
48
48
  def delete_local
49
- # todo
50
- puts
51
- puts "\x1B[31mTODO!\x1B[0m Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!"
52
- puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
53
- Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mLOCALLY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
49
+ Brightpearl::Terminal::output("Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!", Brightpearl::Terminal::MSG_TODO)
50
+ Brightpearl::Terminal::output('Must check branch even exists.', Brightpearl::Terminal::MSG_TODO)
51
+ unless Brightpearl::Terminal::prompt_yes_no("#{Brightpearl::Terminal::format_action('locally delete')} branch #{Brightpearl::Terminal::format_branch(@args[0])}?")
52
+ Brightpearl::Terminal::abort
53
+ end
54
54
  @git.repo_loop.each do |repo_dir|
55
55
  commands = []
56
56
  if @git.current_branch_for_repo(repo_dir) == @args[0]
@@ -59,14 +59,14 @@ module BrightpearlCommand
59
59
  commands << "git branch -d #{@args[0]}"
60
60
  Brightpearl::Terminal::command(commands, repo_dir)
61
61
  end
62
- puts "\nSuccessfully \x1B[35mLOCALLY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
62
+ Brightpearl::Terminal::success('Branch deleted', "Successfully #{Brightpearl::Terminal::format_highlight('locally')} #{Brightpearl::Terminal::format_action('deleted')} branch #{Brightpearl::Terminal::format_branch(@args[0])}")
63
63
  end
64
64
 
65
65
  def delete_remote
66
- # todo
67
- puts
68
- puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
69
- Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mREMOTELY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
66
+ Brightpearl::Terminal::output('Must check branch even exists.', Brightpearl::Terminal::MSG_TODO)
67
+ unless Brightpearl::Terminal::prompt_yes_no("#{Brightpearl::Terminal::format_action('remotely delete')} branch #{Brightpearl::Terminal::format_branch(@args[0])}?")
68
+ Brightpearl::Terminal::abort
69
+ end
70
70
  @git.repo_loop.each do |repo_dir|
71
71
  unless @git.branch_exists(@args[0], repo_dir, Brightpearl::Git::REMOTE)
72
72
  puts "Branch \x1B[32m[#{@args[0]}]\x1B[0m not found in: \x1B[33m#{repo_dir}\x1B[0m. Aborting \x1B[35mREMOTE DELETE\x1B[0m for this repo."
@@ -78,41 +78,26 @@ module BrightpearlCommand
78
78
  ]
79
79
  Brightpearl::Terminal::command(commands, repo_dir)
80
80
  end
81
-
82
- puts "\nSuccessfully \x1B[35mREMOTELY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
83
- new_branch_name = @args[0].split('-')
84
- new_branch_name = "MERGED-#{new_branch_name[new_branch_name.count - 1]}"
85
- Brightpearl::Terminal::prompt_yes("Would you like to \x1B[35mRENAME\x1B[0m \x1B[32m[#{@args[0]}]\x1B[0m to \x1B[32m[#{new_branch_name}]\x1B[0m")
86
-
87
- @git.repo_loop.each do |repo_dir|
88
- unless @git.branch_exists(@args[0], repo_dir, Brightpearl::Git::LOCAL)
89
- puts "Branch \x1B[32m[#{@args[0]}]\x1B[0m not found in: \x1B[33m#{repo_dir}\x1B[0m. Aborting \x1B[35mRENAME\x1B[0m for this repo."
90
- next
91
- end
92
- commands = [
93
- "git branch -m #{@args[0]} #{new_branch_name}",
94
- ]
95
- Brightpearl::Terminal::command(commands, repo_dir)
96
- end
97
- puts
81
+ Brightpearl::Terminal::success('Branch deleted', "Successfully #{Brightpearl::Terminal::format_highlight('remotely')} #{Brightpearl::Terminal::format_action('deleted')} branch #{Brightpearl::Terminal::format_branch(@args[0])}")
98
82
  end
99
83
 
100
84
  def delete_both
101
- # todo
102
- puts
103
- puts "\x1B[31mTODO!\x1B[0m Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!"
104
- puts "\x1B[31mTODO!\x1B[0m Must check branch even exists."
105
- Brightpearl::Terminal::prompt_yes("You're about to \x1B[35mLOCALLY & REMOTELY DELETE\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m")
85
+ Brightpearl::Terminal::output("Must check that branch is up-to-date with it's remote counter-part, otherwise DO NOT delete!", Brightpearl::Terminal::MSG_TODO)
86
+ Brightpearl::Terminal::output('Must check branch even exists.', Brightpearl::Terminal::MSG_TODO)
87
+ unless Brightpearl::Terminal::prompt_yes_no("#{Brightpearl::Terminal::format_action('locally & remotely delete')} branch #{Brightpearl::Terminal::format_branch(@args[0])}?")
88
+ Brightpearl::Terminal::abort
89
+ end
106
90
  @git.repo_loop.each do |repo_dir|
107
91
  commands = []
108
92
  if @git.current_branch_for_repo(repo_dir) == @args[0]
109
93
  commands << 'git checkout master'
110
94
  end
111
95
  commands << "git branch -d #{@args[0]}"
96
+ commands << "git branch --unset-upstream #{@args[0]}"
112
97
  commands << "git push origin --delete #{@args[0]}"
113
98
  Brightpearl::Terminal::command(commands, repo_dir)
114
99
  end
115
- puts "\nSuccessfully \x1B[35mLOCALLY & REMOTELY DELETED\x1B[0m branch \x1B[32m[#{@args[0]}]\x1B[0m\n\n"
100
+ Brightpearl::Terminal::success('Branch deleted', "Successfully #{Brightpearl::Terminal::format_highlight('locally')} and #{Brightpearl::Terminal::format_highlight('remotely')} #{Brightpearl::Terminal::format_action('deleted')} branch #{Brightpearl::Terminal::format_branch(@args[0])}")
116
101
  end
117
102
 
118
103
  end