Moved the list output formatting to action/list.go
diff --git a/action/list.go b/action/list.go index 0e5158f..df8935d 100644 --- a/action/list.go +++ b/action/list.go
@@ -1,6 +1,9 @@ package action import ( + "encoding/json" + "fmt" + "os" "path/filepath" "sort" @@ -13,7 +16,8 @@ // Params: // - dir (string): basedir // - deep (bool): whether to do a deep scan or a shallow scan -func List(basedir string, deep bool) PackageList { +// - format (string): The format to output (text, json, json-pretty) +func List(basedir string, deep bool, format string) { basedir, err := filepath.Abs(basedir) if err != nil { msg.Die("Could not read directory: %s", err) @@ -40,15 +44,57 @@ } installed[i] = relPkg } - return PackageList{ + l := PackageList{ Installed: installed, Missing: h.Missing, Gopath: h.Gopath, } + + outputList(l, format) } +// PackageList contains the packages being used by their location type PackageList struct { Installed []string `json:"installed"` Missing []string `json:"missing"` Gopath []string `json:"gopath"` } + +const ( + textFormat = "text" + jsonFormat = "json" + jsonPrettyFormat = "json-pretty" +) + +func outputList(l PackageList, format string) { + switch format { + case textFormat: + msg.Puts("INSTALLED packages:") + for _, pkg := range l.Installed { + msg.Puts("\t%s", pkg) + } + + if len(l.Missing) > 0 { + msg.Puts("\nMISSING packages:") + for _, pkg := range l.Missing { + msg.Puts("\t%s", pkg) + } + } + if len(l.Gopath) > 0 { + msg.Puts("\nGOPATH packages:") + for _, pkg := range l.Gopath { + msg.Puts("\t%s", pkg) + } + } + case jsonFormat: + json.NewEncoder(os.Stdout).Encode(l) + case jsonPrettyFormat: + b, err := json.MarshalIndent(l, "", " ") + if err != nil { + msg.Die("could not unmarshal package list: %s", err) + } + fmt.Println(string(b)) + default: + msg.Die("invalid output format: must be one of: json|json-pretty|text") + } +}
diff --git a/glide.go b/glide.go index 566a86f..0b9bd4b 100644 --- a/glide.go +++ b/glide.go
@@ -37,7 +37,6 @@ package main import ( - "encoding/json" "path/filepath" "github.com/Masterminds/glide/action" @@ -588,9 +587,7 @@ vendor are only included if they are used by the project. `, Action: func(c *cli.Context) { - outputFormat := c.String("output") - pkgList := action.List(".", true) - outputList(pkgList, outputFormat) + action.List(".", true, c.String("output")) }, Flags: []cli.Flag{ cli.StringFlag{ @@ -649,42 +646,3 @@ } return a } - -const ( - textFormat = "text" - jsonFormat = "json" - jsonPrettyFormat = "json-pretty" -) - -func outputList(l action.PackageList, format string) { - switch format { - case textFormat: - msg.Puts("INSTALLED packages:") - for _, pkg := range l.Installed { - msg.Puts("\t%s", pkg) - } - - if len(l.Missing) > 0 { - msg.Puts("\nMISSING packages:") - for _, pkg := range l.Missing { - msg.Puts("\t%s", pkg) - } - } - if len(l.Gopath) > 0 { - msg.Puts("\nGOPATH packages:") - for _, pkg := range l.Gopath { - msg.Puts("\t%s", pkg) - } - } - case jsonFormat: - json.NewEncoder(os.Stdout).Encode(l) - case jsonPrettyFormat: - b, err := json.MarshalIndent(l, "", " ") - if err != nil { - msg.Die("could not unmarshal package list: %s", err) - } - fmt.Println(string(b)) - default: - msg.Die("invalid output format: must be one of: json|json-pretty|text") - } -}