GitRepositoryManager/app/app.go

94 lines
2.2 KiB
Go

package app
import (
"os"
"gitlab.com/revalus/grm/commands"
"gitlab.com/revalus/grm/config"
)
const (
APP_NAME = "Git repository manager"
APP_DESCRIPTION = "Manage your repository with simple app"
VERSION = "0.2.0"
)
type GitRepositoryManager struct {
cliArguments config.CliArguments
configuration config.Configuration
console ConsoleOutput
}
func (g *GitRepositoryManager) Parse(args []string) {
co := ConsoleOutput{}
checkCriticalError := func(err error) {
if err != nil {
co.ErrorfMsg("%v", err.Error())
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)
co.Color = arguments.Color
g.console = co
g.cliArguments = arguments
g.configuration = configuration
}
func (g *GitRepositoryManager) Run() {
if g.cliArguments.Sync {
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 {
status := commands.NewStatusChecker(g.configuration.Workspace)
g.runCommand(status)
}
if g.cliArguments.Version {
g.console.InfoFMsg("Current version: %v", VERSION)
}
}
func (g GitRepositoryManager) describeStatus(status commands.CommandStatus) {
if status.Error {
g.console.ErrorStatusF("Repository \"%v\": an error occurred: %v", status.Name, status.Message)
return
}
if status.Changed {
g.console.ChangedStatusF("Repository \"%v\": %v", status.Name, status.Message)
} else {
g.console.UnchangedStatusF("Repository \"%v\": %v", status.Name, status.Message)
}
}
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)
}
}