github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/utils/session.go (about) 1 package utils 2 3 import ( 4 "github.com/pkg/errors" 5 "os" 6 "path" 7 ) 8 9 // create and get session directory 10 func GetSessionDir(workDir string, SessionID string) (string, error) { 11 _, err := os.Stat(workDir) 12 if os.IsNotExist(err) { 13 return "", errors.Errorf("work dir (%s) not exists", workDir) 14 } else if err != nil { 15 return "", err 16 } 17 sessionDir := path.Join(workDir, SessionID) 18 s, err := os.Stat(sessionDir) 19 if s != nil || os.IsExist(err) { 20 _ = os.RemoveAll(sessionDir) 21 } 22 err = os.Mkdir(sessionDir, 0755) 23 if err != nil { 24 return "", errors.Errorf("create session dir error: %s", err.Error()) 25 } 26 return sessionDir, nil 27 }