Added script for default configuration file generation

This commit is contained in:
Tripon Alexandru-Ionut
2019-06-21 11:44:07 +03:00
committed by Dan Christian Bogos
parent 85afa27a5e
commit 23deb84f9d
3 changed files with 31 additions and 30 deletions

View File

@@ -18,13 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"fmt"
"os"
"path"
"strings"
)
const CGRATES_CFG_JSON = `
{
@@ -790,24 +783,3 @@ const CGRATES_CFG_JSON = `
},
}`
func writeDefaultCofig() error {
f, err := os.OpenFile(path.Join("/usr", "share", "cgrates", "conf", "cgrates", "cgrates.json"), os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return err
}
defer f.Close()
rows := strings.Split(CGRATES_CFG_JSON, "\n")[1:] // remove first empty row
for i, row := range rows {
if i == 0 || i == len(rows)-1 { // do not comment first and last row
fmt.Fprintln(f, row)
continue
}
if withoutSpace := strings.TrimSpace(row); len(withoutSpace) == 0 || strings.HasPrefix(row, "//") { // do not comment empty rows and alerady commented ones
fmt.Fprintln(f, row)
continue
}
fmt.Fprintf(f, "// %s\n", row)
}
return nil
}

View File

@@ -1,4 +1,4 @@
// +build integration
// +build generate
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
@@ -20,11 +20,37 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"fmt"
"os"
"path"
"strings"
"testing"
)
func writeDefaultCofig(fileName string) error {
f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return err
}
defer f.Close()
rows := strings.Split(CGRATES_CFG_JSON, "\n")[1:] // remove first empty row
for i, row := range rows {
if i == 0 || i == len(rows)-1 { // do not comment first and last row
fmt.Fprintln(f, row)
continue
}
if withoutSpace := strings.TrimSpace(row); len(withoutSpace) == 0 || strings.HasPrefix(row, "//") { // do not comment empty rows and alerady commented ones
fmt.Fprintln(f, row)
continue
}
fmt.Fprintf(f, "// %s\n", row)
}
return nil
}
// used only to generate the commented configuration file
func TestWriteDefaultCofig(t *testing.T) {
if err := writeDefaultCofig(); err != nil {
if err := writeDefaultCofig(path.Join("/usr", "share", "cgrates", "conf", "cgrates", "cgrates.json")); err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,3 @@
#! /usr/bin/env sh
go test github.com/cgrates/cgrates/config -tags=generate | echo "Generating configuration file ..."