Move all code from cmd/maddy into root package

Build info helpers can be reused by maddyctl.
Same goes for directory configuration.
This commit is contained in:
fox.cpp 2019-11-15 04:23:51 +03:00
parent 7cc505d88c
commit b3a09835de
No known key found for this signature in database
GPG key ID: E76D97CCEDE90B6C
9 changed files with 232 additions and 233 deletions

View file

@ -1,64 +0,0 @@
package main
import (
"os"
"path/filepath"
)
var (
// ConfigDirectory specifies platform-specific value
// that should be used as a location of default configuration
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
ConfigDirectory = "/etc/maddy"
// DefaultStateDirectory specifies platform-specific
// default for StateDirectory.
//
// Most code should use StateDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultStateDirectory = "/var/lib/maddy"
// DefaultRuntimeDirectory specifies platform-specific
// default for RuntimeDirectory.
//
// Most code should use RuntimeDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultRuntimeDirectory = "/run/maddy"
// DefaultLibexecDirectory specifies platform-specific
// default for LibexecDirectory.
//
// Most code should use LibexecDirectory since it will
// contain the effective location of the libexec
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultLibexecDirectory = "/usr/lib/maddy"
)
func ensureDirectoryWritable(path string) error {
if err := os.MkdirAll(path, 0700); err != nil {
return err
}
testFile, err := os.Create(filepath.Join(path, "writeable-test"))
if err != nil {
return err
}
testFile.Close()
if err := os.Remove(testFile.Name()); err != nil {
return err
}
return nil
}

View file

@ -1,127 +1,11 @@
package main
import (
"flag"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/foxcpp/maddy"
"github.com/foxcpp/maddy/config"
"github.com/foxcpp/maddy/config/parser"
"github.com/foxcpp/maddy/log"
)
func main() {
configPath := flag.String("config", filepath.Join(ConfigDirectory, "maddy.conf"), "path to configuration file")
logTargets := flag.String("log", "stderr", "default logging target(s)")
flag.StringVar(&config.StateDirectory, "state", DefaultStateDirectory, "path to the state directory")
flag.StringVar(&config.LibexecDirectory, "libexec", DefaultLibexecDirectory, "path to the libexec directory")
flag.StringVar(&config.RuntimeDirectory, "runtime", DefaultRuntimeDirectory, "path to the runtime directory")
flag.BoolVar(&log.DefaultLogger.Debug, "debug", false, "enable debug logging")
profileEndpoint := flag.String("debug.pprof", "", "enable live profiler HTTP endpoint and listen on the specified endpoint")
blockProfileRate := flag.Int("debug.blockprofrate", 0, "set blocking profile rate")
mutexProfileFract := flag.Int("debug.mutexproffract", 0, "set mutex profile fraction)")
printVersion := flag.Bool("v", false, "print version and exit")
flag.Parse()
if *printVersion {
printBuildInfo()
return
}
var err error
log.DefaultLogger.Out, err = maddy.LogOutputOption(strings.Split(*logTargets, " "))
if err != nil {
log.Println(err)
os.Exit(2)
}
if err := ensureDirectoryWritable(config.StateDirectory); err != nil {
log.Println(err)
os.Exit(2)
}
if err := ensureDirectoryWritable(config.RuntimeDirectory); err != nil {
log.Println(err)
os.Exit(2)
}
if *profileEndpoint != "" {
go func() {
log.Println("listening on", "http://"+*profileEndpoint, "for profiler requests")
log.Println("failed to listen on profiler endpoint:", http.ListenAndServe(*profileEndpoint, nil))
}()
}
// These values can also be affected by environment so set them
// only if argument is specified.
if *mutexProfileFract != 0 {
runtime.SetMutexProfileFraction(*mutexProfileFract)
}
if *blockProfileRate != 0 {
runtime.SetBlockProfileRate(*blockProfileRate)
}
f, err := os.Open(*configPath)
if err != nil {
log.Printf("cannot open %q: %v\n", *configPath, err)
os.Exit(1)
}
defer f.Close()
// Make sure all paths we are going to use are absolute
// before we change the working directory.
config.StateDirectory, err = filepath.Abs(config.StateDirectory)
if err != nil {
log.Println(err)
os.Exit(1)
}
config.LibexecDirectory, err = filepath.Abs(config.LibexecDirectory)
if err != nil {
log.Println(err)
os.Exit(1)
}
config.RuntimeDirectory, err = filepath.Abs(config.RuntimeDirectory)
if err != nil {
log.Println(err)
os.Exit(1)
}
// Make directory constants accessible in configuration by using
// environment variables expansion.
if err := os.Setenv("MADDYSTATE", config.StateDirectory); err != nil {
log.Println(err)
}
if err := os.Setenv("MADDYLIBEXEC", config.LibexecDirectory); err != nil {
log.Println(err)
}
if err := os.Setenv("MADDYRUNTIME", config.RuntimeDirectory); err != nil {
log.Println(err)
}
cfg, err := parser.Read(f, *configPath)
if err != nil {
log.Printf("cannot parse %q: %v\n", *configPath, err)
os.Exit(1)
}
// Change the working directory to make all relative paths
// in configuration relative to state directory.
if err := os.Chdir(config.StateDirectory); err != nil {
log.Println(err)
os.Exit(1)
}
if err := maddy.Start(cfg); err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(maddy.Run())
}

View file

@ -1,25 +0,0 @@
package main
import (
"fmt"
"runtime/debug"
)
var Version = "unknown (built from source tree)"
func printBuildInfo() {
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version == "(devel)" {
fmt.Println("maddy", Version)
} else {
fmt.Println("maddy", info.Main.Version, info.Main.Sum)
}
} else {
fmt.Println("maddy", Version)
fmt.Println()
fmt.Println("Building maddy in GOPATH mode can lead to wrong dependency")
fmt.Println("versions being used. Problems created by this will not be")
fmt.Println("addressed. Make sure you are building in Module Mode")
fmt.Println("(see README for details)")
}
}

View file

@ -4,8 +4,10 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/emersion/go-imap/backend"
"github.com/foxcpp/maddy"
"github.com/urfave/cli"
"golang.org/x/crypto/bcrypt"
)
@ -28,20 +30,20 @@ func main() {
app := cli.NewApp()
app.Name = "maddyctl"
app.Usage = "maddy mail server administration utility"
app.Version = buildInfo()
app.Version = maddy.BuildInfo()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Configuration file to use",
EnvVar: "MADDY_CONFIG",
Value: "/etc/maddy/maddy.conf",
Value: filepath.Join(maddy.ConfigDirectory, "maddy.conf"),
},
cli.StringFlag{
Name: "state",
Usage: "State directory to use",
EnvVar: "MADDY_STATE",
Value: "/var/lib/maddy",
Value: maddy.DefaultStateDirectory,
},
}

View file

@ -1,18 +0,0 @@
package main
import (
"fmt"
"runtime/debug"
)
var Version = "unknown (built from source tree)"
func buildInfo() string {
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version == "(devel)" {
return Version
}
return fmt.Sprintf("%s %s", info.Main.Version, info.Main.Sum)
}
return Version
}