Skip to content

Commit

Permalink
feat(kzip): add option for providing input list via file (#5970)
Browse files Browse the repository at this point in the history
The tool is often used like:

$ kzip merge -output out.kzip $(find build/ -name "*.kzip")

This works in most cases, but can fail if there are too many files and the maximum number of commandline arguments is exceeded. This new flag is a workaround that allows you to do instead:

$ find build/ -name "*kzip" > filelist.txt
$ kzip merge -output out.kzip -input_file_list filelist.txt

Additionally, the special input "file" "-" can be used to read from stdin like:

$ find build/ -name "*kzip" | kzip merge -output out.kzip -input_file_list -
  • Loading branch information
justbuchanan committed Feb 10, 2024
1 parent 27c4391 commit 052a0bb
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion kythe/go/platform/tools/kzip/mergecmd/mergecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package mergecmd // import "kythe.io/kythe/go/platform/tools/kzip/mergecmd"

import (
"bufio"
"context"
"flag"
"fmt"
Expand All @@ -40,6 +41,7 @@ type mergeCommand struct {
cmdutil.Info

output string
inputFileList string
append bool
encoding flags.EncodingFlag
recursive bool
Expand All @@ -61,6 +63,7 @@ func New() subcommands.Command {
// for merging kzip files.
func (c *mergeCommand) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&c.output, "output", "", "Path to output kzip file")
fs.StringVar(&c.inputFileList, "input_file_list", "", "Path to a newline-delimited text file containing a list of input kzip files. If '-' is specified, the file list is read from stdin")
fs.BoolVar(&c.append, "append", false, "Whether to additionally merge the contents of the existing output file, if it exists")
fs.Var(&c.encoding, "encoding", "Encoding to use on output, one of JSON, PROTO, or ALL")
fs.BoolVar(&c.recursive, "recursive", false, "Recurisvely merge .kzip files from directories")
Expand Down Expand Up @@ -90,7 +93,20 @@ func (c *mergeCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...any)
vfs.Remove(ctx, tmpName)
}
}()
archives := fs.Args()

var archives []string
if c.inputFileList != "" && len(fs.Args()) > 0 {
return c.Fail("Specify *either* --input_file_list or positional arguments, but not both")
}
if c.inputFileList != "" {
archives, err = fileListFromTextFile(c.inputFileList)
if err != nil {
return c.Fail("Error reading input file list: %v", err)
}
} else {
archives = fs.Args()
}

if c.recursive {
archives, err = recurseDirectories(ctx, archives)
if err != nil {
Expand Down Expand Up @@ -236,3 +252,30 @@ func recurseDirectories(ctx context.Context, archives []string) ([]string, error
return files, nil

}

// fileListFromTextFile returns a list of entries from a newline-delimited text
// file
func fileListFromTextFile(filePath string) ([]string, error) {
var f *os.File
if filePath == "-" {
f = os.Stdin
} else {
var err error
f, err = os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
}
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)

var kzipPaths []string
for scanner.Scan() {
if scanner.Text() != "" {
kzipPaths = append(kzipPaths, scanner.Text())
}
}

return kzipPaths, nil
}

0 comments on commit 052a0bb

Please sign in to comment.