Wrap some more config functions #42

Merged
carlosmn merged 4 commits from cmn/config into master 2014-02-26 03:37:35 -06:00
1 changed files with 86 additions and 0 deletions
Showing only changes of commit 129105d410 - Show all commits

View File

@ -10,10 +10,61 @@ import (
"unsafe"
)
type ConfigLevel int
const (
// System-wide configuration file; /etc/gitconfig on Linux systems
ConfigLevelSystem ConfigLevel = C.GIT_CONFIG_LEVEL_SYSTEM
// XDG compatible configuration file; typically ~/.config/git/config
ConfigLevelXDG ConfigLevel = C.GIT_CONFIG_LEVEL_XDG
// User-specific configuration file (also called Global configuration
// file); typically ~/.gitconfig
ConfigLevelGlobal ConfigLevel = C.GIT_CONFIG_LEVEL_GLOBAL
// Repository specific configuration file; $WORK_DIR/.git/config on
// non-bare repos
ConfigLevelLocal ConfigLevel = C.GIT_CONFIG_LEVEL_LOCAL
// Application specific configuration file; freely defined by applications
ConfigLevelApp ConfigLevel = C.GIT_CONFIG_LEVEL_APP
// Represents the highest level available config file (i.e. the most
// specific config file available that actually is loaded)
ConfigLevelHighest ConfigLevel = C.GIT_CONFIG_HIGHEST_LEVEL
)
type Config struct {
ptr *C.git_config
}
// NewConfig creates a new empty configuration object
func NewConfig() (*Config, error) {
config := new(Config)
ret := C.git_config_new(&config.ptr)
if ret < 0 {
return nil, LastError()
}
return config, nil
}
// AddFile adds a file-backed backend to the config object at the specified level.
func (c *Config) AddFile(path string, level ConfigLevel, force bool) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), cbool(force))
if ret < 0 {
return LastError()
}
return nil
}
func (c *Config) LookupInt32(name string) (int32, error) {
var out C.int32_t
cname := C.CString(name)
@ -165,3 +216,38 @@ func (c *Config) Delete(name string) error {
return nil
}
// OpenLevel creates a single-level focused config object from a multi-level one
func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
config := new(Config)
ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level))
if ret < 0 {
return nil, LastError()
}
return config, nil
}
// OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(parent *Config, path string) (*Config, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
config := new(Config)
ret := C.git_config_open_ondisk(&config.ptr, cpath)
if ret < 0 {
return nil, LastError()
}
return config, nil
}
// Refresh refreshes the configuration to reflect any changes made externally e.g. on disk
func (c *Config) Refresh() error {
ret := C.git_config_refresh(c.ptr)
if ret < 0 {
return LastError()
}
return nil
}