1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-04-09 10:24:17 +00:00

put in default config if file do not exist

This commit is contained in:
postmannen 2021-03-02 06:51:08 +01:00
parent 069bf5a639
commit 9553aec6bd
4 changed files with 34 additions and 11 deletions

View file

@ -36,16 +36,38 @@ func NewConfiguration() *Configuration {
return &c
}
func newConfigurationDefaults() Configuration {
c := Configuration{
ConfigFolder: "./etc",
BrokerAddress: "127.0.0.1:4222",
ProfilingPort: "",
PromHostAndPort: "",
CentralErrorLogger: false,
DefaultMessageTimeout: 10,
DefaultMessageRetries: 1,
PublisherServiceSayhello: 30,
}
return c
}
func (c *Configuration) CheckFlags() {
// read file config
// TODO: Look into how to be able to specify the location via flag,
// now it is only using the default location which is /etc.
// The problem is that the flags are parsed after the default config
// is set, so we need a way to get that location out of the flag when
// creating the config file the first time.
// Create an empty default config
var fc Configuration
// Read file config. Set system default if it can't find config file.
fc, err := c.ReadConfigFile()
if err != nil {
log.Printf("%v\n", err)
fc = newConfigurationDefaults()
}
//fmt.Printf("---\nContent of file\n---\n%#v\n", tmpC)
flag.StringVar(&c.ConfigFolder, "configFolder", fc.ConfigFolder, "")
flag.StringVar(&c.ConfigFolder, "configFolder", fc.ConfigFolder, "folder who contains the config file")
flag.StringVar(&c.NodeName, "node", fc.NodeName, "some unique string to identify this Edge unit")
flag.StringVar(&c.BrokerAddress, "brokerAddress", fc.BrokerAddress, "the address of the message broker")
flag.StringVar(&c.ProfilingPort, "profilingPort", fc.ProfilingPort, "The number of the profiling port")
@ -86,9 +108,11 @@ func (c *Configuration) ReadConfigFile() (Configuration, error) {
return conf, nil
}
// WriteConfigFile will write the current config to file. If the file or the
// directory for the config file does not exist it will be created.
func (c *Configuration) WriteConfigFile() error {
if _, err := os.Stat(c.ConfigFolder); os.IsNotExist(err) {
err := os.Mkdir(c.ConfigFolder, 0600)
err := os.Mkdir(c.ConfigFolder, 0700)
if err != nil {
return fmt.Errorf("error: failed to create directory %v: %v", c.ConfigFolder, err)
}

Binary file not shown.

View file

@ -13,8 +13,6 @@ type Message struct {
// The Unique ID of the message
ID int `json:"id" yaml:"id"`
// The actual data in the message
// TODO: Change this to a slice instead...or maybe use an
// interface type here to handle several data types ?
Data []string `json:"data" yaml:"data"`
// method, what is this message doing, etc. CLI, syslog, etc.
Method Method `json:"method" yaml:"method"`

View file

@ -192,6 +192,7 @@ func (s *server) processPrepareNew(subject Subject, errCh chan errProcess, proce
// create the initial configuration for a sessions communicating with 1 host process.
s.lastProcessID++
// make the slice of allowedReceivers into a map value for easy lookup.
m := make(map[node]struct{})
for _, a := range allowedReceivers {
m[a] = struct{}{}
@ -355,11 +356,11 @@ func (s *server) subscriberHandler(natsConn *nats.Conn, thisNode string, msg *na
out := []byte("not allowed from " + message.FromNode)
var err error
// TESTING: TO ALLOW RECEIVING ONLY FROM SPECIFIC HOSTS
_, arOK1 := proc.allowedReceivers[message.FromNode]
_, arOK2 := proc.allowedReceivers[message.FromNode]
// Check if we are allowed to receive from that host
_, arOK := proc.allowedReceivers[message.FromNode]
//_, arOK2 := proc.allowedReceivers[message.FromNode]
if arOK1 || arOK2 {
if arOK {
out, err = mf.handler(s, message, thisNode)
if err != nil {