@@ -4,10 +4,14 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"bitbucket.org/digitorus/pdfsign/sign"
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/asaskevich/govalidator"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
govalidator.SetFieldsRequiredByDefault(true)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultLocation string = "./pdfsign.conf" // Default location of the config file
|
DefaultLocation string = "./pdfsign.conf" // Default location of the config file
|
||||||
Settings Config // Initialized once inside Read method Settings are stored in memory.
|
Settings Config // Initialized once inside Read method Settings are stored in memory.
|
||||||
@@ -15,8 +19,24 @@ var (
|
|||||||
|
|
||||||
// Config is the root of the config
|
// Config is the root of the config
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Info sign.SignDataSignatureInfo
|
//Info:
|
||||||
TSA sign.TSA
|
//Name: "Jeroen Bobbeldijk",
|
||||||
|
//Location: "Rotterdam",
|
||||||
|
//Reason: "Test",
|
||||||
|
//ContactInfo: "Geen",
|
||||||
|
//CertType: 2,
|
||||||
|
//Approval: false,
|
||||||
|
//TSA: sign.TSA{
|
||||||
|
//URL: "http://aatl-timestamp.globalsign.com/tsa/aohfewat2389535fnasgnlg5m23",
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateFields validates all the fields of the config
|
||||||
|
func (c Config) ValidateFields() error {
|
||||||
|
_, err := govalidator.ValidateStruct(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Read(configfile string) {
|
func Read(configfile string) {
|
||||||
@@ -30,5 +50,9 @@ func Read(configfile string) {
|
|||||||
if _, err := toml.DecodeFile(configfile, &c); err != nil {
|
if _, err := toml.DecodeFile(configfile, &c); err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.ValidateFields(); err != nil {
|
||||||
|
log.Fatal("Config is not valid: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
Settings = c
|
Settings = c
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,5 +20,18 @@ staticPath = "../static"
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Root
|
// Root
|
||||||
assert.Equal(t, "../static", c)
|
assert.Equal(t, "../static", c.StaticPath)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidation(t *testing.T) {
|
||||||
|
const configContent = ``
|
||||||
|
|
||||||
|
var c config.Config
|
||||||
|
if _, err := toml.Decode(configContent, &c); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := c.ValidateFields()
|
||||||
|
assert.NotNil(t, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user