// attachConfFile function is to attach the conf file into the default conf file func attachConfFile(a config.App, s config.Server) error { result := string(readDefaultConfFile(s)) lines := strings.Split(result, "\n") linesLength := len(lines) added := strings.Contains(result, a.Uri()) if !added { F: for i := linesLength - 1; i > 0; i-- { if strings.TrimSpace(lines[i]) == "}" { lines[i] = getIncludeTemplate(a) + "\n}" break F } } output := strings.Join(lines, "\n") err := ioutil.WriteFile(filepath.Join(s.Path, s.Filename), []byte(output), 0644) if err != nil { return err } } return nil }
// RemoveConf is to remove the conf file func RemoveConf(a config.App, s config.Server) error { if err := os.Remove(a.Uri()); err != nil { return err //return NOK + "Error Remove the File: " + err.Error() } detachConfFile(a, s) return nil }
// WriteConf is the main function for creating the conf file func WriteConf(a config.App, s config.Server) error { if err := isDefaultConfExist(s); err != true { return errors.New(NOK + " Default Conf File Error: file is not exist") } result := []byte(createTemplate(s, a)) if err := createDir(a); err != nil { return err //return NOK + "Create Directory Error: " + err.Error() } if err := write(result, a.Uri()); err != nil { return err //return NOK + "Conf File Write Error: " + err.Error() } if err := attachConfFile(a, s); err != nil { return err //return NOK + "Conf File Attach Error: " + err.Error() } return nil }
// isConfigFileAdded will check the default conf file // if the conf file already include in the default conf file, then no need to include the conf file func isConfigFileAdded(a config.App, s config.Server) bool { result := string(readDefaultConfFile(s)) return strings.Contains(result, a.Uri()) }
// getIncludeTemplate function is to get the include formatting with the conf file name func getIncludeTemplate(a config.App) string { return "\n\tinclude\t" + a.Uri() }