Improve verbose status output

Allow multiple "-v" flags to increase the verbosity.
This commit is contained in:
Bernd Ahlers
2022-07-06 11:58:45 +02:00
parent 85fff0cd39
commit b7971db06a
3 changed files with 45 additions and 17 deletions

View File

@@ -20,7 +20,7 @@ var (
var cfgFile string
var repositoryRoot string
var debug bool
var verbose bool
var verbose int
var selectedModules string
var loggerPrefix string
var noUpdateCheck bool
@@ -90,7 +90,7 @@ func init() {
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
RootCmd.PersistentFlags().StringVar(&repositoryRoot, "repository-root", config.DefaultRepositoryRoot, "Git repository root")
RootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "enable debug output (default: false)")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose output (default: false)")
RootCmd.PersistentFlags().CountVarP(&verbose, "verbose", "v", "enable verbose output - use multiple times to increase verbosity")
RootCmd.PersistentFlags().StringVarP(&selectedModules, "selected-modules", "M", "", "apply command to given modules (comma separated)")
RootCmd.PersistentFlags().StringVarP(&loggerPrefix, "logger-prefix", "", "", "output logger prefix")
RootCmd.PersistentFlags().BoolVarP(&noUpdateCheck, "disable-update-check", "U", false, "disable checking for graylog-project-cli updates")

View File

@@ -51,33 +51,42 @@ func statusCommand(cmd *cobra.Command, args []string) {
// Don't display shortened commit IDs in CI environments to avoid future ambiguities regarding the
// shortened commit IDs
commitId = git.GitValue("rev-parse", "HEAD")
} else {
} else {
commitId = git.GitValue("rev-parse", "--short", "HEAD")
}
revision := git.GitValue("rev-parse", "--abbrev-ref", "HEAD")
gitStatus := git.GitValue("status", "--porcelain")
filesModified, filesDeleted, filesAdded := 0, 0, 0
filesModifiedStaged, filesDeletedStaged, filesAddedStaged := 0, 0, 0
for _, l := range strings.Split(gitStatus, "\n") {
line := strings.TrimSpace(l)
for _, line := range strings.Split(gitStatus, "\n") {
if strings.HasPrefix(line, "M") {
filesModifiedStaged++
}
if strings.HasPrefix(line, " M") {
filesModified++
}
if strings.HasPrefix(line, "D") {
filesDeletedStaged++
}
if strings.HasPrefix(line, " D") {
filesDeleted++
}
if strings.HasPrefix(line, "A") {
filesAddedStaged++
}
if strings.HasPrefix(line, " A") {
filesAdded++
}
}
if !module.HasParent() {
logger.Info(" %-"+strconv.Itoa(int(maxNameLength))+"s %s (branch: %s, commit: %s)", module.Name, module.Version(), revision, commitId)
if config.Verbose {
if config.Verbose > 1 {
logger.ColorInfo(color.FgYellow, " <no parent>")
}
} else {
if config.Verbose {
if config.Verbose > 1 {
logger.Info(" %-"+strconv.Itoa(int(maxNameLength))+"s %s (branch: %s, commit: %s)", module.Name, module.Version(), revision, commitId)
logger.ColorInfo(color.FgYellow, " Parent groupId: %s", module.ParentGroupId())
logger.ColorInfo(color.FgYellow, " Parent artifactId: %s", module.ParentArtifactId())
@@ -87,24 +96,43 @@ func statusCommand(cmd *cobra.Command, args []string) {
logger.Info(" %-"+strconv.Itoa(int(maxNameLength))+"s %s (branch: %s, commit: %s, parent: %s)", module.Name, module.Version(), revision, commitId, module.ParentVersion())
}
}
if !config.Verbose && (filesModified > 0 || filesDeleted > 0 || filesAdded > 0) {
if config.Verbose == 0 &&
((filesModified > 0 || filesDeleted > 0 || filesAdded > 0) ||
(filesModifiedStaged > 0 || filesDeletedStaged > 0 || filesAddedStaged > 0)) {
logger.Printf(" Git status:")
if filesAdded > 0 {
logger.ColorPrintf(color.FgGreen, " %d added", filesAdded)
if filesAdded > 0 || filesAddedStaged > 0 {
logger.ColorPrintf(color.FgGreen, " %d added", filesAdded+filesAddedStaged)
}
if filesDeleted > 0 {
logger.ColorPrintf(color.FgRed, " %d deleted", filesDeleted)
if filesDeleted > 0 || filesDeletedStaged > 0 {
logger.ColorPrintf(color.FgRed, " %d deleted", filesDeleted+filesDeletedStaged)
}
if filesModified > 0 {
logger.ColorPrintf(color.FgYellow, " %d modified", filesModified)
if filesModified > 0 || filesModifiedStaged > 0 {
logger.ColorPrintf(color.FgYellow, " %d modified", filesModified+filesModifiedStaged)
}
logger.ColorPrintf(color.FgYellow, "\n")
}
if config.Verbose > 0 && config.Verbose < 2 {
statusValue := git.GitValue("status", "-s")
statusLines := strings.Split(strings.TrimSuffix(statusValue, "\n"), "\n")
if len(statusLines) > 0 && statusLines[0] != "" {
logger.Printf(" Git status:\n")
for _, line := range statusLines {
var lineColor color.Attribute
switch {
case strings.HasPrefix(line, "M") || strings.HasPrefix(line, "D") || strings.HasPrefix(line, "A"):
lineColor = color.FgGreen
default:
lineColor = color.FgRed
}
logger.ColorPrintf(lineColor, " %s\n", line)
}
}
}
})
}
if !config.Verbose {
if config.Verbose <= 1 {
return
}

View File

@@ -34,7 +34,7 @@ type Config struct {
SelectedModules string `mapstructure:"selected-modules"`
Checkout Checkout `mapstructure:"checkout"`
ApplyManifest ApplyManifest `mapstructure:"apply-manifest"`
Verbose bool `mapstructure:"verbose"`
Verbose int `mapstructure:"verbose"`
NoUpdateCheck bool `mapstructure:"disable-update-check"`
ForceHttpsRepos bool `mapstructure:"force-https-repos"`
Update Update `mapstructure:"update"`