Преглед на файлове

Improve the status reporting

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev преди 6 години
родител
ревизия
fd46f114f9
променени са 6 файла, в които са добавени 74 реда и са изтрити 29 реда
  1. 18 17
      cmd/hercules/root.go
  2. 2 0
      core.go
  3. 20 0
      internal/core/forks.go
  4. 20 0
      internal/core/forks_test.go
  5. 9 7
      internal/core/pipeline.go
  6. 5 5
      internal/core/pipeline_test.go

+ 18 - 17
cmd/hercules/root.go

@@ -45,15 +45,17 @@ type oneLineWriter struct {
 }
 
 func (writer oneLineWriter) Write(p []byte) (n int, err error) {
-	if p[len(p)-1] == '\n' {
-		p = p[:len(p)-1]
-		if len(p) > 5 && bytes.Compare(p[len(p)-5:], []byte("done.")) == 0 {
-			p = []byte("cloning...")
-		}
-		p = append(p, '\r')
-		writer.Writer.Write([]byte("\r" + strings.Repeat(" ", 80) + "\r"))
+	strp := strings.TrimSpace(string(p))
+	if strings.HasSuffix(strp, "done.") || len(strp) == 0 {
+		strp = "cloning..."
+	} else {
+		strp = strings.Replace(strp, "\n", "\033[2K\r", -1)
+	}
+	_, err = writer.Writer.Write([]byte("\033[2K\r"))
+	if err != nil {
+		return
 	}
-	n, err = writer.Writer.Write(p)
+	n, err = writer.Writer.Write([]byte(strp))
 	return
 }
 
@@ -96,7 +98,7 @@ func loadRepository(uri string, cachePath string, disableStatus bool, sshIdentit
 
 		repository, err = git.Clone(backend, nil, cloneOptions)
 		if !disableStatus {
-			fmt.Fprint(os.Stderr, strings.Repeat(" ", 80)+"\r")
+			fmt.Fprint(os.Stderr, "\033[2K\r")
 		}
 	} else if stat, err2 := os.Stat(uri); err2 == nil && !stat.IsDir() {
 		localFs := osfs.New(filepath.Dir(uri))
@@ -219,23 +221,22 @@ targets can be added using the --plugin system.`,
 		pipeline.SetFeaturesFromFlags()
 		var bar *progress.ProgressBar
 		if !disableStatus {
-			pipeline.OnProgress = func(commit, length int) {
+			pipeline.OnProgress = func(commit, length int, action string) {
 				if bar == nil {
 					bar = progress.New(length)
 					bar.Callback = func(msg string) {
-						os.Stderr.WriteString("\r" + msg)
+						os.Stderr.WriteString("\033[2K\r" + msg)
 					}
 					bar.NotPrint = true
 					bar.ShowPercent = false
 					bar.ShowSpeed = false
-					bar.SetMaxWidth(80)
-					bar.Start()
+					bar.SetMaxWidth(80).Start()
 				}
-				if commit == length-1 {
+				if action == hercules.MessageFinalize {
 					bar.Finish()
-					fmt.Fprint(os.Stderr, "\r"+strings.Repeat(" ", 80)+"\rfinalizing...")
+					fmt.Fprint(os.Stderr, "\033[2K\rfinalizing...")
 				} else {
-					bar.Set(commit)
+					bar.Set(commit).Postfix(" [" + action + "] ")
 				}
 			}
 		}
@@ -271,7 +272,7 @@ targets can be added using the --plugin system.`,
 			log.Fatalf("failed to run the pipeline: %v", err)
 		}
 		if !disableStatus {
-			fmt.Fprint(os.Stderr, "\r"+strings.Repeat(" ", 80)+"\r")
+			fmt.Fprint(os.Stderr, "\033[2K\r")
 			// if not a terminal, the user will not see the output, so show the status
 			if !terminal.IsTerminal(int(os.Stdout.Fd())) {
 				fmt.Fprint(os.Stderr, "writing...\r")

+ 2 - 0
core.go

@@ -26,6 +26,8 @@ const (
 	FloatConfigurationOption = core.FloatConfigurationOption
 	// StringsConfigurationOption reflects the array of strings value type.
 	StringsConfigurationOption = core.StringsConfigurationOption
+	// MessageFinalize is the status text reported before calling LeafPipelineItem.Finalize()-s.
+	MessageFinalize = core.MessageFinalize
 )
 
 // ConfigurationOption allows for the unified, retrospective way to setup PipelineItem-s.

+ 20 - 0
internal/core/forks.go

@@ -99,6 +99,26 @@ type runAction struct {
 	Items  []int
 }
 
+func (ra runAction) String() string {
+	switch ra.Action {
+	case runActionCommit:
+		return ra.Commit.Hash.String()[:7]
+	case runActionFork:
+		return fmt.Sprintf("fork^%d", len(ra.Items))
+	case runActionMerge:
+		return fmt.Sprintf("merge^%d", len(ra.Items))
+	case runActionEmerge:
+		return "emerge"
+	case runActionDelete:
+		return "delete"
+	case runActionHibernate:
+		return "hibernate"
+	case runActionBoot:
+		return "boot"
+	}
+	return ""
+}
+
 type orderer = func(reverse, direction bool) []string
 
 func cloneItems(origin []PipelineItem, n int) [][]PipelineItem {

+ 20 - 0
internal/core/forks_test.go

@@ -5,6 +5,8 @@ import (
 
 	"github.com/stretchr/testify/assert"
 	"gopkg.in/src-d/go-git.v4"
+	"gopkg.in/src-d/go-git.v4/plumbing"
+	"gopkg.in/src-d/hercules.v7/internal/test"
 )
 
 type testForkPipelineItem struct {
@@ -100,3 +102,21 @@ func TestInsertHibernateBoot(t *testing.T) {
 		{runActionMerge, nil, []int{2, 4}},
 	}, plan)
 }
+
+func TestRunActionString(t *testing.T) {
+	c, _ := test.Repository.CommitObject(plumbing.NewHash("c1002f4265a704c703207fafb95f1d4255bfae1a"))
+	ra := runAction{runActionCommit, c, nil}
+	assert.Equal(t, ra.String(), "c1002f4")
+	ra = runAction{runActionFork, nil, []int{1, 2, 5}}
+	assert.Equal(t, ra.String(), "fork^3")
+	ra = runAction{runActionMerge, nil, []int{1, 2, 5}}
+	assert.Equal(t, ra.String(), "merge^3")
+	ra = runAction{runActionEmerge, nil, nil}
+	assert.Equal(t, ra.String(), "emerge")
+	ra = runAction{runActionDelete, nil, nil}
+	assert.Equal(t, ra.String(), "delete")
+	ra = runAction{runActionHibernate, nil, nil}
+	assert.Equal(t, ra.String(), "hibernate")
+	ra = runAction{runActionBoot, nil, nil}
+	assert.Equal(t, ra.String(), "boot")
+}

+ 9 - 7
internal/core/pipeline.go

@@ -244,9 +244,9 @@ func MetadataToCommonAnalysisResult(meta *Metadata) *CommonAnalysisResult {
 // See the extended example of how a Pipeline works in doc.go
 type Pipeline struct {
 	// OnProgress is the callback which is invoked in Analyse() to output it's
-	// progress. The first argument is the number of complete steps and the
-	// second is the total number of steps.
-	OnProgress func(int, int)
+	// progress. The first argument is the number of complete steps, the
+	// second is the total number of steps and the third is some description of the current action.
+	OnProgress func(int, int, string)
 
 	// HibernationDistance is the minimum number of actions between two sequential usages of
 	// a branch to activate the hibernation optimization (cpu-memory trade-off). 0 disables.
@@ -307,6 +307,8 @@ const (
 	// which always exists. It indicates whether the analyzed commit is a merge commit.
 	// Checking the number of parents is not correct - we remove the back edges during the DAG simplification.
 	DependencyIsMerge = "is_merge"
+	// MessageFinalize is the status text reported before calling LeafPipelineItem.Finalize()-s.
+	MessageFinalize = "finalize"
 )
 
 // NewPipeline initializes a new instance of Pipeline struct.
@@ -705,7 +707,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 	}()
 	onProgress := pipeline.OnProgress
 	if onProgress == nil {
-		onProgress = func(int, int) {}
+		onProgress = func(int, int, string) {}
 	}
 	plan := prepareRunPlan(commits, pipeline.HibernationDistance, pipeline.DumpPlan)
 	progressSteps := len(plan) + 2
@@ -752,7 +754,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 
 	commitIndex := 0
 	for index, step := range plan {
-		onProgress(index+1, progressSteps)
+		onProgress(index+1, progressSteps, step.String())
 		if pipeline.DryRun {
 			continue
 		}
@@ -842,7 +844,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 			}
 		}
 	}
-	onProgress(len(plan)+1, progressSteps)
+	onProgress(len(plan)+1, progressSteps, MessageFinalize)
 	result := map[LeafPipelineItem]interface{}{}
 	if !pipeline.DryRun {
 		for index, item := range getMasterBranch(branches) {
@@ -851,7 +853,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 			}
 		}
 	}
-	onProgress(progressSteps, progressSteps)
+	onProgress(progressSteps, progressSteps, "")
 	result[nil] = &CommonAnalysisResult{
 		BeginTime:      plan[0].Commit.Committer.When.Unix(),
 		EndTime:        newestTime,

+ 5 - 5
internal/core/pipeline_test.go

@@ -351,17 +351,17 @@ func TestPipelineOnProgress(t *testing.T) {
 	pipeline := NewPipeline(test.Repository)
 	progressOk := 0
 
-	onProgress := func(step int, total int) {
-		if step == 1 && total == 4 {
+	onProgress := func(step int, total int, action string) {
+		if step == 1 && total == 4 && action == "emerge" {
 			progressOk++
 		}
-		if step == 2 && total == 4 {
+		if step == 2 && total == 4 && action == "af9ddc0" {
 			progressOk++
 		}
-		if step == 3 && total == 4 {
+		if step == 3 && total == 4 && action == "finalize" {
 			progressOk++
 		}
-		if step == 4 && total == 4 {
+		if step == 4 && total == 4 && action == "" {
 			progressOk++
 		}
 	}