fix: set config, set remote
This commit is contained in:
48
pkg/types/types.go
Normal file
48
pkg/types/types.go
Normal file
@ -0,0 +1,48 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
Remotes []*Remote `json:"remotes"`
|
||||
}
|
||||
|
||||
func (c *Config) ToJSON() (string, error) {
|
||||
var b bytes.Buffer
|
||||
err := c.JSONWriter(&b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
// JSONWriter needs a writer to write the struct into json string
|
||||
func (c *Config) JSONWriter(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
err := encoder.Encode(&c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error in encoding struct to json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JSONDecoder decode a json string from a reader into a struct
|
||||
func (c *Config) JSONDecoder(r io.Reader) error {
|
||||
jsonDecoder := json.NewDecoder(r)
|
||||
if err := jsonDecoder.Decode(&c); err != nil {
|
||||
return fmt.Errorf("Can not unmarshal JSON: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Remote struct {
|
||||
Name string `json:"remote_name"`
|
||||
Address string `json:"remote_address"`
|
||||
}
|
Reference in New Issue
Block a user