Files
graylog-project-cli/cmd/update.go
Bernd Ahlers 385ec22572 Print all errors at the end of update command
This makes it easier to spot any errors when updating the project
repositories by displaying them at the end of the command.
2023-08-08 17:47:05 +02:00

92 lines
2.5 KiB
Go

package cmd
import (
"errors"
c "github.com/Graylog2/graylog-project-cli/config"
"github.com/Graylog2/graylog-project-cli/git"
"github.com/Graylog2/graylog-project-cli/logger"
"github.com/Graylog2/graylog-project-cli/manifest"
p "github.com/Graylog2/graylog-project-cli/project"
"github.com/Graylog2/graylog-project-cli/projectstate"
"github.com/Graylog2/graylog-project-cli/repo"
"github.com/Graylog2/graylog-project-cli/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var updateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"up"},
Short: "Update all repositories for the current manifest",
Long: `
Update all repositories for the current manifest.
This is the equivalent of executing the following git commands in every repository:
git fetch --all --tags (--prune)
git merge --ff-only origin/<branch-name> (--ff with relaxed flag)
`,
Run: updateCommand,
}
func init() {
RootCmd.AddCommand(updateCmd)
updateCmd.Flags().BoolP("prune", "p", false, "Prune local branches that no longer exists in the remote repository. (i.e. \"git fetch --prune\")")
updateCmd.Flags().BoolP("relaxed", "r", false, "Relax merge option - don't require a fast-forward merge. (i.e. \"git merge --ff\")")
viper.BindPFlag("update.prune", updateCmd.Flags().Lookup("prune"))
viper.BindPFlag("update.relaxed", updateCmd.Flags().Lookup("relaxed"))
}
func updateCommand(cmd *cobra.Command, args []string) {
config := c.Get()
manifestFiles := manifest.ReadState().Files()
repoMgr := repo.NewRepoManager(config)
project := p.New(config, manifestFiles)
projectRepoPath, err := git.ToplevelPath()
if err != nil {
logger.Fatal("Couldn't get top-level path: %v", err)
}
logger.Info("Updating %v", projectRepoPath)
utils.InDirectory(projectRepoPath, func() {
args := []string{"pull"}
if config.Update.Prune {
args = append(args, "--prune")
}
if config.Update.Relaxed {
args = append(args, "--ff")
} else {
args = append(args, "--ff-only")
}
git.Git(args...)
})
var updateErrors []error
p.ForEachSelectedModule(project, func(module p.Module) {
logger.Info("Updating %v", module.Path)
if e := repoMgr.UpdateRepository(module); len(e) > 0 {
updateErrors = append(updateErrors, e...)
}
})
projectstate.Sync(project, config)
for _, err := range updateErrors {
var loggableErr *logger.LoggableError
if errors.As(err, &loggableErr) {
logger.Error("ERROR: %s", loggableErr)
for _, msg := range loggableErr.Messages {
logger.Error(" %s", msg)
}
} else {
logger.Error("ERROR: %s", err)
}
}
}