strip all yaml out of serra

This commit is contained in:
Florian Baumann 2021-12-27 11:21:28 +01:00
parent a60dcaabc3
commit 0d7abab166
5 changed files with 17 additions and 114 deletions

View File

@ -12,7 +12,7 @@ func main() {
usage := `Archivar usage := `Archivar
Usage: Usage:
serra new <path>... serra add <path>...
serra update <path>... serra update <path>...
serra value <path>... serra value <path>...
@ -23,13 +23,8 @@ Options:
args, _ := docopt.ParseDoc(usage) args, _ := docopt.ParseDoc(usage)
if args["new"].(bool) { if args["add"].(bool) {
serra.New(args["<path>"].(string)) serra.Add(args["<path>"].([]string))
}
if args["update"].(bool) {
for _, i := range args["<path>"].([]string) {
serra.Update(i)
}
} }
} }

View File

@ -3,6 +3,7 @@ package serra
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -113,28 +114,29 @@ type Card struct {
Variation bool `json:"variation"` Variation bool `json:"variation"`
} }
func fetch(path string) (*Card, bool) { func fetch(path string) (*Card, error) {
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path)) resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path))
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
return &Card{}, false return &Card{}, err
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
LogMessage(fmt.Sprintf("Card %s not found", path), "yellow") LogMessage(fmt.Sprintf("Card %s not found", path), "yellow")
return &Card{}, false err := errors.New("card: not found")
return &Card{}, err
} }
//We Read the response body on the line below. //We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
return &Card{}, false return &Card{}, err
} }
r := bytes.NewReader(body) r := bytes.NewReader(body)
decoder := json.NewDecoder(r) decoder := json.NewDecoder(r)
val := &Card{} val := &Card{}
err = decoder.Decode(val) err = decoder.Decode(val)
return val, true return val, nil
} }

View File

@ -2,57 +2,23 @@ package serra
import ( import (
"fmt" "fmt"
"strconv"
"time"
) )
const ( const (
version string = "0.0.1" version string = "0.0.1"
) )
// Create new set file // Add
func New(set_file string) { func Add(cards []string) {
var s Setfile
s.Write(set_file)
}
// Update values and names in a setfile
func Update(set_file string) {
var s Setfile
var total float32
s.ReadFile(set_file)
LogMessage(fmt.Sprintf("Archivar %v\n", version), "green") LogMessage(fmt.Sprintf("Archivar %v\n", version), "green")
fmt.Printf("Set: %s\n", s.Description)
// Loop over different challenges // Loop over different challenges
for _, entry := range s.Cards { for _, card := range cards {
card, ok := fetch(entry) c, err := fetch(card)
if err != nil {
// catch empty cards LogMessage(fmt.Sprintf("%v", err), "red")
if ok == false {
continue
} }
fmt.Println(c)
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)
} }

View File

@ -1,6 +0,0 @@
package serra
type Set struct {
Description string `description`
Cards []string `cards`
}

View File

@ -1,54 +0,0 @@
package serra
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
type Setfile struct {
Description string `description`
Cards []string `cards`
Value []Value `value`
}
type Value struct {
Date string
Value float32
}
// Read formatted yaml file
func (s *Setfile) ReadFile(path string) *Setfile {
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
LogMessage("Could not open file", "red")
os.Exit(1)
}
err = yaml.Unmarshal(yamlFile, s)
if err != nil {
LogMessage(fmt.Sprintf("Unmarshal %v", err), "red")
os.Exit(1)
}
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
}