l.FatalErr was an antipattern

This commit is contained in:
Jakob Borg 2014-09-20 15:42:20 +02:00
parent eb0d742672
commit c34f3defe1
3 changed files with 48 additions and 27 deletions

View File

@ -198,7 +198,9 @@ func main() {
dir := expandTilde(generateDir) dir := expandTilde(generateDir)
info, err := os.Stat(dir) info, err := os.Stat(dir)
l.FatalErr(err) if err != nil {
l.Fatalln("generate:", err)
}
if !info.IsDir() { if !info.IsDir() {
l.Fatalln(dir, "is not a directory") l.Fatalln(dir, "is not a directory")
} }
@ -212,7 +214,9 @@ func main() {
newCertificate(dir, "") newCertificate(dir, "")
cert, err = loadCert(dir, "") cert, err = loadCert(dir, "")
l.FatalErr(err) if err != nil {
l.Fatalln("load cert:", err)
}
if err == nil { if err == nil {
l.Infoln("Node ID:", protocol.NewNodeID(cert.Certificate[0])) l.Infoln("Node ID:", protocol.NewNodeID(cert.Certificate[0]))
} }
@ -303,7 +307,9 @@ func syncthingMain() {
if err != nil { if err != nil {
newCertificate(confDir, "") newCertificate(confDir, "")
cert, err = loadCert(confDir, "") cert, err = loadCert(confDir, "")
l.FatalErr(err) if err != nil {
l.Fatalln("load cert:", err)
}
} }
myID = protocol.NewNodeID(cert.Certificate[0]) myID = protocol.NewNodeID(cert.Certificate[0])
@ -352,11 +358,15 @@ func syncthingMain() {
} }
port, err := getFreePort("127.0.0.1", 8080) port, err := getFreePort("127.0.0.1", 8080)
l.FatalErr(err) if err != nil {
l.Fatalln("get free port (GUI):", err)
}
cfg.GUI.Address = fmt.Sprintf("127.0.0.1:%d", port) cfg.GUI.Address = fmt.Sprintf("127.0.0.1:%d", port)
port, err = getFreePort("0.0.0.0", 22000) port, err = getFreePort("0.0.0.0", 22000)
l.FatalErr(err) if err != nil {
l.Fatalln("get free port (BEP):", err)
}
cfg.Options.ListenAddress = []string{fmt.Sprintf("0.0.0.0:%d", port)} cfg.Options.ListenAddress = []string{fmt.Sprintf("0.0.0.0:%d", port)}
cfg.Save() cfg.Save()
@ -853,9 +863,13 @@ func listenTLS(conns chan *tls.Conn, addr string, tlsCfg *tls.Config) {
} }
tcaddr, err := net.ResolveTCPAddr("tcp", addr) tcaddr, err := net.ResolveTCPAddr("tcp", addr)
l.FatalErr(err) if err != nil {
l.Fatalln("listen (BEP):", err)
}
listener, err := net.ListenTCP("tcp", tcaddr) listener, err := net.ListenTCP("tcp", tcaddr)
l.FatalErr(err) if err != nil {
l.Fatalln("listen (BEP):", err)
}
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
@ -1000,7 +1014,9 @@ func ensureDir(dir string, mode int) {
fi, err := os.Stat(dir) fi, err := os.Stat(dir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
err := os.MkdirAll(dir, 0700) err := os.MkdirAll(dir, 0700)
l.FatalErr(err) if err != nil {
l.Fatalln(err)
}
} else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode { } else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode {
err := os.Chmod(dir, os.FileMode(mode)) err := os.Chmod(dir, os.FileMode(mode))
// This can fail on crappy filesystems, nothing we can do about it. // This can fail on crappy filesystems, nothing we can do about it.

View File

@ -45,7 +45,9 @@ func newCertificate(dir string, prefix string) {
l.Infoln("Generating RSA key and certificate...") l.Infoln("Generating RSA key and certificate...")
priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits) priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
l.FatalErr(err) if err != nil {
l.Fatalln("generate key:", err)
}
notBefore := time.Now() notBefore := time.Now()
notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC) notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
@ -64,21 +66,35 @@ func newCertificate(dir string, prefix string) {
} }
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
l.FatalErr(err) if err != nil {
l.Fatalln("create cert:", err)
}
certOut, err := os.Create(filepath.Join(dir, prefix+"cert.pem")) certOut, err := os.Create(filepath.Join(dir, prefix+"cert.pem"))
l.FatalErr(err) if err != nil {
l.Fatalln("save cert:", err)
}
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
l.FatalErr(err) if err != nil {
l.Fatalln("save cert:", err)
}
err = certOut.Close() err = certOut.Close()
l.FatalErr(err) if err != nil {
l.Fatalln("save cert:", err)
}
keyOut, err := os.OpenFile(filepath.Join(dir, prefix+"key.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) keyOut, err := os.OpenFile(filepath.Join(dir, prefix+"key.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
l.FatalErr(err) if err != nil {
l.Fatalln("save key:", err)
}
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}) err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
l.FatalErr(err) if err != nil {
l.Fatalln("save key:", err)
}
err = keyOut.Close() err = keyOut.Close()
l.FatalErr(err) if err != nil {
l.Fatalln("save key:", err)
}
} }
type DowngradingListener struct { type DowngradingListener struct {

View File

@ -140,14 +140,3 @@ func (l *Logger) Fatalf(format string, vals ...interface{}) {
l.callHandlers(LevelFatal, s) l.callHandlers(LevelFatal, s)
os.Exit(1) os.Exit(1)
} }
func (l *Logger) FatalErr(err error) {
if err != nil {
l.mut.Lock()
defer l.mut.Unlock()
l.logger.SetFlags(l.logger.Flags() | log.Lshortfile)
l.logger.Output(2, "FATAL: "+err.Error())
l.callHandlers(LevelFatal, err.Error())
os.Exit(1)
}
}