workEnv 0.1.0

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.
@@ -0,0 +1,185 @@
1
+ #!/usr/bin/ruby
2
+ module WorkEnvs
3
+
4
+ # Returns a hash describing a host setup
5
+ #
6
+ # if machine is passed, it overrides host detection and used machine string instead
7
+ #
8
+ # Return format is
9
+ # - :label: machine string or generated string that can be passed to getArch
10
+ # - :distrib: machine distribution (mingw, centos, debian, fedora, ubuntu, unknown)
11
+ # - :version: Distrib version
12
+ # - :arch: Core architecture (i386, x86_64)
13
+ # - :base: Distribution base (RHEL, debian, WINNT, unknown)
14
+ def getArch(machine = nil, ignore_settings = false)
15
+ arch = {}
16
+ machine = WorkEnvs::settings[:arch][:machine] if machine == nil && ignore_settings == false
17
+
18
+ if machine == nil
19
+ uname=`uname -s`.chomp()
20
+ if(uname =~ /^MINGW([0-9]{2})_NT-([0-9]+\.[0-9]+)/) then
21
+ machine="mingw#{$2}-#{$1}"
22
+ else
23
+ machine=`get_machine() {
24
+ ARCH=$(uname -m)
25
+ ARCH_S="32"
26
+ if [ "$ARCH" = "x86_64" ]; then
27
+ ARCH_S="64";
28
+ fi
29
+
30
+ if [ -f /etc/fedora-release ]; then
31
+ FC_NUM=$(sed -e 's/^Fedora release \\([0-9]\\+\\) .*$/\\1/' /etc/fedora-release)
32
+ echo "fedora${FC_NUM}-${ARCH_S}";
33
+ elif [ -f /etc/redhat-release ]; then
34
+ if grep -q "Red Hat" /etc/redhat-release ; then
35
+ if grep -q ' 5\\.' /etc/redhat-release ; then
36
+ echo "redhat5-${ARCH_S}";
37
+ elif grep -q ' 6\\.' /etc/redhat-release ; then
38
+ echo "redhat6-${ARCH_S}";
39
+ elif grep -q ' 7\\.' /etc/redhat-release ; then
40
+ echo "redhat7-${ARCH_S}";
41
+ else
42
+ echo "Unsupported centos/RHEL version"
43
+ exit 1
44
+ fi
45
+ elif grep -q "CentOS" /etc/redhat-release ; then
46
+ if grep -q ' 5\\.' /etc/redhat-release ; then
47
+ echo "centos5-${ARCH_S}";
48
+ elif grep -q ' 6\\.' /etc/redhat-release ; then
49
+ echo "centos6-${ARCH_S}";
50
+ elif grep -q ' 7\\.' /etc/redhat-release ; then
51
+ echo "centos7-${ARCH_S}";
52
+ else
53
+ echo "Unsupported centos/RHEL version"
54
+ exit 1
55
+ fi
56
+ else
57
+ echo "Unsupported centos/RHEL version"
58
+ exit 1
59
+ fi
60
+ elif [ -f /etc/lsb-release ]; then
61
+ . /etc/lsb-release
62
+ VER=$(echo ${DISTRIB_RELEASE} | sed -e 's/\\.//g')
63
+ echo "ubuntu${VER}-${ARCH_S}"
64
+ elif [ -f /etc/debian_version ]; then
65
+ VER=$(cat /etc/debian_version | sed -e 's/\\.[0-9]*//g')
66
+ echo "debian${VER}-${ARCH_S}";
67
+ else
68
+ source /etc/os-release
69
+ echo "$ID_LIKE" | grep -q "suse"
70
+ if [ $? -eq 0 ]; then
71
+ echo "suse$(echo ${VERSION_ID} | sed -e 's/\\..*$//')-${ARCH_S}"
72
+ else
73
+ echo "Unknown"
74
+ return 1;
75
+ fi
76
+ fi
77
+ return 0;
78
+ }
79
+ get_machine`
80
+ raise("Failed to get Arch (#{$?}): #{machine}") if $? != 0
81
+ machine.chomp!()
82
+ end
83
+ end
84
+ case(machine)
85
+ when /debian([0-9]+)-32/
86
+ arch[:version]=$1
87
+ arch[:label]="debian#{arch[:version]}-32"
88
+ arch[:distrib]="debian"
89
+ arch[:arch]="i386"
90
+ arch[:base]="debian"
91
+ when /debian([0-9]+)-64/
92
+ arch[:version]=$1
93
+ arch[:label]="debian#{arch[:version]}-64"
94
+ arch[:distrib]="debian"
95
+ arch[:arch]="amd64"
96
+ arch[:base]="debian"
97
+ when "ubuntu804-32"
98
+ arch[:label]="debian5-32"
99
+ arch[:distrib]="debian"
100
+ arch[:version]="5"
101
+ arch[:arch]="i386"
102
+ arch[:base]="debian"
103
+ when "ubuntu804-64"
104
+ arch[:label]="debian5-64"
105
+ arch[:distrib]="debian"
106
+ arch[:version]="5"
107
+ arch[:arch]="amd64"
108
+ arch[:base]="debian"
109
+ when "ubuntu1004-32"
110
+ arch[:label]="debian6-32"
111
+ arch[:distrib]="debian"
112
+ arch[:version]="6"
113
+ arch[:arch]="i386"
114
+ arch[:base]="debian"
115
+ when "ubuntu1004-64"
116
+ arch[:label]="debian6-64"
117
+ arch[:distrib]="debian"
118
+ arch[:version]="6"
119
+ arch[:arch]="amd64"
120
+ arch[:base]="debian"
121
+ when /fedora([0-9]+)-32/
122
+ arch[:version]=$1
123
+ arch[:label]="fedora#{arch[:version]}-32"
124
+ arch[:distrib]="fedora"
125
+ arch[:arch]="i386"
126
+ arch[:base]="RHEL"
127
+ when /fedora([0-9]+)-64/
128
+ arch[:version]=$1
129
+ arch[:label]="fedora#{arch[:version]}-64"
130
+ arch[:distrib]="fedora"
131
+ arch[:arch]="x86_64"
132
+ arch[:base]="RHEL"
133
+ when /^centos([0-9]+)-32$/
134
+ arch[:version]=$1
135
+ arch[:label]="centos#{arch[:version]}-32"
136
+ arch[:distrib]="centos"
137
+ arch[:arch]="i386"
138
+ arch[:base]="RHEL"
139
+ when /^centos([0-9]+)-64$/
140
+ arch[:version]=$1
141
+ arch[:label]="centos#{arch[:version]}-64"
142
+ arch[:distrib]="centos"
143
+ arch[:arch]="x86_64"
144
+ arch[:base]="RHEL"
145
+ when /^redhat([0-9]+)-32$/
146
+ arch[:version]=$1
147
+ arch[:label]="redhat#{arch[:version]}-32"
148
+ arch[:distrib]="redhat"
149
+ arch[:arch]="i386"
150
+ arch[:base]="RHEL"
151
+ when /^redhat([0-9]+)-64$/
152
+ arch[:version]=$1
153
+ arch[:label]="redhat#{arch[:version]}-64"
154
+ arch[:distrib]="redhat"
155
+ arch[:arch]="x86_64"
156
+ arch[:base]="RHEL"
157
+ when /^suse([0-9\.]+)-64$/
158
+ arch[:version]=$1
159
+ arch[:label]="suse#{arch[:version]}-64"
160
+ arch[:distrib]="suse"
161
+ arch[:arch]="x86_64"
162
+ arch[:base]="SUSE"
163
+ when /^mingw([0-9]+\.[0-9]+)-32$/
164
+ arch[:label]="mingw#{$1}-32"
165
+ arch[:distrib]="mingw"
166
+ arch[:version]=$1
167
+ arch[:arch]="i386"
168
+ arch[:base]="WINNT"
169
+ when '%'
170
+ arch[:label]="%"
171
+ arch[:distrib]="unknown"
172
+ arch[:version]="unknown"
173
+ arch[:arch]="unknown"
174
+ arch[:base]="unknown"
175
+ else
176
+ arch[:label]="unknown"
177
+ arch[:distrib]="unknown"
178
+ arch[:version]="unknown"
179
+ arch[:arch]="unknown"
180
+ arch[:base]="unknown"
181
+ end
182
+ return arch
183
+ end
184
+ module_function :getArch
185
+ end
@@ -0,0 +1,364 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'yaml'
4
+ require_relative 'EnvOpts'
5
+
6
+ module WorkEnvs
7
+ # Path to global configuration file
8
+ WORK_ENV_DB_GLOBAL_CONF = WORK_ENV_GLOBAL_DIR + "/config-global"
9
+
10
+ # Path to host specific configuration file
11
+ WORK_ENV_DB_DEFAULT_CONF = WORK_ENV_CACHE_DIR + "/config"
12
+
13
+ # Version of the settings has. Used for migration
14
+ WORK_ENV_CONFIG_VERSION = 15
15
+
16
+ # Default settings value
17
+ @@BASE_SETTINGS = {
18
+ :db => {
19
+ :initialized => false,
20
+ :package_db => "packages-db",
21
+ :package_db_proto => :mysql,
22
+ :package_repos => [ "http://packages:88" ],
23
+ :maxcount => 5,
24
+ :package_db_user => "workEnv",
25
+ :package_db_passwd => "workEnv",
26
+ :package_db_db => "repositories",
27
+ :package_default_table => "package",
28
+ :package_db_tables => [ "package", "releases", "integration" ],
29
+ :git_repo => "git",
30
+
31
+ },
32
+ :arch => {
33
+ :machine => nil,
34
+ },
35
+ :global => {
36
+ :check_updates => true,
37
+ :confirm_delete => true,
38
+ :config_file => nil,
39
+ :silent_update => false,
40
+ :version => WORK_ENV_CONFIG_VERSION,
41
+ :unthreaded => false,
42
+ :custom_env_dirs => {},
43
+ }
44
+ }
45
+
46
+ # Default settings has
47
+ #
48
+ # Filled during the first call to settings() by parsing options and config files
49
+ @@settings = nil
50
+
51
+ # Merge hash settings into settings
52
+ #
53
+ # Use to merge default, global and host settings
54
+ def merge_settings(settings, hash)
55
+ hash.each(){|top_key, sub_hash|
56
+ next if settings[top_key] == nil
57
+ sub_hash.each(){|key, val|
58
+ settings[top_key][key] = val
59
+ }
60
+ }
61
+ end
62
+ module_function :merge_settings
63
+
64
+ # Remove hash settings from settings
65
+ #
66
+ # Use to remove host, global or default settings value to generate the lightest config file
67
+ def extract_settings(settings, hash)
68
+ hash.each(){|top_key, sub_hash|
69
+ next if settings[top_key] == nil
70
+ sub_hash.each(){|key, val|
71
+ next if top_key == :global and key == :version
72
+ settings[top_key].delete(key) if settings[top_key][key] == val
73
+ }
74
+ }
75
+ end
76
+ module_function :extract_settings
77
+
78
+ # Migrate config object to the latest version
79
+ def config_migrate(settings)
80
+ return {} if settings == {}
81
+ case settings[:global][:version]
82
+ when nil, 1
83
+ settings[:global][:version] = 2
84
+ config_migrate(settings)
85
+ when 2
86
+ settings[:global][:version] = 3
87
+ settings[:global][:check_updates] = true
88
+ config_migrate(settings)
89
+ when 3
90
+ settings[:global][:version] = 4
91
+ settings[:global][:confirm_delete] = true
92
+ config_migrate(settings)
93
+ when 4
94
+ settings[:global][:version] = 5
95
+ settings[:global][:silent_update] = false
96
+ config_migrate(settings)
97
+ when 5
98
+ settings[:global][:version] = 6
99
+ config_migrate(settings)
100
+ when 6
101
+ settings[:db][:package_repo] = "packages:88" if settings[:db][:package_repo] == "hermes:88"
102
+ settings[:global][:version] = 7
103
+ config_migrate(settings)
104
+ when 7
105
+ settings[:db][:package_db] = "packages-db" if settings[:db][:package_db] == "redmine"
106
+ settings[:global][:version] = 8
107
+ config_migrate(settings)
108
+ when 8
109
+ settings[:db][:git_repo] = "git"
110
+ settings[:global][:version] = 9
111
+ config_migrate(settings)
112
+ when 9
113
+ settings[:global][:version] = 10
114
+ config_migrate(settings)
115
+ when 10
116
+ settings[:global][:package_repos] = [
117
+ settings[:global][:package_repo_proto] + "://" + settings[:global][:package_repo]
118
+ ]
119
+ settings[:global][:package_repo] = nil
120
+ settings[:global][:package_repo_proto] = nil
121
+ settings[:global][:version] = 11
122
+ config_migrate(settings)
123
+ when 11
124
+ settings[:db][:package_db_proto] = :mysql
125
+ settings[:global][:version] = 12
126
+ config_migrate(settings)
127
+ when 12
128
+ settings[:global][:version] = 13
129
+ config_migrate(settings)
130
+ when 13
131
+ settings[:global][:unthreaded] = false
132
+ settings[:global][:version] = 14
133
+ config_migrate(settings)
134
+ when 14
135
+ settings[:global][:custom_env_dirs] = {}
136
+ settings[:global][:version] = 15
137
+ config_migrate(settings)
138
+ when WORK_ENV_CONFIG_VERSION
139
+ return settings
140
+ else
141
+ raise("")
142
+ end
143
+ end
144
+ module_function :config_migrate
145
+
146
+ # Load and return a settings hash from a file
147
+ #
148
+ # Automatically migrate old version to the latest
149
+ def config_load(path)
150
+ begin
151
+ return {} if !File.exist?(path)
152
+ desc = File.open(path)
153
+ settings = YAMLLoad(desc)
154
+ desc.close()
155
+ return config_migrate(settings)
156
+ rescue => e
157
+ STDERR.puts "Invalid settings file '#{path}'.\n#{e}\nIgnoring..."
158
+ @@settings[:global].delete(:config_file)
159
+ return {}
160
+ end
161
+ end
162
+ module_function :config_load
163
+
164
+ # Save settings to global settings file
165
+ #
166
+ # Remove default settings and only write the modified ones
167
+ def save_global_config(settings)
168
+ modified_settings = YAMLLoad(settings.to_yaml())
169
+ extract_settings(modified_settings, @@BASE_SETTINGS)
170
+ modified_settings[:global] = {} if modified_settings[:global] == nil
171
+ modified_settings[:global][:version] = WORK_ENV_CONFIG_VERSION
172
+
173
+ runCmd("mkdir -p #{File.dirname(WORK_ENV_DB_GLOBAL_CONF)}", !VERBOSE)
174
+ desc = File.open(WORK_ENV_DB_GLOBAL_CONF, "w+")
175
+ desc.puts modified_settings.to_yaml()
176
+ desc.close()
177
+ end
178
+ module_function :save_global_config
179
+
180
+ # Save settings to host settings file
181
+ #
182
+ # Remove default and global settings and only write the modified ones
183
+ def save_local_settings(settings, path = WORK_ENV_DB_DEFAULT_CONF)
184
+ path = WORK_ENV_DB_DEFAULT_CONF if path.to_s == ""
185
+
186
+ global_settings = config_load(WORK_ENV_DB_GLOBAL_CONF)
187
+ modified_settings = YAMLLoad(settings.to_yaml())
188
+ extract_settings(modified_settings, global_settings)
189
+ extract_settings(modified_settings, @@BASE_SETTINGS)
190
+ settings[:global][:version] = WORK_ENV_CONFIG_VERSION
191
+
192
+ runCmd("mkdir -p #{File.dirname(path)}", !VERBOSE)
193
+ desc = File.open(path, "w+")
194
+ desc.puts modified_settings.to_yaml()
195
+ desc.close()
196
+ end
197
+ module_function :save_local_settings
198
+
199
+ # Fetch the settings hash
200
+ #
201
+ # Generate a setting hash from the default values, the global and the local settings files
202
+ #
203
+ # Store them in @@settings to avoid reparsing file every time
204
+ def settings(options = {})
205
+ return @@settings if @@settings != nil &&
206
+ ( options.empty? == true || options.values.inject({}){|q, c| q.merge(c)}.empty? == true)
207
+
208
+ @@settings = YAMLLoad(@@BASE_SETTINGS.to_yaml())
209
+ return @@settings if options[:control] != nil && options[:control][:reset] != nil
210
+
211
+ config_file = options[:control] != nil ? options[:control][:config_file].to_s : ""
212
+ if config_file == ""
213
+ config_file = WORK_ENV_DB_DEFAULT_CONF
214
+ else
215
+ raise("Invalid config file #{config_file}") if !File.exist?(config_file)
216
+ end
217
+ # Load the global config
218
+ if File.exist?(WORK_ENV_DB_GLOBAL_CONF) then
219
+ settings = config_load(WORK_ENV_DB_GLOBAL_CONF)
220
+ merge_settings(@@settings, settings)
221
+ end
222
+
223
+ # Try to load local config from home
224
+ if (options[:control] == nil || options[:control][:globalOnly] != true) && File.exist?(config_file) then
225
+ @@settings[:global][:config_file] = config_file
226
+ settings = config_load(config_file)
227
+ merge_settings(@@settings, settings)
228
+ @@settings[:global][:config_file] = config_file if @@settings[:global][:config_file].to_s == ""
229
+ end
230
+
231
+ merge_settings(@@settings, options)
232
+ @@settings[:global][:check_updates] = false if getArch()[:base] == "WINNT"
233
+
234
+ return @@settings
235
+ end
236
+ module_function :settings
237
+
238
+ # Add setting options to opts Parser
239
+ def configSelectorPrepare(opts, optsParser)
240
+ opts[:settings] = { :db => {}, :arch =>{}, :global => {}, :control => {}}
241
+
242
+ optsParser.separator "\nDatabase Connection Settings:"
243
+ optsParser.on("--package-db <DB hostname:port|db.sqlite>", String,
244
+ "Package Database hostname (default is #{@@BASE_SETTINGS[:package_db]}).") {|val|
245
+ opts[:settings][:db][:package_db] = val}
246
+ optsParser.on("--package-db-proto <mysql|sqlite>", String,
247
+ "Protocol for package database (default is #{@@BASE_SETTINGS[:package_db_proto]}).") {|val|
248
+ opts[:settings][:db][:package_db_proto] = val.to_sym()}
249
+ optsParser.on("--package-repo <(http|https)://repo hostname:port|file:///path/to/rpm>", String,
250
+ "Package Repository URI (default is #{@@BASE_SETTINGS[:package_repo]}).") {|val|
251
+ opts[:settings][:db][:package_repos] = [] if opts[:settings][:db][:package_repos] == nil
252
+ opts[:settings][:db][:package_repos] << val
253
+ }
254
+ optsParser.on("-T", "--table <table name> ", String,
255
+ "Select database table (conflicts with --release).") {|val|
256
+ opts[:settings][:db][:package_default_table] = val}
257
+ optsParser.on("--table-list <table name,table name,...> ", String,
258
+ "Select database table list (conflicts with --release).") {|val|
259
+ opts[:settings][:db][:package_db_tables] = val.split(",")}
260
+ optsParser.on("--rpc-host [user@]host[:/path/to/envs]", String, "Remote host to use RPC.") {|val|
261
+ opts[:settings][:db][:rpc_host] = val
262
+ }
263
+
264
+ opts[:settings][:arch][:machine] = opts[:machine] if opts[:machine] != nil
265
+ optsParser.separator "\nArch Settings:"
266
+ optsParser.on("-m", "--machine <machinetype>", String,
267
+ "Machine type to use (Default = #{WorkEnvs::getArch(opts[:machine], true)[:label]}).") {|val|
268
+ opts[:settings][:arch][:machine] = WorkEnvs::getArch(val, true)[:label]}
269
+
270
+ optsParser.separator "\nGeneric Config Settings:"
271
+ optsParser.on("--config-file </path/to/file>", String, "YAML config file do DB settings.") {|val|
272
+ opts[:settings][:control][:config_file] = val}
273
+ optsParser.on("--default-config", nil, "Ignore all config file and use the default config. Combine with --save-config to reset your config files.") {|val| opts[:settings][:control][:reset] = true}
274
+ optsParser.on("--save-config [/path/to/save/file]", String,
275
+ "Save the current option set to destionation file (default is #{WORK_ENV_DB_DEFAULT_CONF}).") {|val|
276
+ opts[:settings][:control][:save_config_file] = val.to_s}
277
+ optsParser.on("--show-config", nil, "Show the config used for DB connections.") {|val|
278
+ opts[:settings][:control][:show_config] = true}
279
+ optsParser.on("--clear-config", nil, "Remove default config file.") {|val|
280
+ Confirm.new("Do you want to remove the config file: #{WORK_ENV_DB_DEFAULT_CONF}")
281
+ runCmd("rm -f #{WORK_ENV_DB_DEFAULT_CONF}")
282
+ exit 0
283
+ }
284
+ optsParser.on("--[no-]unthreaded", "Disable multithreaded package download") {|val|
285
+ opts[:settings][:global][:unthreaded] = val}
286
+ optsParser.on("-U", "--update", nil, "Look for workEnv updates.") {
287
+ |val| opts[:settings][:control][:updateLookup] = true}
288
+ optsParser.on("--no-auto-update", nil, "Disable auto updates.") {
289
+ |val| opts[:settings][:global][:check_updates] = false}
290
+ optsParser.on("--yes", nil, "Assume yes to all questions.") {
291
+ |val| opts[:settings][:global][:alwaysYes] = true}
292
+ optsParser.on("--no", nil, "Assume no to all questions.") {
293
+ |val| opts[:settings][:global][:alwaysNo] = true}
294
+ optsParser.on("-R", "--rpc <base64>", String, "RPC Base64 encoded parameters" ) {
295
+ |val| opts[:settings][:control][:rSettings] = WorkEnvs::deserialize(val)[0]}
296
+ optsParser.on("--custom-env-dirs [label1=path1,name2=path2]", String, "Add custom environment dirs" ) {
297
+ |val|
298
+ opts[:settings][:global][:custom_env_dirs] = {}
299
+ val.to_s().split(",").each(){|dir|
300
+ a = dir.split("=")
301
+ raise("Invalid custom-env-dir format '#{dir}'") if a.length != 2
302
+ opts[:settings][:global][:custom_env_dirs][a[0]] = a[1]
303
+ }
304
+ }
305
+
306
+ end
307
+ module_function :configSelectorPrepare
308
+
309
+ # Post process settings passed to command line
310
+ #
311
+ # All the value are changed in the settings already
312
+ #
313
+ # This is just used to show/modify/dump config files
314
+ def configSelectorFinal(opts)
315
+ doUpdate = false
316
+ control = opts[:settings][:control]
317
+ doUpdate = true if control[:updateLookup] == true
318
+ if opts[:settings][:control][:rSettings] != nil then
319
+ # Merge RPC provided settings with ours
320
+ # Drop unwanted stuff first
321
+ # Clean out all regualr control so we cannot hack config through RPC
322
+ rArg = opts[:settings][:control][:rSettings].merge({})
323
+ rSettings = rArg[:settings].merge({})
324
+ opts[:settings][:control] = {}
325
+ opts[:settings][:control][:rSettings] = rArg
326
+
327
+ rSettings[:control] = {}
328
+ dbRSettings = rSettings[:db]
329
+ rSettings[:db] = {}
330
+ rSettings[:db][:package_db_tables] = dbRSettings[:package_db_tables] if dbRSettings[:package_db_tables] != nil
331
+ merge_settings(opts[:settings], rSettings)
332
+ end
333
+ settings = WorkEnvs::settings(opts[:settings])
334
+
335
+ if doUpdate == true
336
+ WorkEnvs::checkEnvs(true)
337
+ exit
338
+ end
339
+
340
+ if settings[:global][:alwaysNo] == true && settings[:global][:alwaysYes] == true then
341
+ raise("Cannot use --yes and --no at the same time")
342
+ end
343
+ if control[:show_config] == true then
344
+ puts "Current DB configuration"
345
+ puts settings.to_yaml
346
+ exit 0
347
+ end
348
+
349
+ if opts[:envOpts].class == WorkEnvs::EnvOpts &&
350
+ settings[:global][:default_options].class == WorkEnvs::EnvOpts then
351
+ opts[:envOpts].concat(settings[:global][:default_options])
352
+ end
353
+ if control[:save_config_file] != nil then
354
+ out_file = control[:save_config_file]
355
+ if control[:globalOnly] == true then
356
+ save_global_config(settings)
357
+ else
358
+ save_local_settings(settings, out_file)
359
+ end
360
+ exit 0
361
+ end
362
+ end
363
+ module_function :configSelectorFinal
364
+ end
@@ -0,0 +1,80 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module WorkEnvs
4
+ # Exception thrown by Confirm when confirm dialog is aborted
5
+ class ConfirmAbortedException < StandardError
6
+ # Default constructor
7
+ def initialize()
8
+ super("Aborted...")
9
+ end
10
+ end
11
+
12
+ # Create a confirmation" dialog"
13
+ #
14
+ # Asks user to reply yes/no if neither --yes or --no is set
15
+ #
16
+ # Return true for yes, false for no
17
+ class Confirm
18
+ # Default constructor
19
+ #
20
+ # str is the string to prompt the user ebfore asking yes/no
21
+ def initialize(str)
22
+ while true do
23
+ printf str + " (y(es)/N(o)): "
24
+ if WorkEnvs::settings()[:global][:alwaysYes] == true
25
+ puts "(--yes option enabled)"
26
+ return
27
+ end
28
+ if WorkEnvs::settings()[:global][:alwaysNo] == true
29
+ puts "(--no option enabled)"
30
+ raise ConfirmAbortedException
31
+ end
32
+ rep = STDIN.gets.to_s().chomp().downcase()
33
+ case rep
34
+ when "y","yes"
35
+ return
36
+ when "n","no"
37
+ raise ConfirmAbortedException
38
+ else
39
+ puts "Are you sure you can read ? That question was not that hard..."
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ # Variation of the Confirm class that support --always
46
+ #
47
+ # Return true, (yes), false (no) or :always (always)
48
+ class OverWrite
49
+ # Prompt the user the str string and ask for an answer (yes, no, always)
50
+ #
51
+ # lastValue should be nil for the first call
52
+ # if lastValue == :always, returns ::always
53
+ def self.check(str, lastVal)
54
+ return :always if lastVal == :always
55
+ while true do
56
+ printf str + " (y(es)/N(o)/a(lways)): "
57
+ if WorkEnvs::settings()[:global][:alwaysYes] == true
58
+ puts "(--yes option enabled)"
59
+ return :always
60
+ end
61
+ if WorkEnvs::settings()[:global][:alwaysNo] == true
62
+ puts "(--no option enabled)"
63
+ return false
64
+ end
65
+ rep = STDIN.gets.chomp().downcase()
66
+ case rep
67
+ when 'y', 'yes'
68
+ return true
69
+ when 'n', 'no'
70
+ return false
71
+ when 'a', 'always'
72
+ return :always
73
+ else
74
+ puts "Please remove your mitten before typing anymore wrong answers..."
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ end