tcms-hostname 1.9 KB

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