git-lfs/config.go

55 lines
1008 B
Go
Raw Normal View History

2013-11-05 17:07:03 +00:00
package gitmedia
2013-11-05 17:45:01 +00:00
import (
"github.com/pelletier/go-toml"
"os"
"path/filepath"
)
2013-11-05 17:07:03 +00:00
type Configuration struct {
Endpoint string
}
var config *Configuration
// Config gets the git media configuration for the current repository. It
// reads .gitmedia, which is a toml file.
//
// https://github.com/mojombo/toml
func Config() *Configuration {
if config == nil {
2013-11-05 17:45:01 +00:00
config = &Configuration{}
readToml(config)
2013-11-05 17:07:03 +00:00
}
return config
}
2013-11-05 17:45:01 +00:00
func readToml(config *Configuration) {
tomlPath := tomlFile()
stat, _ := os.Stat(tomlPath)
if stat != nil {
readTomlFile(tomlPath, config)
}
}
func tomlFile() string {
wd, err := os.Getwd()
if err != nil {
Panic(err, "Unable to read working directory")
2013-11-05 17:45:01 +00:00
}
return filepath.Join(wd, ".gitmedia")
}
func readTomlFile(path string, config *Configuration) {
tomlConfig, err := toml.LoadFile(path)
if err != nil {
Panic(err, "Error reading TOML file: %s", path)
2013-11-05 17:45:01 +00:00
}
if endpoint, ok := tomlConfig.Get("endpoint").(string); ok {
config.Endpoint = endpoint
}
}