From d8c4659c2bebb9474c308d10063f56506129d610 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 10 Jan 2017 17:13:12 +0800 Subject: [PATCH] feat: creat pid folder automatically. --- main.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 0f25056..f3be391 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "path/filepath" "strconv" "github.com/appleboy/gorush/config" @@ -65,19 +66,25 @@ func createPIDFile() error { if !gorush.PushConf.Core.PID.Enabled { return nil } - _, err := os.Stat(gorush.PushConf.Core.PID.Path) + + pidPath := gorush.PushConf.Core.PID.Path + _, err := os.Stat(pidPath) if os.IsNotExist(err) || gorush.PushConf.Core.PID.Override { currentPid := os.Getpid() - file, err := os.Create(gorush.PushConf.Core.PID.Path) + if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil { + return fmt.Errorf("Can't create PID folder on %v", err) + } + + file, err := os.Create(pidPath) if err != nil { return fmt.Errorf("Can't create PID file: %v", err) } defer file.Close() if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil { - return fmt.Errorf("Can'write PID information on %s: %v", gorush.PushConf.Core.PID.Path, err) + return fmt.Errorf("Can'write PID information on %s: %v", pidPath, err) } } else { - return fmt.Errorf("%s already exists", gorush.PushConf.Core.PID.Path) + return fmt.Errorf("%s already exists", pidPath) } return nil }