Make Set Value persistent to yaml inventory file

This commit is contained in:
Florian Baumann 2021-12-22 09:41:31 +01:00
parent 8041bf7bc0
commit b97d101ee2
2 changed files with 43 additions and 4 deletions

View File

@ -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
}

View File

@ -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)
}