|
1 | 1 | package application
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "fmt"
|
5 | 7 | "io/ioutil"
|
6 | 8 |
|
| 9 | + "github.com/BurntSushi/toml" |
7 | 10 | "github.com/coniks-sys/coniks-go/crypto/sign"
|
| 11 | + "github.com/coniks-sys/coniks-go/protocol" |
8 | 12 | "github.com/coniks-sys/coniks-go/utils"
|
9 | 13 | )
|
10 | 14 |
|
@@ -61,3 +65,64 @@ func LoadSigningPubKey(path, file string) (sign.PublicKey, error) {
|
61 | 65 | }
|
62 | 66 | return signPubKey, nil
|
63 | 67 | }
|
| 68 | + |
| 69 | +// LoadInitSTR loads an initial STR at the given path |
| 70 | +// specified in the given config file. |
| 71 | +// If there is any parsing error or the STR is malformed, |
| 72 | +// LoadInitSTR() returns an error with a nil STR. |
| 73 | +func LoadInitSTR(path, file string) (*protocol.DirSTR, error) { |
| 74 | + initSTRPath := utils.ResolvePath(path, file) |
| 75 | + initSTRBytes, err := ioutil.ReadFile(initSTRPath) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("Cannot read init STR: %v", err) |
| 78 | + } |
| 79 | + initSTR := new(protocol.DirSTR) |
| 80 | + if err := json.Unmarshal(initSTRBytes, &initSTR); err != nil { |
| 81 | + return nil, fmt.Errorf("Cannot parse initial STR: %v", err) |
| 82 | + } |
| 83 | + if initSTR.Epoch != 0 { |
| 84 | + return nil, fmt.Errorf("Initial STR epoch must be 0 (got %d)", initSTR.Epoch) |
| 85 | + } |
| 86 | + return initSTR, nil |
| 87 | +} |
| 88 | + |
| 89 | +// SaveSTR serializes the given STR to the given file. |
| 90 | +func SaveSTR(file string, str *protocol.DirSTR) error { |
| 91 | + strBytes, err := json.Marshal(str) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + if err := utils.WriteFile(file, strBytes, 0600); err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// LoadConfig loads an application configuration from the given toml-encoded |
| 104 | +// file. If there is any decoding error, an LoadConfig() returns an error |
| 105 | +// with a nil config. |
| 106 | +func LoadConfig(file string) (AppConfig, error) { |
| 107 | + var conf AppConfig |
| 108 | + if _, err := toml.DecodeFile(file, &conf); err != nil { |
| 109 | + return nil, fmt.Errorf("Failed to load config: %v", err) |
| 110 | + } |
| 111 | + return conf, nil |
| 112 | +} |
| 113 | + |
| 114 | +// SaveConfig stores the given configuration conf in the given |
| 115 | +// file using toml encoding. |
| 116 | +// If there is any encoding or IO error, SaveConfig() returns an error. |
| 117 | +func SaveConfig(file string, conf AppConfig) error { |
| 118 | + var confBuf bytes.Buffer |
| 119 | + |
| 120 | + e := toml.NewEncoder(&confBuf) |
| 121 | + if err := e.Encode(conf); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if err := utils.WriteFile(file, confBuf.Bytes(), 0644); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
0 commit comments