OpenSlides/docs/modelsvalidator/main.go

57 lines
1.2 KiB
Go
Raw Normal View History

2020-11-11 12:02:23 +01:00
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
2021-01-06 11:32:16 +01:00
"github.com/OpenSlides/Openslides/modelsvalidator/check"
models "github.com/OpenSlides/openslides-models-to-go"
2020-11-11 12:02:23 +01:00
)
func main() {
var content io.Reader = os.Stdin
if len(os.Args) > 1 {
c, err := openModels(os.Args[1])
if err != nil {
log.Fatalf("Can not load content: %v", err)
}
defer c.Close()
content = c
}
data, err := models.Unmarshal(content)
if err != nil {
log.Fatalf("Invalid model format: %v", err)
}
2021-01-06 11:32:16 +01:00
if err := check.Check(data); err != nil {
2020-11-11 12:02:23 +01:00
log.Fatalf("Invalid model structure:\n\n%v", err)
}
}
// openModels reads the model either from file or from an url.
func openModels(path string) (io.ReadCloser, error) {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return openModelsFromURL(path)
}
return os.Open(path)
}
func openModelsFromURL(url string) (io.ReadCloser, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("requesting models from url: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("can not get models from url. Got status %s", resp.Status)
}
return resp.Body, nil
}