summaryrefslogtreecommitdiff
path: root/pinyadmin/sbin/pinyconfig
blob: 233bc5c05ab7d1eabdd201954bf1862184c1c34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
#!/usr/bin/perl

use strict;
use warnings;

use Piny;

use Getopt::Long qw(:config bundling auto_abbrev);

my ( $fast, $user, $help ) = ( 0, 0, 0 );

GetOptions (
  "fast" => \$fast,
  "user" => \$user,
  "help" => \$help
);

my ( $reponame, $attr, $value ) = @ARGV;

sub usage {
  print <<EOF;
Usage: $0 [--fast] <reponame|--user> [attribute] [value]
Summary of options:
  --fast, -f    Skip rebuild
  --user, -u    Configure user
EOF

  exit ( 1 );
};

if ( $help ) {
  &usage;
};

my $repo;
my $config;
my $env = Piny::Environment->instance( );

if ( $user ) {
  ( $attr, $value ) = @ARGV;
  $config = $env->user->config;
} else {
  ( $reponame, $attr, $value ) = @ARGV;
  if ( not defined $reponame ) {
    &usage;
  };
  $repo = Piny::Repo->new( $reponame );
  $config = $repo->config;
};

if ( not defined $attr ) {

  my $tweakables = $config->all_tweakables;

  foreach my $t ( sort keys %$tweakables ) {
    my $v = $tweakables->{$t};
    if ( defined $v ) {
      print "$t => $v\n";
    } else {
      print "$t undefined\n";
    };
  };

} else {

  $attr = lc $attr;
  $attr =~ s/\./_/g;

  if ( defined $value ) {
    unless ( $env->user->uid == 0 or $user or $repo->owner->uid == $env->user->uid ) {
      print "You are not the owner of that repo!\n";
      exit( 3 );
    };

    undef $@;
    eval {
      $config->$attr( $value );
    };
    if ( $@ ) {
      print STDERR "$attr is not a legal tweakable, or $value is not a legal value for that tweakable.\n$@\n";
      exit( 4 );
    };
    if ( $value ne $config->$attr ) {
      print STDERR "Failed to set $attr (perhaps an override is in place)\n";
      exit( 5 );
    };
    if ( $attr =~ /sharedrepository|ikiwiki/i and not $user ) { # FIXME: define in Config.pm instead of here
      $repo->rebuild;
    };
  };

  undef $@;
  eval {
    print "$attr = " . $config->$attr . "\n";
  };
  if ( $@ ) {
    print STDERR "$attr is not a legal tweakable, or its current value is illegal.\n$@\n";
    exit( 6 );
  };

};