strip all yaml out of serra
This commit is contained in:
parent
a60dcaabc3
commit
0d7abab166
11
serra.go
11
serra.go
@ -12,7 +12,7 @@ func main() {
|
||||
usage := `Archivar
|
||||
|
||||
Usage:
|
||||
serra new <path>...
|
||||
serra add <path>...
|
||||
serra update <path>...
|
||||
serra value <path>...
|
||||
|
||||
@ -23,13 +23,8 @@ Options:
|
||||
|
||||
args, _ := docopt.ParseDoc(usage)
|
||||
|
||||
if args["new"].(bool) {
|
||||
serra.New(args["<path>"].(string))
|
||||
}
|
||||
if args["update"].(bool) {
|
||||
for _, i := range args["<path>"].([]string) {
|
||||
serra.Update(i)
|
||||
}
|
||||
if args["add"].(bool) {
|
||||
serra.Add(args["<path>"].([]string))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package serra
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@ -113,28 +114,29 @@ type Card struct {
|
||||
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))
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return &Card{}, false
|
||||
return &Card{}, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
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.
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return &Card{}, false
|
||||
return &Card{}, err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(body)
|
||||
decoder := json.NewDecoder(r)
|
||||
val := &Card{}
|
||||
err = decoder.Decode(val)
|
||||
return val, true
|
||||
return val, nil
|
||||
}
|
||||
|
||||
@ -2,57 +2,23 @@ package serra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
version string = "0.0.1"
|
||||
)
|
||||
|
||||
// Create new set file
|
||||
func New(set_file 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)
|
||||
|
||||
// Add
|
||||
func Add(cards []string) {
|
||||
LogMessage(fmt.Sprintf("Archivar %v\n", version), "green")
|
||||
|
||||
fmt.Printf("Set: %s\n", s.Description)
|
||||
|
||||
// Loop over different challenges
|
||||
for _, entry := range s.Cards {
|
||||
card, ok := fetch(entry)
|
||||
|
||||
// catch empty cards
|
||||
if ok == false {
|
||||
continue
|
||||
for _, card := range cards {
|
||||
c, err := fetch(card)
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||
}
|
||||
|
||||
t, _ := strconv.ParseFloat(card.Prices.Eur.(string), 32)
|
||||
total = total + float32(t)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
package serra
|
||||
|
||||
type Set struct {
|
||||
Description string `description`
|
||||
Cards []string `cards`
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user