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 // userhome, err := os.UserHomeDir() // TODO $HOME is not defined while running as daemon !!! // if err != nil { // log.Fatal(err) // } userhome := "/home/asandikci" // TEMPORARY 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) } } }