简体   繁体   English

Perl:命令行覆盖配置文件设置?

[英]Perl: command-line override of config file settings?

I'm building a script that utilizes a config file (YAML) to read in all the necessary configuration information, then prints out all the necessary steps a Linux Admin needs to step through to build a server. 我正在构建一个利用配置文件(YAML)读取所有必要配置信息的脚本,然后打印出Linux管理员需要逐步构建服务器所需的所有必要步骤。

A required option is for the Linux Admin that's running the script to be able to override any of the item/value pairs from the config file, at the command-line. 对于运行脚本的Linux Admin,必需的选项是能够在命令行覆盖配置文件中的任何项/值对。

The way I'm currently handling this seems overly cumbersome and I know there's got to be a more innovative and less clunky way to do this. 我目前正在处理这种方式似乎过于繁琐,我知道必须采用更具创新性和更少笨重的方式来实现这一目标。


In the code I: 在代码I中:

  1. Parse the YAML config file with YAML::Tiny 使用YAML :: Tiny解析YAML配置文件

     location: continent: na country: us city: rh 
  2. Create variables with the same names as the config file items, assigning the values from the config file. 使用与配置文件项相同的名称创建变量,从配置文件中分配值。

     my $yaml = YAML::Tiny->new; $yaml = YAML::Tiny->read($config_yml); my $continent = $yaml->[0]->{location}->{continent}; my $country = $yaml->[0]->{location}->{country}; my $city = $yaml->[0]->{location}->{city}; 
  3. Use Getopt::Long and assign the variables, overriding anything passed at the command-line. 使用Getopt :: Long并分配变量,覆盖在命令行传递的任何内容。

     GetOptions ( "city=s" => \\$city, "continent=s" => \\$continent, "country=s" => \\$country, ); 

So those are just 3 item/values pairs, my actual config has over 40 and will change...Which makes for a bit of work to have to keep updating. 所以那些只是3个项目/值对,我的实际配置超过40并且将改变...这使得必须继续更新的一些工作。 Any suggestions? 有什么建议?

You can let the admin override the YAML settings with a single, flexible switch similar to what ssh(1) does with -o . 你可以让管理员用一个灵活的开关覆盖YAML设置,类似于ssh(1)-o This is especially appropriate if the config settings are numerous and likely to change. 如果配置设置很多且可能更改,这尤其适用。

$ myscript -o location:city=rh --option location:country=us

Now, inside the script, you might keep all your runtime config bundled together in a hash for convenience (rather than having $this_and_that_opt scalars proliferate over time). 现在,在脚本内部,您可以将所有运行时配置捆绑在一起以方便使用(而不是让$this_and_that_opt标量随着时间的推移而扩散)。 Option parsing would then look something like this: 选项解析将看起来像这样:

# First, set up %GlobalAppCfg from defaults and YAML
# now handle "-o location:country=us"
GetOptions('option|o=s' => sub {
                              my (undef, $optstring) = @_;

                              my ($userkey, $val) = split('=', $optstring, 2);
                              my ($major, $minor) = split(':', $userkey,   2);

                              $GlobalAppCfg->{$major}->{$minor} = $val;
                            },
            ...);

or whatever. 管他呢。 You can normalize config keys and values, handle arbitrarily deep key/subkey/subkey configs, etc. This can get slippery, so you might like to key-lock that global hash. 您可以规范化配置键和值,处理任意深度键/子键/子键配置等。这可能会变得很滑,因此您可能希望键锁定该全局哈希。

看一下与GetOpt和YAML结合的一些Config模块,可能是Config :: YAMLConfig :: YAML :: Tiny

Just a sketch, but 只是草图,但是

  1. In your GetOptions call, could you use "deep references" into the YAML structure, thereby getting rid of the "intermediate" variables? 在你的GetOptions调用中,你可以在YAML结构中使用“深度引用”,从而摆脱“中间”变量吗?

  2. By looking at the generated YAML structure, could you not generate the GetOptions call automatically (based on what variables you actually see in the YAML). 通过查看生成的YAML结构,您是否可以自动生成GetOptions调用(基于您在YAML中实际看到的变量)。 By generate, I mean, create the call as a string, and then use "eval" to actually execute it. 通过generate,我的意思是,将调用创建为字符串,然后使用“eval”来实际执行它。

  3. If you want the "intermediate variables" for convenience, you could probably generate those yourself from the YAML structure, also as a string, and then use "eval" to actually create the variables. 如果你想要“中间变量”以方便起见,你可以自己从YAML结构中生成它们,也可以作为字符串,然后使用“eval”来实际创建变量。

As I said, just a sketch. 正如我所说,只是一个草图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM