hozonsite/config.go

68 行
2.0 KiB
Go
Raw 通常表示 履歴

2023-05-10 22:47:27 +09:00
package main
import (
"os"
2023-05-10 22:47:27 +09:00
"fmt"
"runtime"
"encoding/json"
"io/ioutil"
"errors"
2023-05-10 22:47:27 +09:00
)
type Config struct {
configpath, webpath, datapath, domain, ip string
2023-05-10 22:47:27 +09:00
}
func getconf () (Config, error) {
2023-05-10 22:47:27 +09:00
var cnf Config
2023-06-27 13:48:35 +09:00
// バイナリ、データ、及びFreeBSDとNetBSDの場合、コンフィグ
2023-05-10 22:47:27 +09:00
prefix := "/usr"
2023-06-27 13:48:35 +09:00
// BSDだけはただの/usrではない
2023-05-10 22:47:27 +09:00
if runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" {
prefix += "/local"
2023-06-23 12:43:13 +09:00
} else if runtime.GOOS == "netbsd" {
prefix += "/pkg"
2023-05-10 22:47:27 +09:00
}
2023-06-27 13:48:35 +09:00
// コンフィグファイル
2023-05-14 11:44:09 +09:00
cnf.configpath = "/etc/hozonsite/config.json"
2023-05-10 22:47:27 +09:00
cnf.datapath = prefix + "/share/hozonsite"
2023-06-27 13:48:35 +09:00
// また、FreeBSDとNetBSDだけは違う場所だ。OpenBSDは正しい場所
// FreeBSD = /usr/local/etc/hozonsite/config.json
// NetBSD = /usr/pkg/etc/hozonsite/config.json
2023-06-23 12:43:13 +09:00
if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" {
2023-05-10 22:47:27 +09:00
cnf.configpath = prefix + cnf.configpath
}
2023-06-27 13:48:35 +09:00
// コンフィグファイルがなければ、死ね
data, err := ioutil.ReadFile(cnf.configpath)
2023-05-10 22:47:27 +09:00
if err != nil {
fmt.Println("confif.jsonを開けられません", err)
return cnf, errors.New("コンフィグファイルは " + cnf.configpath + " に創作して下さい。")
2023-05-10 22:47:27 +09:00
}
var payload map[string]interface{}
2023-05-10 22:47:27 +09:00
json.Unmarshal(data, &payload)
if payload["webpath"] == nil {
return cnf, errors.New("「webpath」の値が設置していません。")
}
if payload["domain"] == nil {
return cnf, errors.New("「domain」の値が設置していません。")
}
if payload["ip"] == nil {
return cnf, errors.New("「ip」の値が設置していません。")
}
if _, err := os.Stat(payload["webpath"].(string)); err != nil {
fmt.Printf("%v\n", err)
return cnf, errors.New("mkdiorコマンドをつかって、 " + payload["webpath"].(string))
}
2023-06-27 13:48:35 +09:00
cnf.webpath = payload["webpath"].(string) // データパス
cnf.domain = payload["domain"].(string) // ドメイン名
cnf.ip = payload["ip"].(string) // IP
2023-06-27 13:48:35 +09:00
payload = nil // もういらなくなった
2023-05-10 22:47:27 +09:00
return cnf, nil
2023-05-10 22:47:27 +09:00
}