From 15c8a9930e071c060ce7d0e504788ceebaf7d6c0 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 17 Jul 2023 16:30:17 +0300 Subject: [PATCH] feat: write os and date info to config file --- info.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 info.go diff --git a/info.go b/info.go new file mode 100644 index 0000000..bb4179a --- /dev/null +++ b/info.go @@ -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) + } + } +}