mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-07-26 13:47:40 +00:00
Config file reading/writing.
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConfigStructure is structure of main configuration
|
||||||
|
type ConfigStructure struct {
|
||||||
|
RootDir string `json:"rootDir"`
|
||||||
|
DownloadConcurrency int `json:"downloadConcurrency"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config is configuration for aptly, shared by all modules
|
||||||
|
var Config = ConfigStructure{
|
||||||
|
RootDir: "/var/aptly",
|
||||||
|
DownloadConcurrency: 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadConfig loads configuration from json file
|
||||||
|
func LoadConfig(filename string, config *ConfigStructure) error {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
dec := json.NewDecoder(f)
|
||||||
|
return dec.Decode(&config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveConfig write configuration to json file
|
||||||
|
func SaveConfig(filename string, config *ConfigStructure) error {
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
encoded, err := json.MarshalIndent(&config, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.Write(encoded)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "launchpad.net/gocheck"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigSuite struct {
|
||||||
|
config ConfigStructure
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = Suite(&ConfigSuite{})
|
||||||
|
|
||||||
|
func (s *ConfigSuite) TestLoadConfig(c *C) {
|
||||||
|
configname := filepath.Join(c.MkDir(), "aptly.json")
|
||||||
|
f, _ := os.Create(configname)
|
||||||
|
f.WriteString(configFile)
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
err := LoadConfig(configname, &s.config)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
c.Check(s.config.RootDir, Equals, "/opt/aptly/")
|
||||||
|
c.Check(s.config.DownloadConcurrency, Equals, 33)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ConfigSuite) TestSaveConfig(c *C) {
|
||||||
|
configname := filepath.Join(c.MkDir(), "aptly.json")
|
||||||
|
|
||||||
|
s.config.RootDir = "/tmp/aptly"
|
||||||
|
s.config.DownloadConcurrency = 5
|
||||||
|
|
||||||
|
err := SaveConfig(configname, &s.config)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
|
f, _ := os.Open(configname)
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
st, _ := f.Stat()
|
||||||
|
buf := make([]byte, st.Size())
|
||||||
|
f.Read(buf)
|
||||||
|
|
||||||
|
c.Check(string(buf), Equals, ""+
|
||||||
|
"{\n"+
|
||||||
|
" \"rootDir\": \"/tmp/aptly\",\n"+
|
||||||
|
" \"downloadConcurrency\": 5\n"+
|
||||||
|
"}")
|
||||||
|
}
|
||||||
|
|
||||||
|
const configFile = `{"rootDir": "/opt/aptly/", "downloadConcurrency": 33}`
|
||||||
Reference in New Issue
Block a user