// Copyright 2019-2021 Luke Shumaker // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // Command fi-progress is a filter that prints "progress" commands to // stderr. package main import ( "context" "fmt" "os" "regexp" "git.lukeshu.com/go/libfastimport" "github.com/spf13/pflag" "git.parabola.nu/~lukeshu/fastimport-go-utils/fiutil" ) func usage() { fmt.Printf("Usage: git fast-export | %s REGEX1 [REGEX2...] | git fast-import\n", os.Args[0]) fmt.Printf(" or: %s <-h|--help>\n", os.Args[0]) fmt.Printf("A filter that prints \"progress\" commands to stderr\n") fmt.Printf(` The arguments are a series of regexps; each regex expression is given a line of output, and any "progress" commands matching that regex update that line. For example: $ printf 'progress %%s\n' "the foo is back" "bars are closed" "i need food" | ` + os.Args[0] + ` foo bar >/dev/null progress i need food progress bars are closed First the "the foo is back" progress command sets "foo"'s line, but then the "i need food" progress command overwrites that because it also matches the regexp "foo". Progress commands that don't match one of the regexps are written to stdout unchanged. This program is smart about not spending I/O writing duplicate lines and about coalescing extremely rapid line changes. `) os.Exit(0) } func main() { var args struct { help bool } argparser := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError) argparser.BoolVarP(&args.help, "help", "h", false, "") err := argparser.Parse(os.Args[1:]) if args.help { usage() } if err != nil { fiutil.ErrUsage(err.Error()) } if err := fiutil.NArgsAtLeast(argparser, 1); err != nil { fiutil.ErrUsage(err.Error()) } frontend := libfastimport.NewFrontend(os.Stdin, os.Stdin, nil) backend := libfastimport.NewBackend(os.Stdout, os.Stdout, nil) filter := &Filter{ backend: backend, } for _, str := range argparser.Args() { if str == "--help" { usage() } re, err := regexp.Compile(str) if err != nil { fiutil.ErrUsage(fmt.Sprintf("bad regexp: %q: %v", str, err)) } filter.filters = append(filter.filters, re) } filter.writer = NewWriter(len(filter.filters)) ctx, ctxCancel := context.WithCancel(context.Background()) done := make(chan struct{}) go func() { filter.writer.Run(ctx) close(done) }() err = fiutil.RunHandler(frontend, filter) ctxCancel() <-done if err != nil { fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err) os.Exit(1) } }