-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use urfave/cli instead of kingpin2.
- Kingpin indirect depends on an obsolete package with security issues. - Submitted a PR but no response in 15 days. - kingpin2 seems abandoned. Moved on to urfave/cli.
- Loading branch information
1 parent
72f2128
commit ee81de1
Showing
4 changed files
with
363 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// errorFromSlice converts the slice of strings into a single multi-line string | ||
// and returns it, or returns nil if the error list is empty. | ||
func errorFromSlice(errmsgs []string) error { | ||
if len(errmsgs) != 0 { | ||
return errors.New(strings.Join(errmsgs, "\n")) | ||
} | ||
return nil | ||
} | ||
|
||
// checkTwoArgs shows the help message for the current context and return an | ||
// error if we don't have exactly two arguments. | ||
func checkTwoArgs(c *cli.Context) error { | ||
if c.Args().Len() != 2 { | ||
cli.ShowCommandHelp(c, c.Command.Name) | ||
return errors.New("need input and output files") | ||
} | ||
return nil | ||
} | ||
|
||
// checkMultiArgs shows the help message for the current context and return an | ||
// error if we don't have at least one argument. | ||
func checkMultiArgs(c *cli.Context) error { | ||
if c.Args().Len() < 1 { | ||
cli.ShowCommandHelp(c, c.Command.Name) | ||
return errors.New("no files to process") | ||
} | ||
return nil | ||
} | ||
|
||
func runnerFromContext(ctx context.Context) *runner { | ||
ret, ok := ctx.Value(runnerKey).(*runner) | ||
if !ok { | ||
panic("internal error: Unable to retrieve runner from context.") | ||
} | ||
return ret | ||
} | ||
|
||
func actionMerge(c *cli.Context) error { | ||
return remux(c.Args().Slice(), c.String("output"), *runnerFromContext(c.Context), c.Bool("subs")) | ||
} | ||
|
||
func actionOnly(c *cli.Context) error { | ||
if err := checkTwoArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
infile := c.Args().Get(0) | ||
outfile := c.Args().Get(1) | ||
|
||
run := *runnerFromContext(c.Context) | ||
|
||
mkv := mustParseFile(infile) | ||
tfi, err := extract(mkv, c.Int("track"), run) | ||
defer os.Remove(tfi.fname) | ||
if err != nil { | ||
return fmt.Errorf("%s: %v", infile, err) | ||
} | ||
return submux(infile, outfile, true, run) | ||
} | ||
|
||
func actionPrint(c *cli.Context) error { | ||
if err := checkMultiArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
var errmsgs []string | ||
|
||
for _, fname := range c.Args().Slice() { | ||
output, err := format(c.String("format"), fname) | ||
if err != nil { | ||
errmsgs = append(errmsgs, fmt.Sprintf("%s: %v", fname, err)) | ||
continue | ||
} | ||
fmt.Println(output) | ||
} | ||
return errorFromSlice(errmsgs) | ||
} | ||
|
||
func actionRemux(c *cli.Context) error { | ||
if err := checkTwoArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
infile := c.Args().Get(0) | ||
outfile := c.Args().Get(1) | ||
run := *runnerFromContext(c.Context) | ||
|
||
return remux([]string{infile}, outfile, run, true) | ||
} | ||
|
||
func actionRename(c *cli.Context) error { | ||
if err := checkMultiArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
var errmsgs []string | ||
|
||
for _, fname := range readable(c.Args().Slice()) { | ||
err := rename(c.String("format"), fname, c.Bool("dry-run")) | ||
if err != nil { | ||
errmsgs = append(errmsgs, fmt.Sprintf("%s: %v", fname, err)) | ||
} | ||
} | ||
return errorFromSlice(errmsgs) | ||
} | ||
|
||
func actionSetDefault(c *cli.Context) error { | ||
if err := checkMultiArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
run := *runnerFromContext(c.Context) | ||
|
||
var errmsgs []string | ||
|
||
for _, fname := range readable(c.Args().Slice()) { | ||
mkv := mustParseFile(fname) | ||
err := setdefault(mkv, c.Int("track"), run) | ||
if err != nil { | ||
errmsgs = append(errmsgs, fmt.Sprintf("%s: %s", fname, err)) | ||
} | ||
} | ||
return errorFromSlice(errmsgs) | ||
} | ||
|
||
func actionSetDefaultByLang(c *cli.Context) error { | ||
if err := checkMultiArgs(c); err != nil { | ||
return err | ||
} | ||
|
||
run := *runnerFromContext(c.Context) | ||
|
||
var errmsgs []string | ||
|
||
for _, fname := range readable(c.Args().Slice()) { | ||
mkv := mustParseFile(fname) | ||
track, err := trackByLanguage(mkv, c.StringSlice("lang"), c.StringSlice("ignore")) | ||
if err != nil { | ||
errmsgs = append(errmsgs, fmt.Sprintf("%s: %v", fname, err)) | ||
continue | ||
} | ||
err = setdefault(mkv, track, run) | ||
if err != nil { | ||
errmsgs = append(errmsgs, fmt.Sprintf("%s: %v", fname, err)) | ||
} | ||
} | ||
return errorFromSlice(errmsgs) | ||
} | ||
|
||
func actionShow(c *cli.Context) error { | ||
if err := checkMultiArgs(c); err != nil { | ||
return err | ||
} | ||
for _, fname := range readable(c.Args().Slice()) { | ||
mkv := mustParseFile(fname) | ||
show(mkv, c.Bool("uid")) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.