feat: write os and date info to config file

This commit is contained in:
Aliberk Sandıkçı 2023-07-17 16:30:17 +03:00
parent 5ecc55cd53
commit 15c8a9930e

62
info.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"log"
"os"
"runtime"
"time"
)
const ProgramName = "go-daemon"
func checkExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func main() {
// A SAMPLE COMMAND FOR LOGGING OS INFORMATION TO A DEDICATED FILE
// runtime.GOOS
userhome, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
configDir := userhome + "/.config/" + ProgramName + "/"
// TODO Will be replaced with a Go Config Library
if flag, err := checkExists(configDir); flag {
log.Println("Config dir already exists")
if err != nil {
log.Fatal(err)
}
} else if err := os.Mkdir(configDir, os.ModePerm); err != nil {
log.Fatal(err)
}
osInfo := []struct {
str1, str2 string
}{
{"OS", runtime.GOOS},
{"Time", time.Now().Format("2006.01.02 15:04:05")},
}
f, err := os.Create(configDir + "daemon.conf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, info := range osInfo {
_, err := f.WriteString(info.str1 + ": " + info.str2 + "\n")
if err != nil {
log.Fatal(err)
}
}
}