Class: NOMS::Optconfig

Inherits:
Hash show all
Defined in:
lib/noms/optconfig.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#to_bashon

Methods included from BashOn

#name_key

Constructor Details

#initialize(domain, submitted_optspec, version = nil) ⇒ Optconfig

Returns a new instance of Optconfig.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/noms/optconfig.rb', line 61

def initialize(domain, , version=nil)
   @domain = domain
   @optspec = add_standard_opts()
    @caller_version = version || $VERSION || 'Unknown version'

   .each_pair do |optspec, val|
      opt, dummy = optspec.split(/[\=\+\!]/, 2)
      self[opt] = val
   end

    cfgfilepath = [ '/usr/local/etc/' + domain + '.conf',
        '/etc/' + domain + '.conf' ]

   if ENV.has_key? 'HOME' and ! ENV['HOME'].nil?
      cfgfilepath.unshift(ENV['HOME'] + '/.' + domain)
   end
   @config = nil

    cmdlineopt = Longopt.new(optspec.keys)

   if cmdlineopt.has_key? 'config'
      @config = cmdlineopt['config']
      raise "File not found: #{cmdlineopt['config']}" unless
         File.exist? cmdlineopt['config']
      read_config(cmdlineopt['config'])
   else
      cfgfilepath.each do |file|
         if File.readable? file
            @config = file
            read_config(file)
            break
         end
      end
   end

   cmdlineopt.each_pair do |opt, val|
      merge_cmdlineopt(opt, val)
   end

   if self.has_key? 'version' and self['version']
       puts @caller_version
       Process.exit(0)
   end

    if self.has_key? 'help' and self['help']
       help_pattern = /(?:^=head1 +SYNOPSIS|^# *=+ *SYNOPSIS)(.*?)(?:^=head1|^# *=+)/m
       myscript = File.expand_path($0)
       begin
           script_text = File.open(myscript, 'r') { |fh| fh.read }
           if /This file was generated by RubyGems/.match script_text
               # Well, thank you RubyGems. Now I have to guess where
               # my code ended up.
               #
               # The ruby gems wrapper for these scripts has some
               # special way of restricting versions here; I'm hoping
               # that since that has been done (and I no longer have
               # access to the special first argument that overrides
               # the version) that the fact that the 'gem' call has
               # already been called with that version will help
               # the following work correctly. Because there's no
               # real other alternative.
               #
               # This 'parsing' is really a load of crap. Sure hope
               # Rubygems never changes anything.
               # -jdb/20141010
               if m = /^\s*load +(.*)$/.match(script_text)
                   script_file = eval("version = nil\n" + m[1])
                   script_text = File.open(script_file, 'r') { |fh| fh.read }
               end
           end
           m = help_pattern.match(script_text)
           if m
               puts m[1].gsub(/^# ?/m, '').strip
           else
               puts 'No help'
           end
       rescue Errno::ENOENT
           puts "No help (could not search #{myscript})"
       end
       Process.exit(0)
   end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



42
43
44
# File 'lib/noms/optconfig.rb', line 42

def config
  @config
end

#defaultObject

Returns the value of attribute default.



42
43
44
# File 'lib/noms/optconfig.rb', line 42

def default
  @default
end

#domainObject

Returns the value of attribute domain.



42
43
44
# File 'lib/noms/optconfig.rb', line 42

def domain
  @domain
end

#optspecObject

Returns the value of attribute optspec.



42
43
44
# File 'lib/noms/optconfig.rb', line 42

def optspec
  @optspec
end

Instance Method Details

#add_standard_opts(submitted_optspec) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/noms/optconfig.rb', line 44

def add_standard_opts()
   optspec = 
   standard_opts = {
      'config=s' => nil,
      'debug+' => 0,
      'verbose+' => 0,
      'version' => false,
      'help' => false,
      'dry-run!' => false }
   standard_opts.each_pair do |opt, defval|
      if ! optspec.has_key? opt
         optspec[opt] = defval
      end
   end
   optspec
end

#dbg(level, *msg) ⇒ Object



180
181
182
183
184
# File 'lib/noms/optconfig.rb', line 180

def dbg(level, *msg)
   if self['debug'] >= level
      puts "DBG(#{@domain}): " + msg.join("DBG(#{@domain}):    ")
   end
end

#merge_cmdlineopt(opt, val) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/noms/optconfig.rb', line 151

def merge_cmdlineopt(opt, val)
   if self.has_key? opt
      if self[opt].respond_to? :keys
         if val.respond_to? :keys
            # Both hashes, merge
            val.each_pair { |k, v| self[opt][k] = v }
         else
            self[opt] = val
         end
      elsif self[opt].respond_to? :unshift
         if val.respond_to? :each and val.respond_to? :reverse
            val.reverse.each { |v| self[opt].unshift(v) }
         else
            self[opt] = val
         end
      else
         self[opt] = val
      end
   else
      self[opt] = val
   end

   self[opt]
end

#read_config(file) ⇒ Object



144
145
146
147
148
149
# File 'lib/noms/optconfig.rb', line 144

def read_config(file)
   fileconfig = File.open(file) { |fh| JSON.load(fh) }
   fileconfig.each_pair do |opt, val|
      self[opt] = val
   end
end

#vrb(level, *msg) ⇒ Object



176
177
178
# File 'lib/noms/optconfig.rb', line 176

def vrb(level, *msg)
   puts msg.join("\n") if self['verbose'] >= level
end