From 5445ce1ccf5be2989ff0103fba5e2b325721537f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20P=C4=99czkowski?= Date: Sat, 13 Jan 2024 15:39:40 +0100 Subject: [PATCH] Restructure project directories Follow the standard of all go projects --- Taskfile.yml | 7 ++-- main.go => cmd/main.go | 5 ++- {commands => internal/commands}/command.go | 4 ++- .../commands}/common_utils_test.go | 2 +- {commands => internal/commands}/status_cmd.go | 2 +- .../commands}/status_cmd_test.go | 2 +- {commands => internal/commands}/sync_cmd.go | 2 +- .../commands}/sync_cmd_test.go | 3 +- {config => internal/config}/cmd.go | 0 {config => internal/config}/cmd_test.go | 0 .../config}/config_file_parse.go | 0 .../config}/config_file_parse_test.go | 0 {config => internal/config}/errors.go | 0 {config => internal/config}/structures.go | 0 {echo => internal/echo}/echo.go | 0 {echo => internal/echo}/echo_test.go | 0 {app => internal/grm}/app.go | 33 +++++++++---------- {app => internal/grm}/app_test.go | 11 +++---- {app => internal/grm}/utils.go | 5 ++- {app => internal/grm}/utils_test.go | 5 ++- 20 files changed, 39 insertions(+), 42 deletions(-) rename main.go => cmd/main.go (68%) rename {commands => internal/commands}/command.go (78%) rename {commands => internal/commands}/common_utils_test.go (98%) rename {commands => internal/commands}/status_cmd.go (98%) rename {commands => internal/commands}/status_cmd_test.go (99%) rename {commands => internal/commands}/sync_cmd.go (97%) rename {commands => internal/commands}/sync_cmd_test.go (97%) rename {config => internal/config}/cmd.go (100%) rename {config => internal/config}/cmd_test.go (100%) rename {config => internal/config}/config_file_parse.go (100%) rename {config => internal/config}/config_file_parse_test.go (100%) rename {config => internal/config}/errors.go (100%) rename {config => internal/config}/structures.go (100%) rename {echo => internal/echo}/echo.go (100%) rename {echo => internal/echo}/echo_test.go (100%) rename {app => internal/grm}/app.go (77%) rename {app => internal/grm}/app_test.go (98%) rename {app => internal/grm}/utils.go (95%) rename {app => internal/grm}/utils_test.go (96%) diff --git a/Taskfile.yml b/Taskfile.yml index de71000..3431ee5 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -7,11 +7,12 @@ tasks: silent: true test: cmds: - - go test $(find . -name "*.go" -exec dirname {} \; | uniq | grep '/') -cover + - go test $(find './internal' -name "*.go" -exec dirname {} \; | uniq | grep '/') -cover build: + dir: "cmd" cmds: - - mkdir -p build - - go build -o build/grm + - mkdir -p ../build + - go build -o ../build/grm install: deps: diff --git a/main.go b/cmd/main.go similarity index 68% rename from main.go rename to cmd/main.go index a1fcb07..e013f5f 100644 --- a/main.go +++ b/cmd/main.go @@ -1,14 +1,13 @@ package main import ( + "gitlab.com/revalus/grm/internal/grm" "os" - - "gitlab.com/revalus/grm/app" ) func main() { - app := app.GitRepositoryManager{} + app := grm.GitRepositoryManager{} err := app.Parse(os.Args) if err != nil { os.Exit(2) diff --git a/commands/command.go b/internal/commands/command.go similarity index 78% rename from commands/command.go rename to internal/commands/command.go index 88bf1e9..86706b3 100644 --- a/commands/command.go +++ b/internal/commands/command.go @@ -1,6 +1,8 @@ package commands -import "gitlab.com/revalus/grm/config" +import ( + "gitlab.com/revalus/grm/internal/config" +) type Command interface { Command(repoCfg config.RepositoryConfig) CommandStatus diff --git a/commands/common_utils_test.go b/internal/commands/common_utils_test.go similarity index 98% rename from commands/common_utils_test.go rename to internal/commands/common_utils_test.go index 07dfc8b..d58cf29 100644 --- a/commands/common_utils_test.go +++ b/internal/commands/common_utils_test.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "gitlab.com/revalus/grm/internal/config" "os" "github.com/go-git/go-billy/v5" @@ -10,7 +11,6 @@ import ( gitcfg "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/storage/filesystem" - "gitlab.com/revalus/grm/config" ) type TestSetup struct { diff --git a/commands/status_cmd.go b/internal/commands/status_cmd.go similarity index 98% rename from commands/status_cmd.go rename to internal/commands/status_cmd.go index 3279f5b..d98fcf2 100644 --- a/commands/status_cmd.go +++ b/internal/commands/status_cmd.go @@ -2,11 +2,11 @@ package commands import ( "fmt" + "gitlab.com/revalus/grm/internal/config" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" - "gitlab.com/revalus/grm/config" ) type StatusChecker struct { diff --git a/commands/status_cmd_test.go b/internal/commands/status_cmd_test.go similarity index 99% rename from commands/status_cmd_test.go rename to internal/commands/status_cmd_test.go index e8b18a1..a92e8a3 100644 --- a/commands/status_cmd_test.go +++ b/internal/commands/status_cmd_test.go @@ -2,13 +2,13 @@ package commands import ( "fmt" + "gitlab.com/revalus/grm/internal/config" "testing" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/storage/memory" - "gitlab.com/revalus/grm/config" ) func TestIfBranchesAreEqual(t *testing.T) { diff --git a/commands/sync_cmd.go b/internal/commands/sync_cmd.go similarity index 97% rename from commands/sync_cmd.go rename to internal/commands/sync_cmd.go index 36d1be2..dc03bec 100644 --- a/commands/sync_cmd.go +++ b/internal/commands/sync_cmd.go @@ -2,9 +2,9 @@ package commands import ( "fmt" + "gitlab.com/revalus/grm/internal/config" "github.com/go-git/go-git/v5" - "gitlab.com/revalus/grm/config" ) type Synchronizer struct { diff --git a/commands/sync_cmd_test.go b/internal/commands/sync_cmd_test.go similarity index 97% rename from commands/sync_cmd_test.go rename to internal/commands/sync_cmd_test.go index da086d3..93bad1e 100644 --- a/commands/sync_cmd_test.go +++ b/internal/commands/sync_cmd_test.go @@ -2,10 +2,9 @@ package commands import ( "fmt" + "gitlab.com/revalus/grm/internal/config" "os" "testing" - - "gitlab.com/revalus/grm/config" ) func TestSyncInit(t *testing.T) { diff --git a/config/cmd.go b/internal/config/cmd.go similarity index 100% rename from config/cmd.go rename to internal/config/cmd.go diff --git a/config/cmd_test.go b/internal/config/cmd_test.go similarity index 100% rename from config/cmd_test.go rename to internal/config/cmd_test.go diff --git a/config/config_file_parse.go b/internal/config/config_file_parse.go similarity index 100% rename from config/config_file_parse.go rename to internal/config/config_file_parse.go diff --git a/config/config_file_parse_test.go b/internal/config/config_file_parse_test.go similarity index 100% rename from config/config_file_parse_test.go rename to internal/config/config_file_parse_test.go diff --git a/config/errors.go b/internal/config/errors.go similarity index 100% rename from config/errors.go rename to internal/config/errors.go diff --git a/config/structures.go b/internal/config/structures.go similarity index 100% rename from config/structures.go rename to internal/config/structures.go diff --git a/echo/echo.go b/internal/echo/echo.go similarity index 100% rename from echo/echo.go rename to internal/echo/echo.go diff --git a/echo/echo_test.go b/internal/echo/echo_test.go similarity index 100% rename from echo/echo_test.go rename to internal/echo/echo_test.go diff --git a/app/app.go b/internal/grm/app.go similarity index 77% rename from app/app.go rename to internal/grm/app.go index 6b2a2fa..dfde680 100644 --- a/app/app.go +++ b/internal/grm/app.go @@ -1,31 +1,30 @@ -package app +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" - - "gitlab.com/revalus/grm/commands" - "gitlab.com/revalus/grm/config" - "gitlab.com/revalus/grm/echo" ) const ( AppName = "Git repository manager" - AppDescription = "Manage your repository with simple app" + 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 config.CliArguments - configuration config.Configuration + cliArguments config2.CliArguments + configuration config2.Configuration } func (g *GitRepositoryManager) Parse(args []string) error { - arguments, err := config.ParseCliArguments(AppName, AppDescription, args) + arguments, err := config2.ParseCliArguments(AppName, AppDescription, args) if err != nil { fmt.Printf("Error: %v", err.Error()) return err @@ -43,7 +42,7 @@ func (g *GitRepositoryManager) Parse(args []string) error { return err } - configuration, err := config.GetRepositoryConfig(configFileContent, fileExtension) + configuration, err := config2.GetRepositoryConfig(configFileContent, fileExtension) if err != nil { fmt.Printf("Error: %v", err.Error()) return err @@ -79,14 +78,14 @@ func (g *GitRepositoryManager) Run(w io.Writer) int { if g.cliArguments.Sync && exitCode == 0 { echo.InfoFMsg("Synchronizing repositories") - sync := commands.NewSynchronizer(g.configuration.Workspace) + 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 := commands.NewStatusChecker(g.configuration.Workspace) + status := commands2.NewStatusChecker(g.configuration.Workspace) g.runCommand(status) } @@ -96,7 +95,7 @@ func (g *GitRepositoryManager) Run(w io.Writer) int { return exitCode } -func describeStatus(status commands.CommandStatus) { +func describeStatus(status commands2.CommandStatus) { if status.Error { echo.RedMessageF("Repository \"%v\": an error occurred: %v", status.Name, status.Message) return @@ -110,7 +109,7 @@ func describeStatus(status commands.CommandStatus) { } func (g *GitRepositoryManager) limitTags() error { - limitedTagsTmp := []config.RepositoryConfig{} + limitedTagsTmp := []config2.RepositoryConfig{} for _, item := range g.configuration.Repositories { if checkAnyOfItemInSlice(item.Tags, g.cliArguments.LimitToTags) { @@ -127,14 +126,14 @@ func (g *GitRepositoryManager) limitTags() error { func (g *GitRepositoryManager) limitName() error { for _, item := range g.configuration.Repositories { if g.cliArguments.LimitToName == item.Name { - g.configuration.Repositories = []config.RepositoryConfig{item} + g.configuration.Repositories = []config2.RepositoryConfig{item} return nil } } return errors.New(errNotFoundName) } -func (g *GitRepositoryManager) runCommand(cmd commands.Command) { +func (g *GitRepositoryManager) runCommand(cmd commands2.Command) { routines := make(chan struct{}, g.cliArguments.Routines) var wg sync.WaitGroup @@ -146,7 +145,7 @@ func (g *GitRepositoryManager) runCommand(cmd commands.Command) { wg.Add(1) - go func(r config.RepositoryConfig) { + go func(r config2.RepositoryConfig) { defer wg.Done() routines <- struct{}{} describeStatus(cmd.Command(r)) diff --git a/app/app_test.go b/internal/grm/app_test.go similarity index 98% rename from app/app_test.go rename to internal/grm/app_test.go index 18e2952..8ee728c 100644 --- a/app/app_test.go +++ b/internal/grm/app_test.go @@ -1,14 +1,13 @@ -package app +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" - - "gitlab.com/revalus/grm/commands" - "gitlab.com/revalus/grm/config" - "gitlab.com/revalus/grm/echo" ) type FakeCommandToTest struct { @@ -92,7 +91,7 @@ func TestParseApplication(t *testing.T) { os.Remove(workdir) }) - args := []string{"custom-app", "sync", "-c", configFile} + args := []string{"custom-grm", "sync", "-c", configFile} grm := GitRepositoryManager{} grm.Parse(args) diff --git a/app/utils.go b/internal/grm/utils.go similarity index 95% rename from app/utils.go rename to internal/grm/utils.go index 8263379..2bc8a47 100644 --- a/app/utils.go +++ b/internal/grm/utils.go @@ -1,12 +1,11 @@ -package app +package grm import ( "errors" "fmt" + "gitlab.com/revalus/grm/internal/config" "os" "strings" - - "gitlab.com/revalus/grm/config" ) func getFileContent(pathToFile string) ([]byte, error) { diff --git a/app/utils_test.go b/internal/grm/utils_test.go similarity index 96% rename from app/utils_test.go rename to internal/grm/utils_test.go index 5239e55..aa68a0b 100644 --- a/app/utils_test.go +++ b/internal/grm/utils_test.go @@ -1,10 +1,9 @@ -package app +package grm import ( + "gitlab.com/revalus/grm/internal/config" "reflect" "testing" - - "gitlab.com/revalus/grm/config" ) func TestGetFileExtension(t *testing.T) {