From d564e37b317a2d59a9694d80b03b40e5d36f741f Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 27 May 2019 21:18:46 +0200 Subject: repository: add ReadConfigBool and ReadConfigString functions --- repository/git.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index c982f820..4d6ca19a 100644 --- a/repository/git.go +++ b/repository/git.go @@ -8,6 +8,7 @@ import ( "io" "os/exec" "path" + "strconv" "strings" "github.com/MichaelMure/git-bug/util/git" @@ -202,6 +203,40 @@ func (repo *GitRepo) ReadConfigs(keyPrefix string) (map[string]string, error) { return result, nil } +func (repo *GitRepo) ReadConfigBool(key string) (bool, error) { + val, err := repo.ReadConfigString(key) + if err != nil { + return false, err + } + + return strconv.ParseBool(val) +} + +func (repo *GitRepo) ReadConfigString(key string) (string, error) { + stdout, err := repo.runGitCommand("config", "--get-all", key) + + // / \ + // / ! \ + // ------- + // + // There can be a legitimate error here, but I see no portable way to + // distinguish them from the git error that say "no matching value exist" + if err != nil { + return "", ErrNoConfigEntry + } + + lines := strings.Split(stdout, "\n") + + if len(lines) == 0 { + return "", ErrNoConfigEntry + } + if len(lines) > 1 { + return "", ErrMultipleConfigEntry + } + + return lines[0], nil +} + // RmConfigs remove all key/value pair matching the key prefix func (repo *GitRepo) RmConfigs(keyPrefix string) error { _, err := repo.runGitCommand("config", "--unset-all", keyPrefix) -- cgit