GitRepositoryManager/internal/grm/app.go

158 lines
3.7 KiB
Go
Raw Normal View History

package grm
2021-11-02 18:19:31 +00:00
import (
"errors"
"fmt"
2024-03-24 14:18:32 +00:00
"gitlab.com/revalus/grm/internal/commands"
"gitlab.com/revalus/grm/internal/config"
"gitlab.com/revalus/grm/internal/echo"
"io"
2021-11-08 17:30:45 +00:00
"sync"
2021-11-02 18:19:31 +00:00
)
const (
2022-12-10 22:47:59 +00:00
AppName = "Git repository manager"
AppDescription = "Manage your repository with simple grm"
2024-03-24 14:18:32 +00:00
VERSION = "0.3.2"
errNotFoundTags = "no repository was found with the specified tags"
errNotFoundName = "no repository was found with the specified name"
2021-11-02 18:19:31 +00:00
)
type GitRepositoryManager struct {
2024-03-24 14:18:32 +00:00
cliArguments config.CliArguments
configuration config.Configuration
2021-11-02 18:19:31 +00:00
}
2021-11-08 17:30:45 +00:00
func (g *GitRepositoryManager) Parse(args []string) error {
2024-03-24 14:18:32 +00:00
arguments, err := config.ParseCliArguments(AppName, AppDescription, args)
2021-11-08 17:30:45 +00:00
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
2021-11-02 18:19:31 +00:00
configFileContent, err := getFileContent(arguments.ConfigurationFile)
2021-11-08 17:30:45 +00:00
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
2021-11-02 18:19:31 +00:00
2022-12-10 22:47:59 +00:00
fileExtension, err := getFileExtension(arguments.ConfigurationFile)
2021-11-08 17:30:45 +00:00
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
2021-11-02 18:19:31 +00:00
2024-03-24 14:18:32 +00:00
configuration, err := config.GetRepositoryConfig(configFileContent, fileExtension)
2021-11-08 17:30:45 +00:00
if err != nil {
fmt.Printf("Error: %v", err.Error())
return err
}
2021-11-02 18:19:31 +00:00
g.cliArguments = arguments
g.configuration = configuration
2021-11-08 17:30:45 +00:00
return nil
2021-11-02 18:19:31 +00:00
}
func (g *GitRepositoryManager) Run(w io.Writer) int {
echo.Color(g.cliArguments.Color)
echo.Output(w)
exitCode := 0
2021-11-08 17:30:45 +00:00
if len(g.cliArguments.LimitToTags) != 0 {
err := g.limitRepositoriesToTags()
if err != nil {
echo.ErrorfMsg(err.Error())
return 1
}
}
2021-11-08 17:30:45 +00:00
if g.cliArguments.LimitToName != "" {
err := g.limitRepositoryToName()
if err != nil {
echo.ErrorfMsg(err.Error())
return 1
}
}
if g.cliArguments.Sync && exitCode == 0 {
echo.InfoFMsg("Synchronizing repositories")
2024-03-24 14:18:32 +00:00
sync := commands.NewSynchronizer(g.configuration.Workspace)
2021-11-02 18:19:31 +00:00
g.runCommand(sync)
echo.InfoFMsg("All repositories are synced")
2021-11-02 18:19:31 +00:00
}
if g.cliArguments.Status && exitCode == 0 {
echo.InfoFMsg("Current status of repositories")
2024-03-24 14:18:32 +00:00
status := commands.NewStatusChecker(g.configuration.Workspace)
2021-11-05 17:19:06 +00:00
g.runCommand(status)
}
2021-11-02 18:19:31 +00:00
if g.cliArguments.Version {
echo.InfoFMsg("Current version: %v", VERSION)
2021-11-02 18:19:31 +00:00
}
return exitCode
2021-11-02 18:19:31 +00:00
}
2024-03-24 14:18:32 +00:00
func describeStatus(status commands.CommandStatus) {
2021-11-02 18:19:31 +00:00
if status.Error {
echo.RedMessageF("Repository \"%v\": an error occurred: %v", status.Name, status.Message)
2021-11-02 18:19:31 +00:00
return
}
if status.Changed {
echo.YellowMessageF("Repository \"%v\": %v", status.Name, status.Message)
2021-11-02 18:19:31 +00:00
} else {
echo.GreenMessageF("Repository \"%v\": %v", status.Name, status.Message)
2021-11-02 18:19:31 +00:00
}
}
func (g *GitRepositoryManager) limitRepositoriesToTags() error {
2024-03-24 14:18:32 +00:00
limitedTagsTmp := []config.RepositoryConfig{}
for _, item := range g.configuration.Repositories {
2021-11-08 17:30:45 +00:00
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) limitRepositoryToName() error {
for _, item := range g.configuration.Repositories {
2021-11-08 17:30:45 +00:00
if g.cliArguments.LimitToName == item.Name {
2024-03-24 14:18:32 +00:00
g.configuration.Repositories = []config.RepositoryConfig{item}
return nil
}
}
return errors.New(errNotFoundName)
}
2024-03-24 14:18:32 +00:00
func (g *GitRepositoryManager) runCommand(cmd commands.Command) {
2021-11-08 17:30:45 +00:00
routines := make(chan struct{}, g.cliArguments.Routines)
2021-11-02 18:19:31 +00:00
2021-11-08 17:30:45 +00:00
var wg sync.WaitGroup
2021-11-02 18:19:31 +00:00
for _, repo := range g.configuration.Repositories {
if repo.Skip && !g.cliArguments.IgnoreSkipped {
continue
}
2021-11-08 17:30:45 +00:00
wg.Add(1)
2024-03-24 14:18:32 +00:00
go func(r config.RepositoryConfig) {
2021-11-08 17:30:45 +00:00
defer wg.Done()
routines <- struct{}{}
describeStatus(cmd.Command(r))
2021-11-02 18:19:31 +00:00
2021-11-08 17:30:45 +00:00
<-routines
}(repo)
2021-11-02 18:19:31 +00:00
}
2021-11-08 17:30:45 +00:00
wg.Wait()
2021-11-02 18:19:31 +00:00
}