github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/git-codereview/config.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "strings" 10 ) 11 12 var ( 13 configRef = "refs/remotes/origin/master:codereview.cfg" 14 cachedConfig map[string]string 15 ) 16 17 // Config returns the code review config. 18 // Configs consist of lines of the form "key: value". 19 // Lines beginning with # are comments. 20 // If there is no config, it returns an empty map. 21 // If the config is malformed, it dies. 22 func config() map[string]string { 23 if cachedConfig != nil { 24 return cachedConfig 25 } 26 raw, err := cmdOutputErr("git", "show", configRef) 27 if err != nil { 28 verbosef("%sfailed to load config from %q: %v", raw, configRef, err) 29 cachedConfig = make(map[string]string) 30 return cachedConfig 31 } 32 cachedConfig, err = parseConfig(raw) 33 if err != nil { 34 dief("%v", err) 35 } 36 return cachedConfig 37 } 38 39 func parseConfig(raw string) (map[string]string, error) { 40 cfg := make(map[string]string) 41 for _, line := range nonBlankLines(raw) { 42 line = strings.TrimSpace(line) 43 if strings.HasPrefix(line, "#") { 44 // comment line 45 continue 46 } 47 fields := strings.SplitN(line, ":", 2) 48 if len(fields) != 2 { 49 return nil, fmt.Errorf("bad config line, expected 'key: value': %q", line) 50 } 51 cfg[strings.TrimSpace(fields[0])] = strings.TrimSpace(fields[1]) 52 } 53 return cfg, nil 54 }