-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
395 additions
and
33 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
flag "github.com/spf13/pflag" | ||
"github.com/tamada/peripherals" | ||
"github.com/tamada/peripherals/cmd/common" | ||
"github.com/tamada/peripherals/errs" | ||
"github.com/tamada/peripherals/snip" | ||
) | ||
|
||
type SnipOpts struct { | ||
Number int | ||
snip.Snipper | ||
common.HeaderOpts | ||
common.HelpOptions | ||
} | ||
|
||
func (opts *SnipOpts) Validate() error { | ||
if opts.Head < 0 && opts.Tail < 0 && opts.Number < 0 { | ||
return errors.New("no lines specified. either options of -H, -T, -N must be specified") | ||
} | ||
if opts.Head < 0 && opts.Tail < 0 && opts.Number > 0 { | ||
opts.Head = opts.Number | ||
opts.Tail = opts.Number | ||
} | ||
return nil | ||
} | ||
|
||
func helpMessage(appName string, fs *flag.FlagSet) string { | ||
return fmt.Sprintf(`%s [OPTIONS] [FILEs...] | ||
OPTIONS | ||
%s | ||
FILE | ||
gives file name for the input. if this argument is single dash ('-') or absent, | ||
it reads strings from STDIN. | ||
if more than a single file is specified, each file is separated by a header | ||
consisting of the string '==> XXX <==' where 'XXX' is the name of the file.`, appName, fs.FlagUsages()) | ||
} | ||
|
||
func buildFlags() (*flag.FlagSet, *SnipOpts) { | ||
var opts = &SnipOpts{} | ||
var flags = flag.NewFlagSet("pskip", flag.ContinueOnError) | ||
flags.Usage = func() { fmt.Println(helpMessage("pskip", flags)) } | ||
flags.IntVarP(&opts.Head, "head", "H", -1, "print first HEAD lines (same as head command).") | ||
flags.IntVarP(&opts.Tail, "tail", "T", -1, "print last TAIL lines (same as tail command).") | ||
flags.IntVarP(&opts.Number, "number", "N", 5, "print first and last lines (default is 5).") | ||
flags.BoolVarP(&opts.LineNumber, "line-number", "n", false, "print line number with output lines.") | ||
flags.BoolVarP(&opts.SkipSnip, "no-snip-sign", "s", false, "suppress printing of snip sign and the number of snipped lines.") | ||
flags.BoolVarP(&opts.NoHeader, "no-header", "q", false, "suppress printing of headers when multiple files are being examined.") | ||
flags.BoolVarP(&opts.HelpFlag, "help", "h", false, "print this message and exit") | ||
flags.BoolVarP(&opts.VersionFlag, "version", "v", false, "print the version information and exit") | ||
flags.SortFlags = false | ||
return flags, opts | ||
} | ||
|
||
func perform(opts *SnipOpts, args []string) error { | ||
if err := opts.Validate(); err != nil { | ||
return err | ||
} | ||
center := errs.New() | ||
for _, arg := range args { | ||
if opts.IsPrintHeader(args) { | ||
fmt.Printf("===> %s <===\n", arg) | ||
} | ||
center.Push(opts.PerformEach(arg, os.Stdout)) | ||
} | ||
if len(args) == 0 { | ||
center.Push(opts.PerformEach("", os.Stdout)) | ||
} | ||
return center.SelfOrNil() | ||
} | ||
|
||
func printHelp(opts *SnipOpts, fs *flag.FlagSet) int { | ||
if opts.VersionFlag { | ||
fmt.Println(peripherals.Version("snip")) | ||
} | ||
if opts.HelpFlag { | ||
fmt.Println(helpMessage("snip", fs)) | ||
} | ||
return 0 | ||
} | ||
|
||
func goMain(args []string) int { | ||
flags, opts := buildFlags() | ||
if err := flags.Parse(args); err != nil { | ||
return common.PrintError(err, 1) | ||
} | ||
if opts.IsHelp() { | ||
return printHelp(opts, flags) | ||
} | ||
return common.PrintError(perform(opts, flags.Args()[1:]), 2) | ||
} | ||
|
||
func main() { | ||
status := goMain(os.Args) | ||
os.Exit(status) | ||
} |
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,49 @@ | ||
package main | ||
|
||
func Example_withNumber() { | ||
goMain([]string{"snip", "--line-number", "--number", "2", "../../testdata/test1.txt"}) | ||
// Output: | ||
// 1 a1 | ||
// 2 a1 | ||
// ... snip ... (4 lines) | ||
// 7 a1 | ||
// 8 A1 | ||
} | ||
|
||
func Example_headTail() { | ||
goMain([]string{"snip", "--no-snip-sign", "--head", "1", "--tail", "2", "../../testdata/test1.txt"}) | ||
// Output: | ||
// a1 | ||
// a1 | ||
// A1 | ||
} | ||
|
||
func Example_ShortLengthFile() { | ||
goMain([]string{"snip", "../../testdata/eval_script.sh"}) // 4 lines | ||
// Output: | ||
// #! /bin/sh | ||
// | ||
// echo "PLINE: $PLINE" | ||
// exec test "$PLINE" = "a1" | ||
} | ||
|
||
func Example_printHelp() { | ||
goMain([]string{"snip", "--help"}) | ||
// Output: | ||
// snip [OPTIONS] [FILEs...] | ||
// OPTIONS | ||
// -H, --head int print first HEAD lines (same as head command). (default -1) | ||
// -T, --tail int print last TAIL lines (same as tail command). (default -1) | ||
// -N, --number int print first and last lines (default is 5). (default 5) | ||
// -n, --line-number print line number with output lines. | ||
// -s, --no-snip-sign suppress printing of snip sign and the number of snipped lines. | ||
// -q, --no-header suppress printing of headers when multiple files are being examined. | ||
// -h, --help print this message and exit | ||
// -v, --version print the version information and exit | ||
// | ||
// FILE | ||
// gives file name for the input. if this argument is single dash ('-') or absent, | ||
// it reads strings from STDIN. | ||
// if more than a single file is specified, each file is separated by a header | ||
// consisting of the string '==> XXX <==' where 'XXX' is the name of the file. | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
github.com/bits-and-blooms/bitset v1.2.2 h1:J5gbX05GpMdBjCvQ9MteIg2KKDExr7DrgK+Yc15FvIk= | ||
github.com/bits-and-blooms/bitset v1.2.2/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= | ||
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA= | ||
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= |
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.