Add possiblity to limit actions to tagged or named repository
This commit is contained in:
parent
5c98ab6554
commit
23e4547e52
11 changed files with 345 additions and 24 deletions
59
app/app.go
59
app/app.go
|
|
@ -1,6 +1,7 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"gitlab.com/revalus/grm/commands"
|
||||
|
|
@ -10,7 +11,9 @@ import (
|
|||
const (
|
||||
APP_NAME = "Git repository manager"
|
||||
APP_DESCRIPTION = "Manage your repository with simple app"
|
||||
VERSION = "0.2.0"
|
||||
VERSION = "0.3.0"
|
||||
errNotFoundTags = "no repository was found with the specified tags"
|
||||
errNotFoundName = "no repository was found with the specified name"
|
||||
)
|
||||
|
||||
type GitRepositoryManager struct {
|
||||
|
|
@ -47,17 +50,35 @@ func (g *GitRepositoryManager) Parse(args []string) {
|
|||
g.configuration = configuration
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) Run() {
|
||||
if g.cliArguments.Sync {
|
||||
func (g *GitRepositoryManager) Run() int {
|
||||
|
||||
exitCode := 0
|
||||
|
||||
if len(g.cliArguments.LimitTags) != 0 {
|
||||
err := g.limitTags()
|
||||
if err != nil {
|
||||
g.console.ErrorfMsg(err.Error())
|
||||
exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
if g.cliArguments.LimitName != "" {
|
||||
err := g.limitName()
|
||||
if err != nil {
|
||||
g.console.ErrorfMsg(err.Error())
|
||||
exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
if g.cliArguments.Sync && exitCode == 0 {
|
||||
g.console.InfoFMsg("Synchronizing repositories")
|
||||
println()
|
||||
sync := commands.NewSynchronizer(g.configuration.Workspace)
|
||||
g.runCommand(sync)
|
||||
println()
|
||||
g.console.InfoFMsg("All repositories are synced")
|
||||
}
|
||||
|
||||
if g.cliArguments.Status {
|
||||
if g.cliArguments.Status && exitCode == 0 {
|
||||
g.console.InfoFMsg("Current status of repositories")
|
||||
status := commands.NewStatusChecker(g.configuration.Workspace)
|
||||
g.runCommand(status)
|
||||
}
|
||||
|
|
@ -65,6 +86,7 @@ func (g *GitRepositoryManager) Run() {
|
|||
if g.cliArguments.Version {
|
||||
g.console.InfoFMsg("Current version: %v", VERSION)
|
||||
}
|
||||
return exitCode
|
||||
}
|
||||
|
||||
func (g GitRepositoryManager) describeStatus(status commands.CommandStatus) {
|
||||
|
|
@ -80,6 +102,31 @@ func (g GitRepositoryManager) describeStatus(status commands.CommandStatus) {
|
|||
}
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) limitTags() error {
|
||||
limitedTagsTmp := []config.RepositoryConfig{}
|
||||
|
||||
for _, item := range g.configuration.Repositories {
|
||||
if checkAnyOfItemInSlice(item.Tags, g.cliArguments.LimitTags) {
|
||||
limitedTagsTmp = append(limitedTagsTmp, item)
|
||||
}
|
||||
}
|
||||
if len(limitedTagsTmp) == 0 {
|
||||
return errors.New(errNotFoundTags)
|
||||
}
|
||||
g.configuration.Repositories = reverseRepositoryConfigs(limitedTagsTmp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) limitName() error {
|
||||
for _, item := range g.configuration.Repositories {
|
||||
if g.cliArguments.LimitName == item.Name {
|
||||
g.configuration.Repositories = []config.RepositoryConfig{item}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New(errNotFoundName)
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) runCommand(cmd commands.Command) {
|
||||
statusChan := make(chan commands.CommandStatus)
|
||||
|
||||
|
|
|
|||
147
app/app_test.go
147
app/app_test.go
|
|
@ -3,11 +3,36 @@ package app
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitlab.com/revalus/grm/commands"
|
||||
"gitlab.com/revalus/grm/config"
|
||||
)
|
||||
|
||||
type FakeCommandToTest struct {
|
||||
triggerError bool
|
||||
triggerChanged bool
|
||||
}
|
||||
|
||||
func (fk FakeCommandToTest) Command(repoCfg config.RepositoryConfig, cmdStatus chan commands.CommandStatus) {
|
||||
status := commands.CommandStatus{
|
||||
Name: repoCfg.Name,
|
||||
Changed: false,
|
||||
Message: "response from fake command",
|
||||
Error: false,
|
||||
}
|
||||
|
||||
if fk.triggerError {
|
||||
status.Error = true
|
||||
}
|
||||
if fk.triggerChanged {
|
||||
status.Changed = true
|
||||
}
|
||||
|
||||
cmdStatus <- status
|
||||
}
|
||||
|
||||
func prepareConfigContent() (string, string) {
|
||||
checkErrorDuringPreparation := func(err error) {
|
||||
if err != nil {
|
||||
|
|
@ -35,7 +60,9 @@ func prepareConfigContent() (string, string) {
|
|||
yamlConfig := fmt.Sprintf(`
|
||||
workspace: %v
|
||||
repositories:
|
||||
- src: "https://github.com/golang/example.git"`, tempDir)
|
||||
- src: "https://github.com/golang/example.git"
|
||||
tags: ['example']
|
||||
`, tempDir)
|
||||
|
||||
_, err = file.WriteString(yamlConfig)
|
||||
|
||||
|
|
@ -65,12 +92,12 @@ func TestParseApplication(t *testing.T) {
|
|||
Name: "example",
|
||||
Src: "https://github.com/golang/example.git",
|
||||
Dest: "example",
|
||||
Tags: []string{"example"},
|
||||
}
|
||||
|
||||
if expectedRepo != grm.configuration.Repositories[0] {
|
||||
if !reflect.DeepEqual(expectedRepo, grm.configuration.Repositories[0]) {
|
||||
t.Errorf("Expected to get %v, instead of this got %v", expectedRepo, grm.configuration.Repositories[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Example_test_sync_output() {
|
||||
|
|
@ -90,5 +117,117 @@ func Example_test_sync_output() {
|
|||
// Output:
|
||||
// Info: Synchronizing repositories
|
||||
// Info: All repositories are synced
|
||||
// Info: Current version: 0.2.0
|
||||
// Info: Current version: 0.3.0
|
||||
}
|
||||
|
||||
func Example_limit_test_tags() {
|
||||
grm := GitRepositoryManager{
|
||||
cliArguments: config.CliArguments{
|
||||
LimitTags: []string{"example"},
|
||||
},
|
||||
configuration: config.Configuration{
|
||||
Repositories: []config.RepositoryConfig{
|
||||
{Name: "example1", Tags: []string{"example"}},
|
||||
{Name: "example2", Tags: []string{"example"}},
|
||||
{Name: "notExample"},
|
||||
},
|
||||
},
|
||||
console: ConsoleOutput{
|
||||
Color: false,
|
||||
},
|
||||
}
|
||||
fakeCommand := FakeCommandToTest{
|
||||
triggerError: false,
|
||||
triggerChanged: false,
|
||||
}
|
||||
grm.limitTags()
|
||||
grm.runCommand(fakeCommand)
|
||||
// Output:
|
||||
// Repository "example1": response from fake command
|
||||
// Repository "example2": response from fake command
|
||||
}
|
||||
|
||||
func Example_limit_name() {
|
||||
grm := GitRepositoryManager{
|
||||
cliArguments: config.CliArguments{
|
||||
LimitName: "notExample",
|
||||
},
|
||||
configuration: config.Configuration{
|
||||
Repositories: []config.RepositoryConfig{
|
||||
{Name: "example1", Tags: []string{"example"}},
|
||||
{Name: "example2", Tags: []string{"example"}},
|
||||
{Name: "notExample"},
|
||||
},
|
||||
},
|
||||
console: ConsoleOutput{
|
||||
Color: false,
|
||||
},
|
||||
}
|
||||
fakeCommand := FakeCommandToTest{
|
||||
triggerError: false,
|
||||
triggerChanged: false,
|
||||
}
|
||||
grm.limitName()
|
||||
grm.runCommand(fakeCommand)
|
||||
// Output:
|
||||
// Repository "notExample": response from fake command
|
||||
}
|
||||
|
||||
func Example_run_with_limit_not_existing_name() {
|
||||
grm := GitRepositoryManager{
|
||||
cliArguments: config.CliArguments{
|
||||
LimitName: "not-existing-name",
|
||||
},
|
||||
configuration: config.Configuration{
|
||||
Repositories: []config.RepositoryConfig{
|
||||
{Name: "example1", Tags: []string{"example"}},
|
||||
{Name: "example2", Tags: []string{"example"}},
|
||||
{Name: "notExample"},
|
||||
},
|
||||
},
|
||||
console: ConsoleOutput{
|
||||
Color: false,
|
||||
},
|
||||
}
|
||||
grm.Run()
|
||||
// Output:
|
||||
// Error: no repository was found with the specified name
|
||||
}
|
||||
|
||||
func Example_run_with_limit_not_existing_tags() {
|
||||
grm := GitRepositoryManager{
|
||||
cliArguments: config.CliArguments{
|
||||
LimitTags: []string{"not-existing-tag"},
|
||||
},
|
||||
configuration: config.Configuration{
|
||||
Repositories: []config.RepositoryConfig{
|
||||
{Name: "example1", Tags: []string{"example"}},
|
||||
{Name: "example2", Tags: []string{"example"}},
|
||||
{Name: "notExample"},
|
||||
},
|
||||
},
|
||||
console: ConsoleOutput{
|
||||
Color: false,
|
||||
},
|
||||
}
|
||||
grm.Run()
|
||||
// Output:
|
||||
// Error: no repository was found with the specified tags
|
||||
}
|
||||
|
||||
func Example_test_status_output() {
|
||||
grm := GitRepositoryManager{
|
||||
configuration: config.Configuration{
|
||||
Workspace: "/tmp",
|
||||
},
|
||||
cliArguments: config.CliArguments{
|
||||
Status: true,
|
||||
},
|
||||
console: ConsoleOutput{
|
||||
Color: false,
|
||||
},
|
||||
}
|
||||
grm.Run()
|
||||
// Output:
|
||||
// Info: Current status of repositories
|
||||
}
|
||||
|
|
|
|||
30
app/utils.go
30
app/utils.go
|
|
@ -5,6 +5,8 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/revalus/grm/config"
|
||||
)
|
||||
|
||||
func getFileContent(pathToFile string) ([]byte, error) {
|
||||
|
|
@ -23,3 +25,31 @@ func getFileExcension(pathToFile string) (string, error) {
|
|||
|
||||
return fileExcension, nil
|
||||
}
|
||||
|
||||
func checkIsItemInSlice(check string, sliceToCheck []string) bool {
|
||||
for _, item := range sliceToCheck {
|
||||
if item == check {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func checkAnyOfItemInSlice(check []string, sliceToCheck []string) bool {
|
||||
|
||||
for _, item := range check {
|
||||
if checkIsItemInSlice(item, sliceToCheck) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func reverseRepositoryConfigs(repositories []config.RepositoryConfig) []config.RepositoryConfig {
|
||||
for i, j := 0, len(repositories)-1; i < j; i, j = i+1, j-1 {
|
||||
repositories[i], repositories[j] = repositories[j], repositories[i]
|
||||
}
|
||||
return repositories
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
package app
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitlab.com/revalus/grm/config"
|
||||
)
|
||||
|
||||
func TestGetFileExtension(t *testing.T) {
|
||||
|
||||
|
|
@ -32,5 +37,49 @@ func TestErrorInGetExcensionFile(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Errorf("Expected to get error, instead of this got result %v", result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestIsItemInSlice(t *testing.T) {
|
||||
testedSlice := []string{"1", "2", "3", "4", "5"}
|
||||
|
||||
result := checkIsItemInSlice("0", testedSlice)
|
||||
if result {
|
||||
t.Error("Expected to get false as result")
|
||||
}
|
||||
|
||||
result = checkIsItemInSlice("1", testedSlice)
|
||||
if !result {
|
||||
t.Error("Expected to get true as result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAnyInSlice(t *testing.T) {
|
||||
testedSlice := []string{"1", "2", "3", "4", "5"}
|
||||
|
||||
result := checkAnyOfItemInSlice([]string{"0", "10"}, testedSlice)
|
||||
if result {
|
||||
t.Error("Expected to get false as result")
|
||||
}
|
||||
|
||||
result = checkAnyOfItemInSlice([]string{"0", "5"}, testedSlice)
|
||||
if !result {
|
||||
t.Error("Expected to get true as result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverseStringsSlice(t *testing.T) {
|
||||
testedSlice := []config.RepositoryConfig{
|
||||
{Name: "test1"},
|
||||
{Name: "test2"},
|
||||
{Name: "test3"},
|
||||
}
|
||||
expectedResult := []config.RepositoryConfig{
|
||||
{Name: "test3"},
|
||||
{Name: "test2"},
|
||||
{Name: "test1"},
|
||||
}
|
||||
result := reverseRepositoryConfigs(testedSlice)
|
||||
if !reflect.DeepEqual(result, expectedResult) {
|
||||
t.Errorf("Expected to get \"%#v\", instead of this got \"%#v\"", expectedResult, result)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue