feat: squash multiple modules to one
This commit is contained in:
26
cmd/sort-cli/config.go
Normal file
26
cmd/sort-cli/config.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "flag"
|
||||
|
||||
type Config struct {
|
||||
Key int
|
||||
Numeric bool
|
||||
Reverse bool
|
||||
Unique bool
|
||||
Sources []string
|
||||
}
|
||||
|
||||
func (c *Config) ParseFlags() {
|
||||
flag.IntVar(&c.Key, "k", 0, "sort via column")
|
||||
flag.BoolVar(&c.Numeric, "n", false, "compare according to string numerical value")
|
||||
flag.BoolVar(&c.Reverse, "r", false, "reverse the result of comparisons")
|
||||
flag.BoolVar(&c.Unique, "u", false, "output only the first of an equal run")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
c.Sources = flag.Args()
|
||||
|
||||
if len(c.Sources) == 0 {
|
||||
c.Sources = []string{"-"}
|
||||
}
|
||||
}
|
33
cmd/sort-cli/main.go
Normal file
33
cmd/sort-cli/main.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
sortcli "git.grachevko.ru/grachevko/h/sort-cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
result := run()
|
||||
|
||||
if _, err := os.Stdout.WriteString(result); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func run() string {
|
||||
cfg := &Config{}
|
||||
cfg.ParseFlags()
|
||||
|
||||
lines := sortcli.Content{}
|
||||
lines.Load(sortcli.Open(cfg.Sources))
|
||||
|
||||
if cfg.Unique {
|
||||
lines.Uniques()
|
||||
}
|
||||
|
||||
lines.Sort(cfg.Reverse)
|
||||
|
||||
return lines.String()
|
||||
}
|
71
cmd/sort-cli/main_test.go
Normal file
71
cmd/sort-cli/main_test.go
Normal file
@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFlags(t *testing.T) {
|
||||
// We manipulate the Args to set them up for the testcases
|
||||
// after this test we restore the initial args
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
|
||||
cases := []struct {
|
||||
Name string
|
||||
Args []string
|
||||
ExpectedExit int
|
||||
ExpectedOutput string
|
||||
}{
|
||||
{
|
||||
"No flags",
|
||||
[]string{"testdata/first"},
|
||||
0,
|
||||
"alabama barcelona\nbarcelona california\ncalifornia denver\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
|
||||
},
|
||||
{
|
||||
"Reverse",
|
||||
[]string{"-r", "testdata/first"},
|
||||
0,
|
||||
"волгоград геленджик\nбелгород волгоград\nамур брянск\ncalifornia denver\ncalifornia denver\nbarcelona california\nalabama barcelona",
|
||||
},
|
||||
{
|
||||
"Unique",
|
||||
[]string{"-u", "testdata/first"},
|
||||
0,
|
||||
"alabama barcelona\nbarcelona california\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
|
||||
},
|
||||
{
|
||||
"Column 2",
|
||||
[]string{"-k=2", "testdata/first"},
|
||||
0,
|
||||
"alabama barcelona\nbarcelona california\ncalifornia denver\ncalifornia denver\nамур брянск\nбелгород волгоград\nволгоград геленджик",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
// this call is required because otherwise flags panics, if args are set between flag.Parse calls
|
||||
flag.CommandLine = flag.NewFlagSet(tc.Name, flag.ExitOnError)
|
||||
// we need a value to set Args[0] to, cause flag begins parsing at Args[1]
|
||||
os.Args = append([]string{tc.Name}, tc.Args...)
|
||||
|
||||
assert.Equal(t, tc.ExpectedOutput, strings.Trim(run(), "\n"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRun(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
flag.CommandLine = flag.NewFlagSet("Bench", flag.ExitOnError)
|
||||
os.Args = append([]string{"Bench"}, "testdata/first")
|
||||
|
||||
_ = run()
|
||||
}
|
||||
}
|
7
cmd/sort-cli/testdata/first
vendored
Normal file
7
cmd/sort-cli/testdata/first
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
волгоград геленджик
|
||||
амур брянск
|
||||
alabama barcelona
|
||||
california denver
|
||||
california denver
|
||||
barcelona california
|
||||
белгород волгоград
|
Reference in New Issue
Block a user