Fix precedence of command line arguments
authorDavor Ocelic <docelic@crystallabs.io>
Sun, 28 Apr 2019 22:11:46 +0200
changeset 102 e92cb378144d
parent 101 cb4de2ecc7ab
child 103 20752fcf201c
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.
shelldap
--- a/shelldap	Sun Apr 28 21:40:06 2019 +0200
+++ b/shelldap	Sun Apr 28 22:11:46 2019 +0200
@@ -667,8 +667,7 @@
 		print "Would you like to cache your connection information? [Yn]: ";
 		chomp( my $response = <STDIN> );
 		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 @@
 	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