config.go

 1package git
 2
 3// ConfigOptions are options for Config.
 4type ConfigOptions struct {
 5	File string
 6	All  bool
 7	Add  bool
 8}
 9
10// Config gets a git configuration.
11func Config(key string, opts ...ConfigOptions) (string, error) {
12	var opt ConfigOptions
13	if len(opts) > 0 {
14		opt = opts[0]
15	}
16	cmd := NewCommand("config")
17	if opt.File != "" {
18		cmd.AddArgs("--file", opt.File)
19	}
20	if opt.All {
21		cmd.AddArgs("--get-all")
22	}
23	cmd.AddArgs(key)
24	bts, err := cmd.Run()
25	if err != nil {
26		return "", err
27	}
28	return string(bts), nil
29}
30
31// SetConfig sets a git configuration.
32func SetConfig(key string, value string, opts ...ConfigOptions) error {
33	var opt ConfigOptions
34	if len(opts) > 0 {
35		opt = opts[0]
36	}
37	cmd := NewCommand("config")
38	if opt.File != "" {
39		cmd.AddArgs("--file", opt.File)
40	}
41	cmd.AddArgs(key, value)
42	_, err := cmd.Run()
43	return err
44}