...

Source file src/discovery/config/utils.go

Documentation: discovery/config

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2020. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package config
    21  
    22  import (
    23  	"discovery/log"
    24  	"encoding/json"
    25  	"io/ioutil"
    26  	"os"
    27  )
    28  
    29  func GetConfigurationDynamicFilePath() string {
    30  	return GetDataPath() + "/" + ConfigurationFileName
    31  }
    32  
    33  func SaveConfigurationDynamicToConfigFile() error {
    34  	// create folder if not exists
    35  	err := CreateDataFolder() // os.Mkdir(GetDataPath(), 0664)
    36  	if err != nil {
    37  		log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
    38  		return err
    39  	}
    40  
    41  	// save configuration to file
    42  	configJson, err := json.MarshalIndent(configurationDynamic, "", "  ")
    43  	err = ioutil.WriteFile(GetConfigurationDynamicFilePath(), configJson, 0644)
    44  	if err != nil {
    45  		log.Log.Errorf("Cannot save configuration to file %s: %s", GetConfigurationDynamicFilePath(), err.Error())
    46  		return err
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  func CreateDataFolder() error {
    53  	// check if folder exists
    54  	if _, err := os.Stat(GetDataPath()); !os.IsNotExist(err) {
    55  		return nil
    56  	}
    57  
    58  	// create folder if not exists
    59  	err := os.Mkdir(GetDataPath(), 0664)
    60  	if err != nil {
    61  		log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }
    67  

View as plain text