Restructure project directories

Follow the standard of all go projects
This commit is contained in:
Mikołaj Pęczkowski 2024-01-13 15:39:40 +01:00
parent 741f18efd1
commit 5445ce1ccf
20 changed files with 39 additions and 42 deletions

157
internal/grm/app.go Normal file
View file

@ -0,0 +1,157 @@
package grm
import (
"errors"
"fmt"
commands2 "gitlab.com/revalus/grm/internal/commands"
config2 "gitlab.com/revalus/grm/internal/config"
"gitlab.com/revalus/grm/internal/echo"
"io"
"sync"
)
const (
AppName = "Git repository manager"
AppDescription = "Manage your repository with simple grm"
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 {
cliArguments config2.CliArguments
configuration config2.Configuration
}
func (g *GitRepositoryManager) Parse(args []string) error {
arguments, err := config2.ParseCliArguments(AppName, AppDescription, args)
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
configFileContent, err := getFileContent(arguments.ConfigurationFile)
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
fileExtension, err := getFileExtension(arguments.ConfigurationFile)
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
configuration, err := config2.GetRepositoryConfig(configFileContent, fileExtension)
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
g.cliArguments = arguments
g.configuration = configuration
return nil
}
func (g *GitRepositoryManager) Run(w io.Writer) int {
echo.Color(g.cliArguments.Color)
echo.Output(w)
exitCode := 0
if len(g.cliArguments.LimitToTags) != 0 {
err := g.limitTags()
if err != nil {
echo.ErrorfMsg(err.Error())
exitCode = 1
}
}
if g.cliArguments.LimitToName != "" {
err := g.limitName()
if err != nil {
echo.ErrorfMsg(err.Error())
exitCode = 1
}
}
if g.cliArguments.Sync && exitCode == 0 {
echo.InfoFMsg("Synchronizing repositories")
sync := commands2.NewSynchronizer(g.configuration.Workspace)
g.runCommand(sync)
echo.InfoFMsg("All repositories are synced")
}
if g.cliArguments.Status && exitCode == 0 {
echo.InfoFMsg("Current status of repositories")
status := commands2.NewStatusChecker(g.configuration.Workspace)
g.runCommand(status)
}
if g.cliArguments.Version {
echo.InfoFMsg("Current version: %v", VERSION)
}
return exitCode
}
func describeStatus(status commands2.CommandStatus) {
if status.Error {
echo.RedMessageF("Repository \"%v\": an error occurred: %v", status.Name, status.Message)
return
}
if status.Changed {
echo.YellowMessageF("Repository \"%v\": %v", status.Name, status.Message)
} else {
echo.GreenMessageF("Repository \"%v\": %v", status.Name, status.Message)
}
}
func (g *GitRepositoryManager) limitTags() error {
limitedTagsTmp := []config2.RepositoryConfig{}
for _, item := range g.configuration.Repositories {
if checkAnyOfItemInSlice(item.Tags, g.cliArguments.LimitToTags) {
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.LimitToName == item.Name {
g.configuration.Repositories = []config2.RepositoryConfig{item}
return nil
}
}
return errors.New(errNotFoundName)
}
func (g *GitRepositoryManager) runCommand(cmd commands2.Command) {
routines := make(chan struct{}, g.cliArguments.Routines)
var wg sync.WaitGroup
for _, repo := range g.configuration.Repositories {
if repo.Skip && !g.cliArguments.IgnoreSkipped {
continue
}
wg.Add(1)
go func(r config2.RepositoryConfig) {
defer wg.Done()
routines <- struct{}{}
describeStatus(cmd.Command(r))
<-routines
}(repo)
}
wg.Wait()
}

432
internal/grm/app_test.go Normal file
View file

@ -0,0 +1,432 @@
package grm
import (
"fmt"
"gitlab.com/revalus/grm/internal/commands"
"gitlab.com/revalus/grm/internal/config"
"gitlab.com/revalus/grm/internal/echo"
"os"
"reflect"
"testing"
)
type FakeCommandToTest struct {
triggerError bool
triggerChanged bool
}
type ExpectedMessageTester struct {
expectedMessages []string
}
func (emt ExpectedMessageTester) Write(p []byte) (n int, err error) {
msg := string(p)
if !checkIsItemInSlice(msg, emt.expectedMessages) {
panic(fmt.Sprintf("the message \"%v\"does not match any of the given patterns: %#v", msg, emt.expectedMessages))
} else {
fmt.Println(msg)
}
return 0, nil
}
func (fk FakeCommandToTest) Command(repoCfg config.RepositoryConfig) 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
}
return status
}
func prepareConfigContent() (string, string) {
checkErrorDuringPreparation := func(err error) {
if err != nil {
fmt.Printf("Cannot prepare a temporary directory for testing! %v ", err.Error())
os.Exit(2)
}
}
baseTmp := fmt.Sprintf("%v/grmTest", os.TempDir())
if _, ok := os.Stat(baseTmp); ok != nil {
err := os.Mkdir(baseTmp, 0777)
checkErrorDuringPreparation(err)
}
tempDir, err := os.MkdirTemp(baseTmp, "*")
checkErrorDuringPreparation(err)
configFilePath := fmt.Sprintf("%v/config-file.yaml", tempDir)
file, err := os.Create(configFilePath)
checkErrorDuringPreparation(err)
defer file.Close()
yamlConfig := fmt.Sprintf(`
workspace: %v
repositories:
- src: "https://github.com/golang/example.git"
tags: ['example']
`, tempDir)
_, err = file.WriteString(yamlConfig)
checkErrorDuringPreparation(err)
return tempDir, configFilePath
}
func TestParseApplication(t *testing.T) {
workdir, configFile := prepareConfigContent()
t.Cleanup(func() {
os.Remove(workdir)
})
args := []string{"custom-grm", "sync", "-c", configFile}
grm := GitRepositoryManager{}
grm.Parse(args)
if workdir != grm.configuration.Workspace {
t.Errorf("Expected to get %v, instead of this got %v", workdir, grm.configuration.Repositories)
}
if !grm.cliArguments.Sync {
t.Error("The value of \"sync\" is expected to be true")
}
expectedRepo := config.RepositoryConfig{
Name: "example",
Src: "https://github.com/golang/example.git",
Dest: "example",
Tags: []string{"example"},
}
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 TestOutputFromSync(t *testing.T) {
grm := GitRepositoryManager{
configuration: config.Configuration{
Workspace: "/tmp",
},
cliArguments: config.CliArguments{
Sync: true,
Version: true,
Color: false,
Routines: 10,
},
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Info: Synchronizing repositories\n",
"Info: All repositories are synced\n",
fmt.Sprintf("Info: Current version: %v\n", VERSION),
},
}
grm.Run(emt)
}
func TestLimitTags(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToTags: []string{"example"},
Routines: 10,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1", Tags: []string{"example"}},
{Name: "example2", Tags: []string{"example"}},
{Name: "notExample"},
},
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"example1\": response from fake command\n",
"Repository \"example2\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.limitTags()
grm.runCommand(fakeCommand)
}
func TestLimitName(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToName: "notExample",
Routines: 10,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1", Tags: []string{"example"}},
{Name: "example2", Tags: []string{"example"}},
{Name: "notExample"},
},
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"notExample\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.limitName()
grm.runCommand(fakeCommand)
}
func TestRunWithNotExistingNameInLimit(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToName: "not-existing-name",
Routines: 10,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1", Tags: []string{"example"}},
{Name: "example2", Tags: []string{"example"}},
{Name: "notExample"},
},
},
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Error: no repository was found with the specified name\n",
},
}
echo.Color(false)
status := grm.Run(emt)
if status != 1 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
}
func TestRunWithNotExistingTagsInLimit(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToTags: []string{"not-existing-tag"},
Routines: 10,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1", Tags: []string{"example"}},
{Name: "example2", Tags: []string{"example"}},
{Name: "notExample"},
},
},
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Error: no repository was found with the specified tags\n",
},
}
echo.Color(false)
status := grm.Run(emt)
if status != 1 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
}
func TestGetStatusOutput(t *testing.T) {
grm := GitRepositoryManager{
configuration: config.Configuration{
Workspace: "/tmp",
},
cliArguments: config.CliArguments{
Status: true,
Routines: 10,
},
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Info: Current status of repositories\n",
},
}
echo.Color(false)
status := grm.Run(emt)
if status != 0 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
}
func TestDescribeStatusErrorNoColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"Test\": an error occurred: test\n",
},
}
echo.Color(false)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Error: true,
}
describeStatus(status)
}
func TestDescribeStatusErrorColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
fmt.Sprintf("%vRepository \"Test\": an error occurred: test%v\n", echo.ColorRed, echo.ColorReset),
},
}
echo.Color(true)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Error: true,
}
describeStatus(status)
}
func TestDescribeStatusChangedNoColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"Test\": test\n",
},
}
echo.Color(false)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Changed: true,
}
describeStatus(status)
}
func TestDescribeStatusChangedColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
fmt.Sprintf("%vRepository \"Test\": test%v\n", echo.ColorYellow, echo.ColorReset),
},
}
echo.Color(true)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Changed: true,
}
describeStatus(status)
}
func TestDescribeStatusNoChangeNoColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"Test\": test\n",
},
}
echo.Color(false)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Changed: false,
}
describeStatus(status)
}
func TestDescribeStatusNoChangeColor(t *testing.T) {
emt := ExpectedMessageTester{
expectedMessages: []string{
fmt.Sprintf("%vRepository \"Test\": test%v\n", echo.ColorGreen, echo.ColorReset),
},
}
echo.Color(true)
echo.Output(emt)
status := commands.CommandStatus{
Name: "Test",
Message: "test",
Changed: false,
}
describeStatus(status)
}
func TestSkipRepository(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToTags: []string{"example"},
Routines: 10,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1"},
{Name: "example2", Skip: true},
{Name: "example3"},
},
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"example1\": response from fake command\n",
"Repository \"example3\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.runCommand(fakeCommand)
}
func TestSkipRepositoryWithIgnore(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitToTags: []string{"example"},
Routines: 10,
IgnoreSkipped: true,
},
configuration: config.Configuration{
Repositories: []config.RepositoryConfig{
{Name: "example1"},
{Name: "example2", Skip: true},
{Name: "example3"},
},
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"example1\": response from fake command\n",
"Repository \"example2\": response from fake command\n",
"Repository \"example3\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.runCommand(fakeCommand)
}

54
internal/grm/utils.go Normal file
View file

@ -0,0 +1,54 @@
package grm
import (
"errors"
"fmt"
"gitlab.com/revalus/grm/internal/config"
"os"
"strings"
)
func getFileContent(pathToFile string) ([]byte, error) {
return os.ReadFile(pathToFile)
}
func getFileExtension(pathToFile string) (string, error) {
splitFileName := strings.Split(pathToFile, ".")
if len(splitFileName) == 1 {
msg := fmt.Sprintf("excension for file \"%v\", not found", splitFileName)
return "", errors.New(msg)
}
fileExtension := splitFileName[len(splitFileName)-1]
return fileExtension, 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
}

View file

@ -0,0 +1,84 @@
package grm
import (
"gitlab.com/revalus/grm/internal/config"
"reflect"
"testing"
)
func TestGetFileExtension(t *testing.T) {
toTest := map[string]string{
"myYamlFile.yaml": "yaml",
"myTxtFile.txt": "txt",
"myJsonFile.json": "json",
}
for key, value := range toTest {
result, err := getFileExtension(key)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if result != value {
t.Errorf("Expected to get %v, instead of this got %v", value, result)
}
}
}
func TestErrorInGetExcensionFile(t *testing.T) {
result, err := getFileExtension("test")
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)
}
}