diff options
author | Michael Muré <batolettre@gmail.com> | 2019-05-27 21:18:46 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-05-27 21:39:55 +0200 |
commit | d564e37b317a2d59a9694d80b03b40e5d36f741f (patch) | |
tree | ba6bcdca38286372293b65f3c0b087cf95c066a1 /repository/git.go | |
parent | c7abac388aadd274d4f23f996a15f8bba90f2a92 (diff) | |
download | git-bug-d564e37b317a2d59a9694d80b03b40e5d36f741f.tar.gz |
repository: add ReadConfigBool and ReadConfigString functions
Diffstat (limited to 'repository/git.go')
-rw-r--r-- | repository/git.go | 35 |
1 files changed, 35 insertions, 0 deletions
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) |