From c6fbbbded8463fc94e5af223246eac97d538a9e5 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 4 May 2024 20:22:23 +0200 Subject: model: Add storage.Exists() and storage.Create() These new methods provide essential functionality related to the storage folder. --- main.go | 3 +++ model/storage.go | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 937fa8e..da06f8e 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,9 @@ func main() { } storage := model.Storage{Path: config.StorageFilePath} + if !storage.Exists() { + storage.Create() + } storage.Init(gitDescribe) model.InitDatabase(config.StorageFilePath) diff --git a/model/storage.go b/model/storage.go index fcec125..5450c60 100644 --- a/model/storage.go +++ b/model/storage.go @@ -10,17 +10,22 @@ type Storage struct { Path string } -func (s *Storage) Init(version string) { +func (s Storage) Exists() bool { + _, err := os.Stat(s.Path) + return !os.IsNotExist(err) +} + +func (s Storage) Create() { err := os.Mkdir(s.Path, 0750) - if err != nil && !os.IsExist(err) { + if err != nil { log.Fatal(err) } +} - log.Printf("Created storage folder %s\n", s.Path) - +func (s *Storage) Init(version string) { if version != "" { vp := filepath.Join(s.Path, "version.txt") - err = os.WriteFile(vp, []byte(version+"\n"), 0644) + err := os.WriteFile(vp, []byte(version+"\n"), 0644) if err != nil { log.Fatal(err) } -- cgit v1.2.3-70-g09d2