]> git.refcnt.org Git - distdns.git/commitdiff
Move config data to files
authorSteven Schubiger <stsc@refcnt.org>
Sun, 1 Sep 2013 16:37:24 +0000 (18:37 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Sun, 1 Sep 2013 16:37:24 +0000 (18:37 +0200)
client.conf [new file with mode: 0644]
client.pl
dynuser.conf [deleted file]
server.cgi
server.conf [new file with mode: 0644]

diff --git a/client.conf b/client.conf
new file mode 100644 (file)
index 0000000..354a64f
--- /dev/null
@@ -0,0 +1,10 @@
+[path]
+hosts_file   = hosts
+session_file = session.dat
+
+[url]
+server_url = http://refcnt.org/~sts/cgi-bin/ketterle/server.cgi
+
+[data]
+netz = netz1
+name = name1
index 917eec30f1e90608d17e25fc11f9e9e9407fd4af..1d03898ac44b17cbe201696c8364aa04cf23f8a2 100755 (executable)
--- a/client.pl
+++ b/client.pl
@@ -24,27 +24,17 @@ use constant false => 0;
 use Config::Tiny;
 use Digest::MD5 qw(md5_hex);
 use Fcntl ':flock';
-use File::Spec::Functions qw(rel2abs);
+use File::Spec::Functions qw(catfile rel2abs);
+use FindBin qw($Bin);
 use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
 use JSON qw(decode_json);
 use LWP::UserAgent;
 use Sys::Hostname qw(hostname);
 use Tie::File;
 
-my $VERSION = '0.04';
+my $VERSION = '0.05';
 
-#-----------------------
-# Start of configuration
-#-----------------------
-
-my $config_file  = 'dynuser.conf';
-my $hosts_file   = 'hosts';
-my $session_file = 'session.dat';
-my $server_url   = 'http://refcnt.org/~sts/cgi-bin/ketterle/server.cgi';
-
-#---------------------
-# End of configuration
-#---------------------
+my $conf_file = catfile($Bin, 'client.conf');
 
 sub usage
 {
@@ -61,9 +51,29 @@ my %opts;
 GetOptions(\%opts, qw(d|debug h|help i|init)) or usage();
 usage() if $opts{h};
 
-$config_file  = rel2abs($config_file);
-$hosts_file   = rel2abs($hosts_file);
-$session_file = rel2abs($session_file);
+my $config = Config::Tiny->new;
+   $config = Config::Tiny->read($conf_file);
+
+my $get_config_opts = sub
+{
+    my ($section, $options) = @_;
+
+    die "$0: Section $section missing in $conf_file\n" unless exists $config->{$section};
+
+    my %options;
+    @options{@$options} = @{$config->{$section}}{@$options};
+
+    foreach my $option (@$options) {
+        die "$0: Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
+    }
+
+    return @options{@$options};
+};
+
+my ($hosts_file, $session_file) = map rel2abs($_, $Bin), $get_config_opts->('path', [ qw(hosts_file session_file) ]);
+
+my ($server_url)  = $get_config_opts->('url',  [ qw(server_url) ]);
+my ($netz, $name) = $get_config_opts->('data', [ qw(netz name)  ]);
 
 my $save_session = sub
 {
@@ -86,13 +96,6 @@ my $get_session = sub
 
 my $session = $opts{i} ? substr(md5_hex(md5_hex(time() . {} . rand() . $$)), 0, 32) : $get_session->();
 
-my $config = Config::Tiny->new;
-   $config = Config::Tiny->read($config_file);
-
-my ($netz, $name) = @{$config->{data}}{qw(netz name)};
-
-die "$0: Network and/or name not set in $config_file\n" unless defined $netz && defined $name;
-
 my %params = (
     netz    => $netz,
     pc      => hostname(),
diff --git a/dynuser.conf b/dynuser.conf
deleted file mode 100644 (file)
index 87d130a..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-[data]
-netz = netz1
-name = name1
index f52f3d93ff7ceb0ab16f8c48191f2d09f7381f64..bdd9ac729bdc84bd4c3c013f28b5d3b055ac261a 100755 (executable)
 
 use strict;
 use warnings;
+use lib qw(lib);
 
 use CGI ();
+use Config::Tiny ();
 use Fcntl ':flock';
+use File::Spec::Functions qw(catfile rel2abs);
+use FindBin qw($Bin);
 use JSON qw(decode_json encode_json);
 
-my $VERSION = '0.04';
+my $VERSION = '0.05';
 
-#-----------------------
-# Start of configuration
-#-----------------------
-
-my $json_file    = 'data.json';
-my $session_file = 'session.dat';
-
-#---------------------
-# End of configuration
-#---------------------
+my $conf_file = catfile($Bin, 'server.conf');
 
 my $query = CGI->new;
 
@@ -56,6 +51,24 @@ if ($params{debug}) {
     };
 }
 
+my $config = Config::Tiny->new;
+   $config = Config::Tiny->read($conf_file);
+
+my $section = 'path';
+
+die "Section $section missing in $conf_file\n" unless exists $config->{$section};
+
+my @options = qw(json_file session_file);
+
+my %options;
+@options{@options} = @{$config->{$section}}{@options};
+
+foreach my $option (@options) {
+    die "Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
+}
+
+my ($json_file, $session_file) = map rel2abs($options{$_}, $Bin), @options;
+
 if ($params{init}) {
     die "Delete server-side $session_file first\n" if -e $session_file;
 
diff --git a/server.conf b/server.conf
new file mode 100644 (file)
index 0000000..d4f0c20
--- /dev/null
@@ -0,0 +1,3 @@
+[path]
+json_file    = data.json
+session_file = session.dat