Skip to content

Commit

Permalink
Resolves hyperledger#221, to show currently running stacks.
Browse files Browse the repository at this point in the history
Signed-off-by: tobigiwa <giwaoluwatobi@gmail.com>
  • Loading branch information
tobigiwa committed Sep 27, 2023
1 parent eb74f05 commit 6c5ee91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
10 changes: 1 addition & 9 deletions cmd/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
27 changes: 20 additions & 7 deletions internal/stacks/stack_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"text/tabwriter"
"time"

"github.com/hyperledger/firefly-cli/internal/blockchain"
Expand Down Expand Up @@ -58,6 +60,7 @@ type StackManager struct {
blockchainProvider blockchain.IBlockchainProvider
tokenProviders []tokens.ITokensProvider
IsOldFileStructure bool
once sync.Once
}

func ListStacks() ([]string, error) {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 6c5ee91

Please sign in to comment.