Skip to content

Commit 6529c63

Browse files
committed
Resolves hyperledger#271, info command now output information
about the stack not container. Signed-off-by: tobigiwa
1 parent e7623ee commit 6529c63

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

cmd/info.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
"github.com/hyperledger/firefly-cli/internal/docker"
2425
"github.com/hyperledger/firefly-cli/internal/log"
@@ -30,7 +31,7 @@ var infoCmd = &cobra.Command{
3031
Use: "info <stack_name>",
3132
Short: "Get info about a stack",
3233
Long: `Get info about a stack such as each container name
33-
and image version.`,
34+
and image version. If non is given, it run the "info" command for all stack on the local machine.`,
3435
RunE: func(cmd *cobra.Command, args []string) error {
3536
ctx := log.WithVerbosity(context.Background(), verbose)
3637
ctx = context.WithValue(ctx, docker.CtxIsLogCmdKey{}, true)
@@ -42,17 +43,33 @@ var infoCmd = &cobra.Command{
4243
}
4344
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)
4445

45-
stackManager := stacks.NewStackManager(ctx)
46-
if len(args) == 0 {
47-
return fmt.Errorf("no stack specified")
46+
allStacks, err := stacks.ListStacks()
47+
if err != nil {
48+
return err
4849
}
49-
stackName := args[0]
5050

51-
if err := stackManager.LoadStack(stackName); err != nil {
52-
return err
51+
if len(args) > 0 {
52+
namedStacks := make([]string, 0, len(args))
53+
for _, stackName := range args {
54+
if contains(allStacks, strings.TrimSpace(stackName)) {
55+
namedStacks = append(namedStacks, stackName)
56+
} else {
57+
fmt.Printf("stack name - %s, is not present on your local machine. Run `ff ls` to see all available stacks.\n", stackName)
58+
}
59+
}
60+
61+
allStacks = namedStacks // replace only the user specified stacks in the slice instead.
5362
}
54-
if err := stackManager.PrintStackInfo(); err != nil {
55-
return err
63+
64+
stackManager := stacks.NewStackManager(ctx)
65+
for _, stackName := range allStacks {
66+
if err := stackManager.LoadStack(stackName); err != nil {
67+
return err
68+
}
69+
70+
if err := stackManager.PrintStacksInfo(); err != nil {
71+
return err
72+
}
5673
}
5774
return nil
5875
},

cmd/ps.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var psCmd = &cobra.Command{
1919
Short: "Returns information on running stacks",
2020
Long: `ps returns currently running stacks on your local machine.
2121
22-
It also takes a continuous list of whitespace optional arguement - stack name.`,
22+
It also takes a continuous list of whitespace optional arguement - stack name. If non
23+
is given, it run the "ps" command for all stack on the local machine.`,
2324
Aliases: []string{"process"},
2425
RunE: func(cmd *cobra.Command, args []string) error {
2526

internal/stacks/stack_manager.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,39 @@ func replaceVersions(oldManifest, newManifest *types.VersionManifest, filename s
10911091
return ioutil.WriteFile(filename, []byte(s), 0755)
10921092
}
10931093

1094+
// initTabwriter takes the stackManger, the format of the Header row and a variadic arguement
1095+
// of strings (Headers) that is expected to comply with the Header format. It returns a Writer with sane defaults
1096+
// to write the body row.
1097+
func initTabwriter(s *StackManager, formatHeader string, headers ...interface{}) *tabwriter.Writer {
1098+
w := new(tabwriter.Writer)
1099+
w.Init(os.Stdout, 8, 8, 8, ' ', 0)
1100+
s.once.Do(func() {
1101+
fmt.Fprintf(w, formatHeader, headers...)
1102+
})
1103+
return w
1104+
}
1105+
// PrintStacksInfo prints to os.Stdout information about the stack.
1106+
func (s *StackManager) PrintStacksInfo() error {
1107+
formatHeader := "%-15s %-22s %-10s %-20s %-25s\n"
1108+
formatBody := " %-18s %-18s %-7s %-14s %-20s\n"
1109+
1110+
w := initTabwriter(s, formatHeader, "STACK-NAME", "BLOCKCHAIN-PROVIDER", "MEMBERS", "STATUS", "DOCKER COMPOSE DIR")
1111+
1112+
status, err := isRunning(s)
1113+
if err != nil {
1114+
return err
1115+
}
1116+
1117+
if status {
1118+
fmt.Fprintf(w, formatBody, s.Stack.Name, s.Stack.BlockchainProvider, fmt.Sprint(len(s.Stack.Members)), "running", filepath.Join(s.Stack.StackDir, "docker-compose.yml"))
1119+
} else {
1120+
fmt.Fprintf(w, formatBody, s.Stack.Name, s.Stack.BlockchainProvider, fmt.Sprint(len(s.Stack.Members)), "not_running", filepath.Join(s.Stack.StackDir, "docker-compose.yml"))
1121+
}
1122+
fmt.Fprintln(w)
1123+
w.Flush()
1124+
1125+
return nil
1126+
}
10941127
func (s *StackManager) PrintStackInfo() error {
10951128
fmt.Print("\n")
10961129
if err := s.runDockerComposeCommand("images"); err != nil {
@@ -1106,31 +1139,40 @@ func (s *StackManager) PrintStackInfo() error {
11061139

11071140
// IsRunning prints to the stdout, the stack name and it status as "running" or "not_running".
11081141
func (s *StackManager) IsRunning() error {
1109-
output, err := docker.RunDockerComposeCommandReturnsStdout(s.Stack.StackDir, "ps")
1142+
formatHeader := " %-15s %-20s\n"
1143+
formatBody := " %-13s %-20s"
1144+
1145+
w := initTabwriter(s, formatHeader, "STACK-NAME", "STATUS")
1146+
1147+
status, err := isRunning(s)
11101148
if err != nil {
11111149
return err
11121150
}
11131151

1114-
formatHeader := "\n %s\t%s\t "
1115-
formatBody := "\n %s\t%s\t"
1116-
1117-
w := new(tabwriter.Writer)
1118-
w.Init(os.Stdout, 8, 8, 8, '\t', 0)
1119-
1120-
s.once.Do(func() {
1121-
fmt.Fprintf(w, formatHeader, "STACK", "STATUS")
1122-
})
1123-
1124-
if strings.Contains(string(output), s.Stack.Name) { // if the output contains the stack name, it means the container is running.
1152+
if status {
11251153
fmt.Fprintf(w, formatBody, s.Stack.Name, "running")
11261154
} else {
11271155
fmt.Fprintf(w, formatBody, s.Stack.Name, "not_running")
11281156
}
11291157
fmt.Fprintln(w)
11301158
w.Flush()
1159+
11311160
return nil
11321161
}
11331162

1163+
// isRunning returns true if a stack on the local machine is currently running, otherwise false.
1164+
func isRunning(s *StackManager) (bool, error) {
1165+
output, err := docker.RunDockerComposeCommandReturnsStdout(s.Stack.StackDir, "ps")
1166+
if err != nil {
1167+
return false, err
1168+
}
1169+
if strings.Contains(string(output), s.Stack.Name) { // from running `docker compose ps`, if the output contains the stack name, it means the container is running.
1170+
return true, nil
1171+
}
1172+
1173+
return false, nil
1174+
}
1175+
11341176
func (s *StackManager) disableFireflyCoreContainers() error {
11351177
compose := s.buildDockerCompose()
11361178
for _, member := range s.Stack.Members {

0 commit comments

Comments
 (0)