Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
list: Use interface explicitly
Browse files Browse the repository at this point in the history
Actually make use of the `formatState` interface. This make the code
clearer.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Feb 7, 2018
1 parent e95d828 commit f3b8e73
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,25 @@ To list containers created using a non-default value for "--root":
file := defaultOutputFile
showAll := context.Bool("cc-all")

var fs formatState = formatIDList{}

if context.Bool("quiet") {
return (&formatIDList{}).Write(s, showAll, file)
}
fs = formatIDList{}
} else {

switch context.String("format") {
case "table":
return (&formatTabular{}).Write(s, showAll, file)
switch context.String("format") {
case "table":
fs = formatTabular{}

case "json":
return (&formatJSON{}).Write(s, showAll, file)
case "json":
fs = formatJSON{}

default:
return fmt.Errorf("invalid format option")
default:
return fmt.Errorf("invalid format option")
}
}

return fs.Write(s, showAll, file)
},
}

Expand Down Expand Up @@ -196,7 +201,7 @@ func getStaleAssets(old, new hypervisorDetails) []string {
return stale
}

func (f *formatIDList) Write(state []fullContainerState, showAll bool, file *os.File) error {
func (f formatIDList) Write(state []fullContainerState, showAll bool, file *os.File) error {
for _, item := range state {
_, err := fmt.Fprintln(file, item.ID)
if err != nil {
Expand All @@ -207,7 +212,7 @@ func (f *formatIDList) Write(state []fullContainerState, showAll bool, file *os.
return nil
}

func (f *formatTabular) Write(state []fullContainerState, showAll bool, file *os.File) error {
func (f formatTabular) Write(state []fullContainerState, showAll bool, file *os.File) error {
// values used by runc
flags := uint(0)
minWidth := 12
Expand Down Expand Up @@ -270,7 +275,7 @@ func (f *formatTabular) Write(state []fullContainerState, showAll bool, file *os
return w.Flush()
}

func (f *formatJSON) Write(state []fullContainerState, showAll bool, file *os.File) error {
func (f formatJSON) Write(state []fullContainerState, showAll bool, file *os.File) error {
return json.NewEncoder(file).Encode(state)
}

Expand Down

0 comments on commit f3b8e73

Please sign in to comment.