This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
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
11 changed files
with
433 additions
and
95 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor | ||
uniq2 | ||
|
||
### https://raw.github.com/github/gitignore/5c5a1b536b6f9bf11515518f03d58addaf29f681/Go.gitignore | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,103 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/tamada/uniq2/lib" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
const VERSION = "0.2.0" | ||
|
||
func printHelp(app *cli.App) { | ||
fmt.Printf(`%s | ||
OPTIONS | ||
-a, --adjacent delete only adjacent duplicated lines. | ||
-c, --show-counts show counts of deleted lines. | ||
-d, --delete-lines only prints deleted lines. | ||
-i, --ignore-case case sensitive. | ||
-h, --help print this message. | ||
INPUT gives file name of input. If argument is single dash ('-') | ||
or absent, the program read strings from stdin. | ||
OUTPUT represents the destination. | ||
`, app.Usage) | ||
} | ||
|
||
func buildFlags() []cli.Flag { | ||
return []cli.Flag{ | ||
cli.BoolFlag{ | ||
Name: "adjacent, a", | ||
Usage: "delete only adjacent duplicated lines.", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "show-counts, c", | ||
Usage: "show counts of deleted lines.", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "delete-lines, d", | ||
Usage: "only prints deleted lines.", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "ignore-case, i", | ||
Usage: "case sensitive", | ||
}, | ||
} | ||
} | ||
|
||
func constructApp() *cli.App { | ||
var app = cli.NewApp() | ||
app.Name = "uniq2" | ||
app.Usage = "Eliminates duplicated lines" | ||
app.UsageText = "uniq2 [OPTIONS] [INPUT [OUTPUT]]" | ||
app.Version = VERSION | ||
app.Flags = buildFlags() | ||
app.Action = func(c *cli.Context) error { | ||
return action(app, c) | ||
} | ||
return app | ||
} | ||
|
||
func parseOptions(c *cli.Context) *lib.Options { | ||
return &lib.Options{ | ||
Adjacent: c.Bool("adjacent"), | ||
ShowCounts: c.Bool("show-counts"), | ||
DeleteLines: c.Bool("delete-lines"), | ||
IgnoreCase: c.Bool("ignore-case"), | ||
} | ||
} | ||
|
||
func perform(args *lib.Arguments) error { | ||
defer args.Close() | ||
return args.Perform() | ||
} | ||
|
||
func action(app *cli.App, c *cli.Context) error { | ||
var options = parseOptions(c) | ||
if c.Bool("help") { | ||
printHelp(app) | ||
return nil | ||
} | ||
var args, err = lib.NewArguments(options, c.Args()) | ||
if err != nil { | ||
return err | ||
} | ||
return perform(args) | ||
} | ||
|
||
func goMain() int { | ||
var app = constructApp() | ||
var err = app.Run(os.Args) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func main() { | ||
// separates main function in order to run defers before exit. | ||
var exitStatus = goMain() | ||
os.Exit(exitStatus) | ||
} |
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,169 @@ | ||
package lib | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
/* | ||
Options represents option parameter values. | ||
*/ | ||
type Options struct { | ||
Adjacent bool | ||
ShowCounts bool | ||
DeleteLines bool | ||
IgnoreCase bool | ||
} | ||
|
||
/* | ||
Arguments represents the command line arguments. | ||
*/ | ||
type Arguments struct { | ||
Options *Options | ||
Input io.Reader | ||
Output io.Writer | ||
} | ||
|
||
type entry struct { | ||
line string | ||
count map[bool]int | ||
} | ||
|
||
/* | ||
Close finalize the files of Arguments. | ||
*/ | ||
func (args *Arguments) Close() { | ||
var inputFile, ok1 = args.Input.(*os.File) | ||
if ok1 { | ||
inputFile.Close() | ||
} | ||
var outputFile, ok2 = args.Output.(*os.File) | ||
if ok2 { | ||
outputFile.Close() | ||
} | ||
} | ||
|
||
/* | ||
NewArguments construct an instance of Arguments with the given parameters. | ||
*/ | ||
func NewArguments(opts *Options, args []string) (*Arguments, error) { | ||
var arguments = Arguments{Options: opts} | ||
var input, output, err = parseCliArguments(args) | ||
arguments.Input = input | ||
arguments.Output = output | ||
return &arguments, err | ||
} | ||
|
||
func parseCliArguments(args []string) (*os.File, *os.File, error) { | ||
switch len(args) { | ||
case 0: | ||
return os.Stdin, os.Stdout, nil | ||
case 1: | ||
var input, err = createInput(args[0]) | ||
return input, os.Stdout, err | ||
case 2: | ||
var input, output *os.File | ||
var err error | ||
input, err = createInput(args[0]) | ||
if err == nil { | ||
output, err = createOutput(args[1]) | ||
} | ||
return input, output, err | ||
} | ||
return nil, nil, fmt.Errorf("too many arguments: %v", args) | ||
} | ||
|
||
func createOutput(output string) (*os.File, error) { | ||
if output == "-" { | ||
return os.Stdout, nil | ||
} | ||
return os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0644) | ||
} | ||
|
||
func createInput(input string) (*os.File, error) { | ||
if input == "-" { | ||
return os.Stdin, nil | ||
} | ||
return os.Open(input) | ||
} | ||
|
||
/* | ||
Perform reads files from args.Input and writes result to args.Output. | ||
*/ | ||
func (args *Arguments) Perform() error { | ||
var scanner = bufio.NewScanner(args.Input) | ||
var writer = bufio.NewWriter(args.Output) | ||
|
||
return args.runUnique(scanner, writer) | ||
} | ||
|
||
func isPrint(uniqFlag bool, deleteLineFlag bool) bool { | ||
return !uniqFlag && !deleteLineFlag || | ||
uniqFlag && deleteLineFlag | ||
} | ||
|
||
func updateDatabase(line string, uniqFlag bool, entries []entry) []entry { | ||
for i, entry := range entries { | ||
if entry.line == line { | ||
entries[i].count[uniqFlag]++ | ||
} | ||
} | ||
return entries | ||
} | ||
|
||
func upsertDatabase(line string, uniqFlag bool, entries []entry) []entry { | ||
if uniqFlag { | ||
return updateDatabase(line, uniqFlag, entries) | ||
} | ||
var entry = entry{line: line, count: map[bool]int{uniqFlag: 1}} | ||
return append(entries, entry) | ||
} | ||
|
||
func (args *Arguments) runUnique(scanner *bufio.Scanner, writer *bufio.Writer) error { | ||
var entries = []entry{} | ||
for scanner.Scan() { | ||
var line = scanner.Text() | ||
var uniqFlag, lineToDB = args.isUniqLine(line, entries) | ||
entries = upsertDatabase(lineToDB, uniqFlag, entries) | ||
if isPrint(uniqFlag, args.Options.DeleteLines) { | ||
writer.WriteString(line) | ||
writer.WriteString("\n") | ||
} | ||
} | ||
writer.Flush() | ||
return nil | ||
} | ||
|
||
func (opts *Options) match(readLine string, lineOfDB entry) bool { | ||
return readLine == lineOfDB.line | ||
} | ||
|
||
func (opts *Options) isFoundLineInAdjacentDB(line string, list []entry) bool { | ||
if len(list) == 0 { | ||
return false | ||
} | ||
return opts.match(line, list[len(list)-1]) | ||
} | ||
|
||
func (opts *Options) isFoundLineInDB(line string, list []entry) bool { | ||
for _, lineInList := range list { | ||
if line == lineInList.line { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (args *Arguments) isUniqLine(line string, list []entry) (flag bool, lineToDB string) { | ||
lineToDB = line | ||
if args.Options.IgnoreCase { | ||
lineToDB = strings.ToLower(line) | ||
} | ||
if args.Options.Adjacent { | ||
return args.Options.isFoundLineInAdjacentDB(lineToDB, list), lineToDB | ||
} | ||
return args.Options.isFoundLineInDB(lineToDB, list), lineToDB | ||
} |
Oops, something went wrong.