| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
ErrMissingName = errors.New("Missing or empty name field") |
| 11 |
ErrMissingDesc = errors.New("Missing or empty description field") |
| 12 |
ErrMissingSite = errors.New("Missing or empty website field") |
| 13 |
) |
| 14 |
|
| 15 |
// PluginInfo contains all the needed info about a plugin |
| 16 |
// The info is just strings and are not used beyond that (except |
| 17 |
// the Site and Install fields should be valid URLs). This means |
| 18 |
// that the requirements for example can be formatted however the |
| 19 |
// plugin maker decides, the fields will only be parsed by humans |
| 20 |
// Name: name of plugin |
| 21 |
// Desc: description of plugin |
| 22 |
// Site: home website of plugin |
| 23 |
// Install: install link for plugin (can be link to repo or zip file) |
| 24 |
// Vstr: version |
| 25 |
// Require: list of dependencies and requirements |
| 26 |
type PluginInfo struct { |
| 27 |
Name string `json:"Name"` |
| 28 |
Desc string `json:"Description"` |
| 29 |
Site string `json:"Website"` |
| 30 |
} |
| 31 |
|
| 32 |
// NewPluginInfo parses a JSON input into a valid PluginInfo struct |
| 33 |
// Returns an error if there are any missing fields or any invalid fields |
| 34 |
// There are no optional fields in a plugin info json file |
| 35 |
func NewPluginInfo(data []byte) (*PluginInfo, error) { |
| 36 |
var info []PluginInfo |
| 37 |
|
| 38 |
dec := json.NewDecoder(bytes.NewReader(data)) |
| 39 |
// dec.DisallowUnknownFields() // Force errors |
| 40 |
|
| 41 |
if err := dec.Decode(&info); err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
|
| 45 |
return &info[0], nil |
| 46 |
} |