status: add `-out` flag to `status` command

* Added `-out` flag to `dep status` command.
* The new flag accepts a single argument. Users can mention
  the file path where the status output should be written to.
* Blank values are ignored.
* When a file path is specified, the status output will be
  written to a file at the specified file path. If the file
  cannot be created (for some reason), a meaningful error
  message will be printed on the stdout.

Tests:
  - dep status -?
    Shows the information about the new -out flag
  - dep status -out junk/file/path
    A meaningful error is printed
  - dep status -out proper/file/path
    The status output is printed to the file at the
    mentioned file path.
This commit is contained in:
stanguturi 2018-03-27 12:33:44 -04:00 коммит произвёл akutz
Родитель 103c77564e
Коммит a71121a92e
1 изменённых файлов: 27 добавлений и 12 удалений

Просмотреть файл

@ -13,6 +13,7 @@ import (
"io"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"sync"
@ -49,19 +50,19 @@ const statusExamples = `
dep status
Displays a table of the various dependencies in the project along with
their properties such as the constraints they are bound by and the
their properties such as the constraints they are bound by and the
revision they are at.
dep status -f='{{if eq .Constraint "master"}}{{.ProjectRoot}} {{end}}'
Displays the list of package names constrained on the master branch.
The -f flag allows you to use Go templates along with it's various
The -f flag allows you to use Go templates along with it's various
constructs for formating the output data. Available flags are as follows:
` + availableTemplateVariables + `
dep status -json
Displays the dependency information in JSON format as a list of
Displays the dependency information in JSON format as a list of
project objects. Each project object contains keys which correspond
to the table column names from the standard 'dep status' command.
@ -100,16 +101,18 @@ func (cmd *statusCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.dot, "dot", false, "output the dependency graph in GraphViz format")
fs.BoolVar(&cmd.old, "old", false, "only show out-of-date dependencies")
fs.BoolVar(&cmd.missing, "missing", false, "only show missing dependencies")
fs.StringVar(&cmd.outFilePath, "out", "", "path to a file to which to write the output. Blank value will be ignored")
}
type statusCommand struct {
examples bool
json bool
template string
output string
dot bool
old bool
missing bool
examples bool
json bool
template string
output string
dot bool
old bool
missing bool
outFilePath string
}
type outputter interface {
@ -399,8 +402,20 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
return err
}
// Print the status output
ctx.Out.Print(buf.String())
if cmd.outFilePath == "" {
// Print the status output
ctx.Out.Print(buf.String())
} else {
file, err := os.Create(cmd.outFilePath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer file.Close()
if _, err := io.Copy(file, bytes.NewReader(buf.Bytes())); err != nil {
return fmt.Errorf("error writing output file: %v", err)
}
}
return nil
}