~reesmichael1/chantpointer

ref: 0ba42d0dff660f88ca66c3b97a6fed56d37721a4 chantpointer/saved.go -rw-r--r-- 1.0 KiB View raw
0ba42d0dMichael Rees Use .ssh/config as secret 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import (
	"bytes"
	"encoding/json"
	"io"
)

// Saved holds the user input from a previous run
type Saved struct {
	ScorePath string
	ScoreName string
	Chant     string
	Title     string
	Subtitle  string
}

// LoadSaved loads the saved chant from a given io.Reader
func LoadSaved(chant io.Reader) Saved {
	var savedMap map[string]string
	buf := bytes.NewBuffer([]byte{})
	buf.ReadFrom(chant)
	json.Unmarshal(buf.Bytes(), &savedMap)
	return Saved{
		Title:     savedMap["title"],
		Subtitle:  savedMap["subtitle"],
		ScorePath: savedMap["score"],
		ScoreName: savedMap["scoreName"],
		Chant:     savedMap["chant"],
	}
}

// SaveInput saves the submitted input to a given io.Writer
func SaveInput(f io.Writer, title, subtitle, chant, scoreName, scorePath string) error {
	jsMap := map[string]string{
		"title":     title,
		"subtitle":  subtitle,
		"chant":     chant,
		"scoreName": scoreName,
		"score":     scorePath,
	}
	js, err := json.Marshal(jsMap)
	if err != nil {
		return err
	}

	_, err = f.Write(js)
	return err
}