From 4448213735dfaa6639faf7243158d2f4e80b0f1c Mon Sep 17 00:00:00 2001 From: "docelic@crystallabs.io" Date: Sun, 28 Apr 2019 20:11:46 +0000 Subject: [PATCH] Fix precedence of command line arguments Previously, when autogenerating the config file, the name of config itself was included in the YAML contents. This in turn caused the config file to be re-parsed again and any command line arguments were overwritten with values from the config file. Now the name of config file is not dumped if it is equal to the file being written, and the config file is not reparsed if it is the same file. FossilOrigin-Name: 9c4483633c8bcf36f4e98ce41ab0f54ea4620a4b7f21ccf07e563e49afbe0da6 --- shelldap | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/shelldap b/shelldap index 545d071..1604acb 100755 --- a/shelldap +++ b/shelldap @@ -667,8 +667,7 @@ You may try connecting insecurely, or install the module and try again.\n} if $@ print "Would you like to cache your connection information? [Yn]: "; chomp( my $response = ); unless ( $response =~ /^n/i ) { - YAML::Syck::DumpFile( $conf->{'configfile'}, $conf ); - chmod 0600, $conf->{'configfile'}; + main::save_config($conf->{configfile}); print "Connection info cached to $conf->{'configfile'}.\n"; } } @@ -2454,8 +2453,33 @@ sub load_config eval { $conf = YAML::Syck::Load( $data ) }; die "Invalid YAML in $confpath\n" if $@; + # remove reference to itself, if somehow it got dumped + # into YAML. + if( $conf->{configfile} and ($confpath eq $conf->{configfile})) { + delete $conf->{'configfile'} + } + return $conf; } +### dump YAML config into conf file while making sure that +### name of configfile itself is not dumped if it is equal +### to the file being written. +### +sub save_config +{ + my $confpath = shift; + + my %conf2 = %$conf; + if( $confpath eq $conf->{configfile}) { + delete $conf2{'configfile'} + } + + YAML::Syck::DumpFile( $conf->{'configfile'}, \%conf2 ); + chmod 0600, $conf->{'configfile'}; + + return 1; +} + ### EOF