From 6c5ee910758f454e0808092dc02f4ebc8179f833 Mon Sep 17 00:00:00 2001 From: tobigiwa Date: Tue, 26 Sep 2023 20:17:09 -0400 Subject: [PATCH] Resolves #221, to show currently running stacks. Signed-off-by: tobigiwa --- cmd/ps.go | 10 +--------- internal/stacks/stack_manager.go | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cmd/ps.go b/cmd/ps.go index 49fcebfa..0ce306a1 100644 --- a/cmd/ps.go +++ b/cmd/ps.go @@ -8,7 +8,6 @@ import ( "fmt" "strings" - "github.com/hyperledger/firefly-cli/internal/docker" "github.com/hyperledger/firefly-cli/internal/log" "github.com/hyperledger/firefly-cli/internal/stacks" "github.com/spf13/cobra" @@ -18,22 +17,15 @@ import ( var psCmd = &cobra.Command{ Use: "ps [a stack name]...", Short: "Returns information on running stacks", - Long: `ps returns a list of currently running stacks on your local machine. + Long: `ps returns currently running stacks on your local machine. It also takes a continuous list of whitespace optional arguement - stack name.`, Aliases: []string{"process"}, RunE: func(cmd *cobra.Command, args []string) error { ctx := log.WithVerbosity(context.Background(), verbose) - ctx = context.WithValue(ctx, docker.CtxIsLogCmdKey{}, true) ctx = log.WithLogger(ctx, logger) - version, err := docker.CheckDockerConfig() - if err != nil { - return err - } - ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version) - allStacks, err := stacks.ListStacks() if err != nil { return err diff --git a/internal/stacks/stack_manager.go b/internal/stacks/stack_manager.go index 0055ec77..400f8db4 100644 --- a/internal/stacks/stack_manager.go +++ b/internal/stacks/stack_manager.go @@ -27,7 +27,9 @@ import ( "path" "path/filepath" "strings" + "sync" "syscall" + "text/tabwriter" "time" "github.com/hyperledger/firefly-cli/internal/blockchain" @@ -58,6 +60,7 @@ type StackManager struct { blockchainProvider blockchain.IBlockchainProvider tokenProviders []tokens.ITokensProvider IsOldFileStructure bool + once sync.Once } func ListStacks() ([]string, error) { @@ -1101,20 +1104,30 @@ func (s *StackManager) PrintStackInfo() error { return nil } -// IsRunning prints to the stdout, details of the stack if it is running. +// IsRunning prints to the stdout, the stack name and it status as "running" or "not_running". func (s *StackManager) IsRunning() error { output, err := docker.RunDockerComposeCommandReturnsStdout(s.Stack.StackDir, "ps") if err != nil { return err } - outputtedString := string(output) - if len(outputtedString) >= 136 && strings.Contains(outputtedString, s.Stack.Name) { // the length of the stdout string if the stacks is not running is 66, and more than 136 if running. - fmt.Println(string(output)) - fmt.Println(len(string(output))) + + formatHeader := "\n %s\t%s\t " + formatBody := "\n %s\t%s\t" + + w := new(tabwriter.Writer) + w.Init(os.Stdout, 8, 8, 8, '\t', 0) + + s.once.Do(func() { + fmt.Fprintf(w, formatHeader, "STACK", "STATUS") + }) + + if strings.Contains(string(output), s.Stack.Name) { // if the output contains the stack name, it means the container is running. + fmt.Fprintf(w, formatBody, s.Stack.Name, "running") } else { - fmt.Printf("stack %s is currently not running, use `ff start %s` to run stack.\n", s.Stack.Name, s.Stack.Name) + fmt.Fprintf(w, formatBody, s.Stack.Name, "not_running") } - + fmt.Fprintln(w) + w.Flush() return nil }