summaryrefslogtreecommitdiff
path: root/core/command/config.go
blob: 35eff2329ccdc8c412dedc3f912afcce42489818 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package command

import (
	"errors"

	"github.com/isacikgoz/gitbatch/core/git"
	log "github.com/sirupsen/logrus"
)

var (
	configCmdMode string

	configCommand       = "config"
	configCmdModeLegacy = "git"
	configCmdModeNative = "go-git"
)

// ConfigOptions defines the rules for commit operation
type ConfigOptions struct {
	// Section
	Section string
	// Option
	Option string
	// Site should be Global or Local
	Site ConfigSite
}

// ConfigSite defines a string type for the site.
type ConfigSite string

const (
	// ConfigSiteLocal defines a local config.
	ConfigSiteLocal ConfigSite = "local"

	// ConfgiSiteGlobal defines a global config.
	ConfgiSiteGlobal ConfigSite = "global"
)

// Config adds or reads config of a repository
func Config(r *git.Repository, options ConfigOptions) (value string, err error) {
	// here we configure config operation
	// default mode is go-git (this may be configured)
	configCmdMode = configCmdModeLegacy

	switch configCmdMode {
	case configCmdModeLegacy:
		return configWithGit(r, options)
	case configCmdModeNative:
		return configWithGoGit(r, options)
	}
	return value, errors.New("Unhandled config operation")
}

// configWithGit is simply a bare git commit -m <msg> command which is flexible
func configWithGit(r *git.Repository, options ConfigOptions) (value string, err error) {
	args := make([]string, 0)
	args = append(args, configCommand)
	if len(string(options.Site)) > 0 {
		args = append(args, "--"+string(options.Site))
	}
	args = append(args, "--get")
	args = append(args, options.Section+"."+options.Option)
	// parse options to command line arguments
	out, err := GenericGitCommandWithOutput(r.AbsPath, args)
	if err != nil {
		return out, err
	}
	// till this step everything should be ok
	return out, nil
}

// commitWithGoGit is the primary commit method
func configWithGoGit(r *git.Repository, options ConfigOptions) (value string, err error) {
	// TODO: add global search
	config, err := r.Repo.Config()
	if err != nil {
		return value, err
	}
	return config.Raw.Section(options.Section).Option(options.Option), nil
}

// AddConfig adds an entry on the ConfigOptions field.
func AddConfig(r *git.Repository, options ConfigOptions, value string) (err error) {
	return addConfigWithGit(r, options, value)

}

// addConfigWithGit is simply a bare git config --add <option> command which is flexible
func addConfigWithGit(r *git.Repository, options ConfigOptions, value string) (err error) {
	args := make([]string, 0)
	args = append(args, configCommand)
	if len(string(options.Site)) > 0 {
		args = append(args, "--"+string(options.Site))
	}
	args = append(args, "--add")
	args = append(args, options.Section+"."+options.Option)
	if len(value) > 0 {
		args = append(args, value)
	}
	if err := GenericGitCommand(r.AbsPath, args); err != nil {
		log.Warn("Error at git command (config)")
		return err
	}
	// till this step everything should be ok
	return r.Refresh()
}