tcms-hostname 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use FindBin::libs;
  6. use Trog::Config();
  7. use Getopt::Long qw{GetOptionsFromArray};
  8. =head1 SYNOPSIS
  9. Get, or set, the hostname this tCMS instance serves.
  10. Everything which needs to know the site's domain -- TOTP enrollment, the DNS
  11. zone, generated URLs -- reads it out of the configuration, so this is where it
  12. gets put there in the first place.
  13. =head2 USAGE
  14. bin/tcms-hostname # print the configured hostname
  15. bin/tcms-hostname example.com # set it, then print it
  16. bin/tcms-hostname --user # print it as a valid username
  17. =head2 OPTIONS
  18. =over 4
  19. =item --user
  20. Print the hostname mangled into something usable as a system username: dots
  21. become dashes, and it's truncated to 32 characters. Does not change what is
  22. stored in the configuration.
  23. =back
  24. =head2 CAVEATS
  25. Writes to $Trog::Config::home_cfg rather than saving over whatever get()
  26. loaded, as that is the tracked config/default.cfg until the instance has a
  27. configuration of its own.
  28. Dies if asked to print a hostname which was never set.
  29. =cut
  30. my $user;
  31. GetOptionsFromArray(
  32. \@ARGV,
  33. 'user' => \$user,
  34. );
  35. my $hostname = $ARGV[0];
  36. my $conf = Trog::Config->get();
  37. if ($hostname) {
  38. $conf->param( 'general.hostname', $hostname );
  39. # write(), not save(). save() writes back to whatever file get() loaded,
  40. # which is the tracked config/default.cfg until the instance has saved a
  41. # configuration of its own.
  42. $conf->write($Trog::Config::home_cfg) or die "Could not write $Trog::Config::home_cfg";
  43. }
  44. my $domain = $conf->param('general.hostname');
  45. die "Hostname not set in tCMS configuration. Please set this first by passing the hostname to bin/tcms-hostname." unless $domain;
  46. # Transform the domain name into something maximally compatible with being a username.
  47. if ($user) {
  48. $domain = substr( $domain, 0, 32 );
  49. $domain =~ s/\./-/g;
  50. }
  51. print "$domain\n";