Skip to content

Commit f39c599

Browse files
committed
Create remote on need
1 parent 75e192a commit f39c599

File tree

8 files changed

+113
-45
lines changed

8 files changed

+113
-45
lines changed

cmds/checkcmd/check.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ const (
3535
func (c *Check) Action([]string) {
3636
c.config = core.NewConfig("release-mgt.yaml")
3737

38+
_, err := c.config.Load()
39+
kingpin.FatalIfError(err, "Unable to load %s properly.", c.config.Filename())
40+
3841
c.github = core.NewGithub()
3942

40-
err := c.github.CheckGithub()
43+
err = c.github.CheckGithub()
4144
kingpin.FatalIfError(err, "Unable to get github-release")
4245

4346
c.git = core.NewGit()

cmds/statecmd/state.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ const (
3333
// Action execute the `check` command
3434
func (c *State) Action([]string) {
3535
c.config = core.NewConfig("release-mgt.yaml")
36+
37+
_, err := c.config.Load()
38+
kingpin.FatalIfError(err, "Unable to load %s properly.", c.config.Filename())
3639

3740
c.github = core.NewGithub()
3841

39-
err := c.github.CheckGithub()
42+
err = c.github.CheckGithub()
4043
kingpin.FatalIfError(err, "Unable to get github-release")
4144

4245
c.git = core.NewGit()

cmds/tagcmd/tag.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ const (
4040
func (c *Tag) Action([]string) {
4141
c.config = core.NewConfig("release-mgt.yaml")
4242

43+
_, err := c.config.Load()
44+
kingpin.FatalIfError(err, "Unable to load %s properly.", c.config.Filename())
45+
4346
c.github = core.NewGithub()
4447

45-
err := c.github.CheckGithub()
48+
err = c.github.CheckGithub()
4649
kingpin.FatalIfError(err, "Unable to get github-release")
4750

4851
c.git = core.NewGit()
@@ -79,24 +82,18 @@ func (c *Tag) Action([]string) {
7982
os.Exit(1)
8083
}
8184

85+
c.git.SetRemote(c.config.Yaml.Upstream.Name, c.config.Yaml.Upstream.Protocol, c.config.Yaml.Upstream.Server, c.config.Yaml.Upstream.RepoPath)
86+
87+
8288
// Check remote. Create it if missing.
83-
options := make(map[string]string)
84-
if *c.user != "" {
85-
options["username"] = *c.user
86-
}
87-
if *c.pass != "" {
88-
options["password"] = *c.pass
89-
}
90-
if *c.remoteName != "" {
91-
options["remote-name"] = *c.remoteName
92-
}
93-
if *c.removeRemote {
94-
options["auto-remove-remote"] = "true"
95-
}
89+
options := make(core.GitRemoteConfig)
90+
91+
options.Set("username", *c.user)
92+
options.Set("password", *c.pass)
93+
options.SetIfNotContains("remote-name", *c.remoteName, "ci-upstream")
94+
options.SetIf("auto-remove-remote", "false", !*c.removeRemote)
95+
options.Set("protocol", *c.proto)
9696

97-
if *c.proto != "" {
98-
options["protocol"] = *c.proto
99-
}
10097
if err = c.git.CreateRemote(options); err != nil {
10198
fmt.Printf("%s\n", err)
10299
os.Exit(1)

core/config.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"fmt"
66
"os"
77

8+
"github.com/forj-oss/forjj-modules/trace"
89
"gopkg.in/yaml.v2"
910
)
1011

1112
// Config is the top Configuration object.
1213
type Config struct {
13-
yaml yamlConfig
14+
Yaml YamlConfig
1415
file string
1516
}
1617

@@ -22,27 +23,39 @@ func NewConfig(file string) (ret *Config) {
2223
return
2324
}
2425

26+
// Filename return the configuration file name.
27+
func (c Config) Filename() string {
28+
return c.file
29+
}
30+
2531
// Load the configuration file
26-
func (c *Config) Load() (err error) {
32+
func (c *Config) Load() (loaded bool, err error) {
2733
if c == nil {
28-
return errors.New("Config object is nil. Unable to load")
34+
err = errors.New("Config object is nil. Unable to load")
35+
return
2936
}
3037

3138
var fd *os.File
3239
fd, err = os.Open(c.file)
3340
if err != nil {
34-
return fmt.Errorf("Unable to load '%s'. %s", c.file, err)
41+
gotrace.Warning("%s not loaded. %s", c.file, err)
42+
return
3543
}
3644

3745
decoder := yaml.NewDecoder(fd)
3846

3947
if decoder == nil {
40-
return fmt.Errorf("Unable to load '%s'. yaml decoder object not created", c.file)
48+
err = fmt.Errorf("Unable to load '%s'. yaml decoder object not created", c.file)
49+
return
4150
}
4251

43-
err = decoder.Decode(&c.yaml)
52+
err = decoder.Decode(&c.Yaml)
4453
if err != nil {
45-
return fmt.Errorf("Unable to read yaml file '%s'. %s", c.file, err)
54+
err = fmt.Errorf("Unable to read yaml file '%s'. %s", c.file, err)
55+
return
4656
}
57+
58+
gotrace.Info("%s loaded.", c.file)
59+
loaded = true
4760
return
4861
}

core/git-remote-config.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package core
2+
3+
type GitRemoteConfig map[string]string
4+
5+
// Set if value and name is set
6+
func (grc GitRemoteConfig) Set(name, value string) {
7+
if value == "" || name == "" {
8+
return
9+
}
10+
11+
grc[name] = value
12+
}
13+
14+
// SetIfNotContains set value if the value is not one on the listed ifNot
15+
func (grc GitRemoteConfig) SetIfNotContains(name, value string, ifNot ...string) {
16+
for _, v := range ifNot {
17+
if value == v {
18+
return
19+
}
20+
21+
}
22+
23+
grc.Set(name, value)
24+
}
25+
26+
// SetIf set value if the value is not one on the listed ifNot
27+
func (grc GitRemoteConfig) SetIf(name, value string, ifCase bool) {
28+
if !ifCase {
29+
return
30+
}
31+
32+
grc.Set(name, value)
33+
}
34+
35+
// Get if value and name is set
36+
func (grc GitRemoteConfig) Get(name string, defaultValues ...string) (value string) {
37+
if name == "" {
38+
return
39+
}
40+
41+
if v, found := grc[name]; found && v != "" {
42+
return v
43+
}
44+
45+
for _, v := range defaultValues {
46+
if v != "" {
47+
return v
48+
}
49+
}
50+
51+
return
52+
}

core/git.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ func NewGit() (ret *Git) {
3535
return
3636
}
3737

38-
func (g *Git) SetRemote(protocol, host, repoPath string) {
38+
func (g *Git) SetRemote(name, protocol, host, repoPath string) {
3939
if g == nil {
4040
return
4141
}
42+
g.remoteName = name
43+
g.protocol = protocol
44+
g.host = host
45+
g.repoPath = repoPath
4246
}
4347

4448
// OpenRepo open the GIT repo
@@ -118,36 +122,30 @@ func (g *Git) CreateTag(name string) (err error) {
118122
}
119123

120124
// CreateRemote create or update a remote.
121-
func (g *Git) CreateRemote(options map[string]string) (_ error) {
122-
if v, found := options["remote-name"]; found {
123-
g.remoteName = v
124-
}
125+
func (g *Git) CreateRemote(options GitRemoteConfig) (_ error) {
126+
g.remoteName = options.Get("remote-name", g.remoteName, "ci-upstream")
125127

126128
if g.host == "" || g.repoPath == "" {
127129
return errors.New("Unable to create a remote without host/repo-path setup in 'release-mgt.yaml'")
128130
}
129131

130132
if r, err := g.repo.Remote(g.remoteName); r == nil && err.Error() != git.ErrRemoteNotFound.Error() {
131133
return err
134+
} else if r != nil {
135+
g.removeRemote = false
136+
gotrace.Trace("Remote '%s' already exist. Not recreated.", g.remoteName)
137+
return nil
132138
}
133139

134140
if v, found := options["auto-remove-remote"]; !found || v == "true" {
135141
g.removeRemote = true
136142
}
137-
if g.remoteName == "" {
138-
g.remoteName = "ci-upstream"
139-
}
140-
protocol := "https"
141-
if v, found := options["protocol"]; found {
142-
protocol = v
143-
}
144-
if protocol == "" {
145-
protocol = "https"
146-
}
143+
147144
remoteConfig := gitconfig.RemoteConfig{
148145
Name: g.remoteName,
149146
}
150-
switch protocol {
147+
148+
switch protocol := options.Get("protocol", g.protocol, "https"); protocol {
151149
case "https", "http":
152150
var user *url.Userinfo
153151
if u, foundUser := options["user"]; foundUser {
@@ -189,6 +187,7 @@ func (g *Git) CreateRemote(options map[string]string) (_ error) {
189187
if _, err := g.repo.CreateRemote(&remoteConfig); err != nil {
190188
return err
191189
}
190+
gotrace.Trace("Remote '%s' created.", g.remoteName)
192191

193192
return
194193
}
@@ -205,6 +204,7 @@ func (g *Git) CleanRemote() (_ error) {
205204
r, err := g.repo.Remote(g.remoteName)
206205
if r != nil {
207206
g.repo.DeleteRemote(g.remoteName)
207+
gotrace.Trace("Remote '%s' removed.", g.remoteName)
208208
}
209209

210210
return err

core/yaml-config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package core
22

3-
type yamlConfig struct {
4-
Upstream yamlUpstream
3+
type YamlConfig struct {
4+
Upstream YamlUpstream
55
}

core/yaml-upstream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package core
22

33

4-
type yamlUpstream struct {
4+
type YamlUpstream struct {
55
Name string
66
Server string
77
RepoPath string `yaml:"repo-path"`

0 commit comments

Comments
 (0)