Mainflux.mainflux/provision/config.go

83 lines
2.4 KiB
Go
Raw Normal View History

NOISSUE - Update provision service (#1133) * Add provision service Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * delete sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add provision file Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add provision Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add provision Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * deleete LICENSE Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix modules Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix modules Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix modules Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactoring provison service Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * provision Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test, and use sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove bootstrap mock Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * implement channel Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * export errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tests Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add new line Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove docker-compse Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add new line Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix load config Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix load config Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change dflt file Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix content Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * cert sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix bootstraping Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update swagger Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix mproxy mod Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix test Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter messages Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * upd vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * upd vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove dead code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add some space for readability Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remov thingids Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert readme Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix logging Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * use snake_case Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix var naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix signature Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * aling with defaults Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix logging info Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix logging Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix tag Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2020-04-29 06:39:23 +08:00
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package provision
import (
"fmt"
"io/ioutil"
"github.com/mainflux/mainflux/errors"
"github.com/pelletier/go-toml"
)
// Service represents service config.
type ServiceConf struct {
Port string `toml:"port"`
LogLevel string `toml:"log_level"`
TLS bool `toml:"tls"`
CACerts string `toml:"ca_certs"`
ServerCert string `toml:"server_cert"`
ServerKey string `toml:"server_key"`
ThingsLocation string `toml:"things_location"`
UsersLocation string `toml:"users_location"`
MQTTURL string `toml:"mqtt_url"`
HTTPPort string `toml:"http_port"`
MfUser string `toml:"mf_user"`
MfPass string `toml:"mf_pass"`
MfAPIKey string `toml:"mf_api_key"`
MfBSURL string `toml:"mf_bs_url"`
MfWhiteListURL string `toml:"mf_whit_list"`
MfCertsURL string `toml:"mf_certs_url"`
}
type Bootstrap struct {
X509Provision bool `toml:"x509_provision"`
Provision bool `toml:"provision"`
AutoWhiteList bool `toml:"autowhite_list"`
Content string `toml:"content"`
}
type Channel struct {
Name string `toml:"name"`
Metadata map[string]interface{} `toml:"metadata" mapstructure:"metadata"`
}
type Thing struct {
Name string `toml:"name"`
Metadata map[string]interface{} `toml:"metadata" mapstructure:"metadata"`
}
// Config struct of Provision
type Config struct {
File string `toml:"file"`
Server ServiceConf `toml:"server" mapstructure:"server"`
Bootstrap Bootstrap `toml:"bootstrap" mapstructure:"boostrap"`
Things []Thing `toml:"things" mapstructure:"things"`
Channels []Channel `toml:"channels" mapstructure:"channels"`
}
// Save - store config in a file
func Save(c Config, file string) error {
b, err := toml.Marshal(c)
if err != nil {
return errors.New(fmt.Sprintf("Error reading config file: %s", err))
}
if err := ioutil.WriteFile(file, b, 0644); err != nil {
return errors.New(fmt.Sprintf("Error writing toml: %s", err))
}
return nil
}
// Read - retrieve config from a file
func Read(file string) (Config, error) {
data, err := ioutil.ReadFile(file)
c := Config{}
if err != nil {
return c, errors.New(fmt.Sprintf("Error reading config file: %s", err))
}
if err := toml.Unmarshal(data, &c); err != nil {
return Config{}, errors.New(fmt.Sprintf("Error unmarshaling toml: %s", err))
}
return c, nil
}