GitRepositoryManager/app/app.go

142 lines
3.4 KiB
Go
Raw Normal View History

2021-11-02 18:19:31 +00:00
package app
import (
"errors"
"fmt"
"io"
2021-11-02 18:19:31 +00:00
"os"
"gitlab.com/revalus/grm/commands"
"gitlab.com/revalus/grm/config"
"gitlab.com/revalus/grm/echo"
2021-11-02 18:19:31 +00:00
)
const (
APP_NAME = "Git repository manager"
APP_DESCRIPTION = "Manage your repository with simple app"
VERSION = "0.3.0"
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 {
cliArguments config.CliArguments
configuration config.Configuration
}
func (g *GitRepositoryManager) Parse(args []string) {
checkCriticalError := func(err error) {
if err != nil {
fmt.Printf("Error: %v", err.Error())
2021-11-02 18:19:31 +00:00
os.Exit(2)
}
}
arguments, err := config.ParseCliArguments(APP_NAME, APP_DESCRIPTION, args)
checkCriticalError(err)
configFileContent, err := getFileContent(arguments.ConfigurationFile)
checkCriticalError(err)
fileExcension, err := getFileExcension(arguments.ConfigurationFile)
checkCriticalError(err)
configuration, err := config.GetRepositoryConfig(configFileContent, fileExcension)
checkCriticalError(err)
g.cliArguments = arguments
g.configuration = configuration
}
func (g *GitRepositoryManager) Run(w io.Writer) int {
echo.Color(g.cliArguments.Color)
echo.Output(w)
exitCode := 0
if len(g.cliArguments.LimitTags) != 0 {
err := g.limitTags()
if err != nil {
echo.ErrorfMsg(err.Error())
exitCode = 1
}
}
if g.cliArguments.LimitName != "" {
err := g.limitName()
if err != nil {
echo.ErrorfMsg(err.Error())
exitCode = 1
}
}
if g.cliArguments.Sync && exitCode == 0 {
echo.InfoFMsg("Synchronizing repositories")
2021-11-02 18:19:31 +00:00
sync := commands.NewSynchronizer(g.configuration.Workspace)
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")
2021-11-05 17:19:06 +00:00
status := commands.NewStatusChecker(g.configuration.Workspace)
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
}
func (g GitRepositoryManager) describeStatus(status commands.CommandStatus) {
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) 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)
}
2021-11-02 18:19:31 +00:00
func (g *GitRepositoryManager) runCommand(cmd commands.Command) {
statusChan := make(chan commands.CommandStatus)
for _, repo := range g.configuration.Repositories {
go cmd.Command(repo, statusChan)
}
for range g.configuration.Repositories {
g.describeStatus(<-statusChan)
}
}