gcloud_hosts 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: d2ddcd65fb8f4196960205752361d591aa7ff7f4
4
- data.tar.gz: a37e9aa6c729f372e941149f8797c34598a9432b
3
+ metadata.gz: 67c9cf2078e975e924cf0c7da637214ef0959379
4
+ data.tar.gz: 22e9c351e240c14e1e0ec341bd2dee7f7b66020d
5
5
  SHA512:
6
- metadata.gz: e226856a75147c8c1e1de93f53986b78af452c6db3ad022c7ef3097816cc558eccc5976cb089c4e05ad1fcd7b5b7b7a144a4539a979cad8044d01a70b50785f7
7
- data.tar.gz: 41391e1a31adc2a4c1ab4db9ac8ed57176e5799bfef635ecc58e6bb9580bf2d873011670583a5cd366ff16dc51a779da45c1c8d596989cbdbe36ad6a09b0a75b
6
+ metadata.gz: a97080304ac9a2d7332d9ed2e127478d291e645114b83a7d5b7b9144789d66c692e0c5691169969fe80482f4d5763f8566041f522e09bb6e840d9980c5c37099
7
+ data.tar.gz: 4597b5ba293d1e6b9dff6126f095a230af5a3373332c9cdd37ef026ddbc85374a697b84f7e556c827528fb6de981c1e30213eb0355280b79018f5423881a1b3f
@@ -16,7 +16,8 @@ module GcloudHosts
16
16
  file: '/etc/hosts',
17
17
  backup: nil,
18
18
  dry_run: false,
19
- delete: false
19
+ delete: false,
20
+ clear: false
20
21
  }
21
22
  parser.parse!(args)
22
23
  end
@@ -57,6 +58,9 @@ module GcloudHosts
57
58
  opts.on('--[no-]delete', "Delete the project from hosts file. Defaults to false") do |opt|
58
59
  @options[:delete] = opt
59
60
  end
61
+ opts.on('--[no-]clear', "Clear all gcloud host entries from hosts file. Defaults to false") do |opt|
62
+ @options[:clear] = opt
63
+ end
60
64
  opts.on_tail("--help", "Show this message") do
61
65
  puts opts
62
66
  exit
@@ -8,29 +8,39 @@ module GcloudHosts
8
8
  end
9
9
 
10
10
  def run!
11
- project = @options[:project]
12
- if project.to_s.strip == ""
13
- project = env["core"]["project"]
11
+ project = @options[:project].to_s.strip
12
+ if @options[:clear]
13
+ if project != ""
14
+ raise ArgumentError.new("Cannot specify 'clear' and 'project' at the same time.")
15
+ end
14
16
  end
15
- if project.to_s.strip == ""
16
- raise AuthError.new("No gcloud project specified.")
17
+ if project == ""
18
+ project = env["core"]["project"].to_s.strip
17
19
  end
18
-
19
- if @options[:domain]
20
- domain = @options[:domain].to_s.strip
21
- else
22
- domain = "c.#{project}.internal"
20
+ if project == ""
21
+ raise AuthError.new("No gcloud project specified.")
23
22
  end
24
23
 
25
24
  backup = @options[:backup] ||
26
25
  @options[:file] + '.bak'
27
26
 
28
- if @options[:delete]
29
- new_hosts_list = []
27
+ if @options[:clear]
28
+ Updater.clear(@options[:file], backup, @options[:dry_run])
30
29
  else
31
- new_hosts_list = Hosts.hosts(@options[:gcloud], project, @options[:network], domain, @options[:public], @options[:exclude_public])
30
+ if @options[:domain]
31
+ domain = @options[:domain].to_s.strip
32
+ else
33
+ domain = "c.#{project}.internal"
34
+ end
35
+
36
+
37
+ if @options[:delete]
38
+ new_hosts_list = []
39
+ else
40
+ new_hosts_list = Hosts.hosts(@options[:gcloud], project, @options[:network], domain, @options[:public], @options[:exclude_public])
41
+ end
42
+ Updater.update(new_hosts_list.join("\n"), project, @options[:file], backup, @options[:dry_run], @options[:delete])
32
43
  end
33
- Updater.update(new_hosts_list.join("\n"), project, @options[:file], backup, @options[:dry_run], @options[:delete])
34
44
  end
35
45
 
36
46
  private
@@ -11,8 +11,8 @@ module GcloudHosts
11
11
  end
12
12
 
13
13
  def self.update(new_hosts, project, file, backup_file, dry_run, delete)
14
- start_marker = "# START GCLOUD HOSTS - #{project} #"
15
- end_marker = "# END GCLOUD HOSTS - #{project} #"
14
+ start_marker = get_start_marker(project)
15
+ end_marker = get_end_marker(project)
16
16
 
17
17
  old_hosts = File.read(file)
18
18
 
@@ -98,6 +98,35 @@ module GcloudHosts
98
98
  new_content
99
99
  end
100
100
 
101
+ def self.clear(file, backup_file, dry_run)
102
+ old_hosts = File.read(file)
103
+ new_content = old_hosts.dup
104
+
105
+ markers = old_hosts.each_line.map do |line|
106
+ regex = "\# (START|END) GCLOUD HOSTS - (.+) \#"
107
+ if m = line.match(/^#{regex}$/)
108
+ m[2]
109
+ end
110
+ end.compact.uniq
111
+
112
+ markers.each do |project|
113
+ start_marker = get_start_marker(project)
114
+ end_marker = get_end_marker(project)
115
+
116
+ new_content = delete_project_hosts(new_content, start_marker, end_marker)
117
+ end
118
+ new_content.gsub!(/\s+$/, "\n")
119
+
120
+ if dry_run
121
+ puts new_content
122
+ elsif new_content != old_hosts
123
+ # backup old host file
124
+ File.open(backup_file, 'w') { |f| f << old_hosts }
125
+ # write new content
126
+ File.open(file, 'w') { |f| f << new_content }
127
+ end
128
+ end
129
+
101
130
  def self.delete_project_hosts(hosts, start_marker, end_marker)
102
131
  new_content = ''
103
132
  marker_state = Marker::BEFORE
@@ -128,5 +157,13 @@ module GcloudHosts
128
157
  new_content
129
158
  end
130
159
 
160
+ def self.get_start_marker(project)
161
+ "# START GCLOUD HOSTS - #{project} #"
162
+ end
163
+
164
+ def self.get_end_marker(project)
165
+ "# END GCLOUD HOSTS - #{project} #"
166
+ end
167
+
131
168
  end
132
169
  end
@@ -1,3 +1,3 @@
1
1
  module GcloudHosts
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcloud_hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Tongen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-21 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler