From b97d101ee28d999ee52c22c6aea5cc9c3b756cd3 Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Wed, 22 Dec 2021 09:41:31 +0100 Subject: [PATCH] Make Set Value persistent to yaml inventory file --- src/archivar/setfile.go | 28 +++++++++++++++++++++++++--- src/archivar/start.go | 19 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/archivar/setfile.go b/src/archivar/setfile.go index dc46309..3d5a304 100644 --- a/src/archivar/setfile.go +++ b/src/archivar/setfile.go @@ -11,10 +11,16 @@ import ( type Setfile struct { Description string `description` Cards []string `cards` + Value []Value `value` +} + +type Value struct { + Date string + Value float32 } // Read formatted yaml file -func (c *Setfile) ReadFile(path string) *Setfile { +func (s *Setfile) ReadFile(path string) *Setfile { yamlFile, err := ioutil.ReadFile(path) if err != nil { @@ -22,11 +28,27 @@ func (c *Setfile) ReadFile(path string) *Setfile { os.Exit(1) } - err = yaml.Unmarshal(yamlFile, c) + err = yaml.Unmarshal(yamlFile, s) if err != nil { LogMessage(fmt.Sprintf("Unmarshal %v", err), "red") os.Exit(1) } - return c + return s +} + +func (s *Setfile) Write(path string) *Setfile { + data, err := yaml.Marshal(*s) + if err != nil { + LogMessage(fmt.Sprintf("Marshal %v", err), "red") + os.Exit(1) + } + + err = ioutil.WriteFile(path, data, 0644) + if err != nil { + LogMessage("Could not write file", "red") + os.Exit(1) + } + + return s } diff --git a/src/archivar/start.go b/src/archivar/start.go index 5b978e4..7131be2 100644 --- a/src/archivar/start.go +++ b/src/archivar/start.go @@ -2,6 +2,7 @@ package archivar import ( "fmt" + "strconv" "time" ) @@ -13,17 +14,33 @@ const ( func Start(set_file string) { var s Setfile + var total float32 + s.ReadFile(set_file) LogMessage(fmt.Sprintf("Archivar %v\n", version), "green") - fmt.Printf("Your Challenge: %s\n", s.Description) + fmt.Printf("Set: %s\n", s.Description) // Loop over different challenges for _, entry := range s.Cards { card := fetch(entry) fmt.Println(card) + t, _ := strconv.ParseFloat(card.Prices.Eur.(string), 32) + total = total + float32(t) time.Sleep(100 * time.Millisecond) } + // build new valueset + v := &Value{} + v.Date = time.Now().Format("2006-01-02 15:04:05") + v.Value = total + + // add new valueset to set + s.Value = append(s.Value, *v) + + LogMessage(fmt.Sprintf("Total value in this set %.2f", total), "green") + + s.Write(set_file) + }