pikapika/go/cmd/init.go

58 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
package main
import (
"errors"
"os"
"os/exec"
"path"
path2 "path"
"path/filepath"
2021-10-22 02:24:39 +00:00
"pikapi/main/config"
2021-09-29 23:57:09 +00:00
"runtime"
"strings"
)
func init() {
applicationDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
switch runtime.GOOS {
case "windows":
// applicationDir = path.Join(applicationDir, "AppData", "Roaming")
file, err := exec.LookPath(os.Args[0])
if err != nil {
panic(err)
}
path, err := filepath.Abs(file)
if err != nil {
panic(err)
}
i := strings.LastIndex(path, "/")
if i < 0 {
i = strings.LastIndex(path, "\\")
}
if i < 0 {
panic(errors.New(" can't find \"/\" or \"\\\""))
}
applicationDir = path2.Join(path[0:i+1], "data", "pikapi")
case "darwin":
applicationDir = path.Join(applicationDir, "Library", "Application Support", "pikapi")
case "linux":
applicationDir = path.Join(applicationDir, ".pikapi")
default:
panic(errors.New("not supported system"))
}
if _, err = os.Stat(applicationDir); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(applicationDir, os.FileMode(0700))
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
config.InitApplication(applicationDir)
}