Commit c34f3def authored by Jakob Borg's avatar Jakob Borg

l.FatalErr was an antipattern

parent eb0d7426
......@@ -198,7 +198,9 @@ func main() {
dir := expandTilde(generateDir)
info, err := os.Stat(dir)
l.FatalErr(err)
if err != nil {
l.Fatalln("generate:", err)
}
if !info.IsDir() {
l.Fatalln(dir, "is not a directory")
}
......@@ -212,7 +214,9 @@ func main() {
newCertificate(dir, "")
cert, err = loadCert(dir, "")
l.FatalErr(err)
if err != nil {
l.Fatalln("load cert:", err)
}
if err == nil {
l.Infoln("Node ID:", protocol.NewNodeID(cert.Certificate[0]))
}
......@@ -303,7 +307,9 @@ func syncthingMain() {
if err != nil {
newCertificate(confDir, "")
cert, err = loadCert(confDir, "")
l.FatalErr(err)
if err != nil {
l.Fatalln("load cert:", err)
}
}
myID = protocol.NewNodeID(cert.Certificate[0])
......@@ -352,11 +358,15 @@ func syncthingMain() {
}
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)
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.Save()
......@@ -853,9 +863,13 @@ func listenTLS(conns chan *tls.Conn, addr string, tlsCfg *tls.Config) {
}
tcaddr, err := net.ResolveTCPAddr("tcp", addr)
l.FatalErr(err)
if err != nil {
l.Fatalln("listen (BEP):", err)
}
listener, err := net.ListenTCP("tcp", tcaddr)
l.FatalErr(err)
if err != nil {
l.Fatalln("listen (BEP):", err)
}
for {
conn, err := listener.Accept()
......@@ -1000,7 +1014,9 @@ func ensureDir(dir string, mode int) {
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
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 {
err := os.Chmod(dir, os.FileMode(mode))
// This can fail on crappy filesystems, nothing we can do about it.
......
......@@ -45,7 +45,9 @@ func newCertificate(dir string, prefix string) {
l.Infoln("Generating RSA key and certificate...")
priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
l.FatalErr(err)
if err != nil {
l.Fatalln("generate key:", err)
}
notBefore := time.Now()
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)
l.FatalErr(err)
if err != nil {
l.Fatalln("create cert:", err)
}
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})
l.FatalErr(err)
if err != nil {
l.Fatalln("save cert:", err)
}
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)
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)})
l.FatalErr(err)
if err != nil {
l.Fatalln("save key:", err)
}
err = keyOut.Close()
l.FatalErr(err)
if err != nil {
l.Fatalln("save key:", err)
}
}
type DowngradingListener struct {
......
......@@ -140,14 +140,3 @@ func (l *Logger) Fatalf(format string, vals ...interface{}) {
l.callHandlers(LevelFatal, s)
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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment