Skip to content

Commit

Permalink
feat: adds quiet option to list command.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Sep 4, 2023
1 parent 86011cd commit fc2308a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ Lists running Imposter mocks for the current engine type.
Usage:
imposter list [flags]
Aliases:
list, ls
Flags:
-t, --engine-type string Imposter engine type (valid: docker,jvm - default "docker")
-x, --exit-code-health Set exit code based on mock health
-h, --help help for down
-h, --help help for list
-q, --quiet Quieten output; only print ID
```

### Install plugin
Expand Down
16 changes: 12 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
var listFlags = struct {
engineType string
healthExitCode bool
quiet bool
}{}

// listCmd represents the list command
Expand All @@ -37,18 +38,19 @@ var listCmd = &cobra.Command{
Short: "List running mocks",
Long: `Lists running Imposter mocks for the current engine type.`,
Run: func(cmd *cobra.Command, args []string) {
listMocks(engine.GetConfiguredType(listFlags.engineType))
listMocks(engine.GetConfiguredType(listFlags.engineType), listFlags.quiet)
},
}

func init() {
listCmd.Flags().StringVarP(&listFlags.engineType, "engine-type", "t", "", "Imposter engine type (valid: docker,jvm - default \"docker\")")
listCmd.Flags().BoolVarP(&listFlags.healthExitCode, "exit-code-health", "x", false, "Set exit code based on mock health")
listCmd.Flags().BoolVarP(&listFlags.quiet, "quiet", "q", false, "Quieten output; only print ID")
registerEngineTypeCompletions(listCmd)
rootCmd.AddCommand(listCmd)
}

func listMocks(engineType engine.EngineType) {
func listMocks(engineType engine.EngineType, quiet bool) {
configDir := filepath.Join(os.TempDir(), "imposter-list")
mockEngine := engine.BuildEngine(engineType, configDir, engine.StartOptions{})

Expand All @@ -61,12 +63,18 @@ func listMocks(engineType engine.EngineType) {
var rows [][]string
for _, mock := range mocks {
engine.PopulateHealth(&mock)
rows = append(rows, []string{mock.ID, mock.Name, strconv.Itoa(mock.Port), string(mock.Health)})
if quiet {
os.Stdout.WriteString(mock.ID + "\n")
} else {
rows = append(rows, []string{mock.ID, mock.Name, strconv.Itoa(mock.Port), string(mock.Health)})
}
if mock.Health != engine.MockHealthHealthy {
anyFailed = true
}
}
renderMocks(rows)
if !quiet {
renderMocks(rows)
}

if listFlags.healthExitCode {
// if there is at least one mock, and all mocks are healthy, return status 0
Expand Down

0 comments on commit fc2308a

Please sign in to comment.