From 56284d32d88dd0f9dc99bdf54a61a5eb4703b563 Mon Sep 17 00:00:00 2001
From: lijianguo <lijianguo@zhumengyuan.com>
Date: Thu, 15 Oct 2020 14:49:20 +0800
Subject: [PATCH] feat(ps):add ps:stop/start command pretty print

---
 ps/ps.go      | 12 +++++++-----
 ps/ps_test.go | 38 +++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/ps/ps.go b/ps/ps.go
index faa6424..83c315b 100644
--- a/ps/ps.go
+++ b/ps/ps.go
@@ -127,11 +127,13 @@ func ByType(processes api.PodsList) api.PodTypes {
 
 		// Is processtype for process doesn't exist, create a new one
 		if !exists {
-			p := api.PodsList{}
-			status := "stopped"
-			if process.Name != "" {
-				p = api.PodsList{process}
-				status = "started"
+			p := api.PodsList{process}
+			status := "started"
+			if process.State == "stopped" && process.Replicas != 0 {
+				status = "stopped"
+			}
+			if process.Name == "" {
+				p = api.PodsList{}
 			}
 			pts = append(pts, api.PodType{
 				Type:     process.Type,
diff --git a/ps/ps_test.go b/ps/ps_test.go
index 76c9d02..b6febb7 100644
--- a/ps/ps_test.go
+++ b/ps/ps_test.go
@@ -359,9 +359,9 @@ func TestByType(t *testing.T) {
 		{
 			Type: "abc",
 			PodsList: api.PodsList{
-				{Type: "abc", Name: "1", Started: started, Replicas: 3},
-				{Type: "abc", Name: "2", Started: started, Replicas: 3},
-				{Type: "abc", Name: "3", Started: started, Replicas: 3},
+				{Type: "abc", Name: "1", Started: started, Replicas: 3, State: "up"},
+				{Type: "abc", Name: "2", Started: started, Replicas: 3, State: "up"},
+				{Type: "abc", Name: "3", Started: started, Replicas: 3, State: "up"},
 			},
 			Replicas: 3,
 			Status:   "started",
@@ -375,9 +375,9 @@ func TestByType(t *testing.T) {
 		{
 			Type: "web",
 			PodsList: api.PodsList{
-				{Type: "web", Name: "test1", Started: started, Replicas: 3},
-				{Type: "web", Name: "test2", Started: started, Replicas: 3},
-				{Type: "web", Name: "test3", Started: started, Replicas: 3},
+				{Type: "web", Name: "test1", Started: started, Replicas: 3, State: "up"},
+				{Type: "web", Name: "test2", Started: started, Replicas: 3, State: "up"},
+				{Type: "web", Name: "test3", Started: started, Replicas: 3, State: "up"},
 			},
 			Replicas: 3,
 			Status:   "started",
@@ -385,9 +385,9 @@ func TestByType(t *testing.T) {
 		{
 			Type: "worker",
 			PodsList: api.PodsList{
-				{Type: "worker", Name: "a", Started: started, Replicas: 3},
-				{Type: "worker", Name: "b", Started: started, Replicas: 3},
-				{Type: "worker", Name: "c", Started: started, Replicas: 3},
+				{Type: "worker", Name: "a", Started: started, Replicas: 3, State: "up"},
+				{Type: "worker", Name: "b", Started: started, Replicas: 3, State: "up"},
+				{Type: "worker", Name: "c", Started: started, Replicas: 3, State: "up"},
 			},
 			Replicas: 3,
 			Status:   "started",
@@ -395,16 +395,16 @@ func TestByType(t *testing.T) {
 	}
 
 	input := api.PodsList{
-		{Type: "worker", Name: "c", Started: started, Replicas: 3},
-		{Type: "abc", Name: "2", Started: started, Replicas: 3},
-		{Type: "worker", Name: "b", Started: started, Replicas: 3},
-		{Type: "web", Name: "test1", Started: started, Replicas: 3},
-		{Type: "web", Name: "test3", Started: started, Replicas: 3},
-		{Type: "abc", Name: "1", Started: started, Replicas: 3},
-		{Type: "worker", Name: "a", Started: started, Replicas: 3},
-		{Type: "abc", Name: "3", Started: started, Replicas: 3},
-		{Type: "web", Name: "test2", Started: started, Replicas: 3},
-		{Type: "job", Replicas: 3},
+		{Type: "worker", Name: "c", Started: started, Replicas: 3, State: "up"},
+		{Type: "abc", Name: "2", Started: started, Replicas: 3, State: "up"},
+		{Type: "worker", Name: "b", Started: started, Replicas: 3, State: "up"},
+		{Type: "web", Name: "test1", Started: started, Replicas: 3, State: "up"},
+		{Type: "web", Name: "test3", Started: started, Replicas: 3, State: "up"},
+		{Type: "abc", Name: "1", Started: started, Replicas: 3, State: "up"},
+		{Type: "worker", Name: "a", Started: started, Replicas: 3, State: "up"},
+		{Type: "abc", Name: "3", Started: started, Replicas: 3, State: "up"},
+		{Type: "web", Name: "test2", Started: started, Replicas: 3, State: "up"},
+		{Type: "job", Replicas: 3, State: "stopped"},
 	}
 
 	actual := ByType(input)
