Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

ref #2 change cli library to ogier/pflag. #3

Merged
merged 3 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"
name = "github.com/ogier/pflag"
version = "0.0.1"

[prune]
go-tests = true
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO=go
NAME := uniq2
VERSION := 0.2.0
VERSION := 1.0.0
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'main.version=$(VERSION)'
-X 'main.revision=$(REVISION)'
Expand All @@ -21,6 +21,9 @@ setup: deps update_version
git submodule update --init

update_version:
@for i in README.md docs/content/_index.md; do\
sed -e 's!Version-[0-9.]*-yellowgreen!Version-${VERSION}-yellowgreen!g' -e 's!tag/v[0-9.]*!tag/v${VERSION}!g' $$i > a ; mv a $$i; \
done
@sed 's/const VERSION = .*/const VERSION = "${VERSION}"/g' cmd/uniq2/main.go > a
@mv a cmd/uniq2/main.go
@echo "Replace version to \"${VERSION}\""
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![codebeat badge](https://codebeat.co/badges/855266ea-99d4-4d80-ac43-81a1712f0f90)](https://codebeat.co/projects/github-com-tamada-uniq2-master)
[![Go Report Card](https://goreportcard.com/badge/github.com/tamada/uniq2)](https://goreportcard.com/report/github.com/tamada/uniq2)
[![License](https://img.shields.io/badge/License-WTFPL-blue.svg)](https://github.com/tamada/uniq2/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-0.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v0.1.0)
[![Version](https://img.shields.io/badge/Version-1.0.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.0)

# uniq2

Expand All @@ -17,12 +17,38 @@ We want to delete not continuous duplicated lines with remaining the order.

## Install

### Install by Homebrew

Simply type the following commands.

```sh
$ brew tap tamada/brew
$ brew install uniq2
```

### Install by Go

Simply type the following command.

```sh
$ go get github.com/tamada/uniq2
```

## Usage

```
uniq2 [OPTIONS] [INPUT [OUTPUT]]
OPTIONS
-a, --adjacent delete only adjacent duplicated 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.
```

## License

[WTFPL](https://github.com/tamada/uniq2/blob/master/LICENSE)
94 changes: 28 additions & 66 deletions cmd/uniq2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,60 @@ import (
"fmt"
"os"

flag "github.com/ogier/pflag"
"github.com/tamada/uniq2/lib"
"github.com/urfave/cli"
)

const VERSION = "0.2.0"
const VERSION = "1.0.0"

func printHelp(app *cli.App) {
fmt.Printf(`%s
func printHelp(appName string) {
fmt.Printf(`%s [OPTIONS] [INPUT [OUTPUT]]
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)
`, appName)
}

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 {
func perform(flags *flag.FlagSet, opts *lib.Options) int {
var args, err = lib.NewArguments(opts, flags.Args()[1:])
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
if err == nil {
err = args.Perform()
}
var args, err = lib.NewArguments(options, c.Args())
if err != nil {
return err
fmt.Println(err.Error())
return 1
}
return perform(args)
return 0
}

func goMain() int {
var app = constructApp()
var err = app.Run(os.Args)
if err != nil {
fmt.Println(err.Error())
return 1
var flags, opts = buildFlagSet()
var err = flags.Parse(os.Args)
if err == nil {
return perform(flags, opts)
}
fmt.Println(err.Error())

return 0
}

func buildFlagSet() (*flag.FlagSet, *lib.Options) {
var opts = lib.Options{}
var flags = flag.NewFlagSet("uniq2", flag.ContinueOnError)
flags.Usage = func() { printHelp("uniq2") }
flags.BoolVarP(&opts.Adjacent, "adjacent", "a", false, "delete only the adjacent duplicate lines")
flags.BoolVarP(&opts.DeleteLines, "delete-lines", "d", false, "only prints deleted lines")
flags.BoolVarP(&opts.IgnoreCase, "ignore-case", "i", false, "case sensitive")
return flags, &opts
}

func main() {
// separates main function in order to run defers before exit.
var exitStatus = goMain()
Expand Down
6 changes: 3 additions & 3 deletions docs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pygmentsStyle = "pygments"

[params]
project_name = "uniq2"
project_tagline = "Eliminates duplicate lines"
project_tagline = "Deletes duplicate lines"
dateFormat = "2006-01-02"

footer = "[![GitHub](https://img.shields.io/badge/GitHub-tamada/uniq2-blueviolet.svg?logo=github)](https://github.com/tamada/uniq2) Made with [Hugo](https://gohugo.io/). Theme by [Cayman](https://github.com/zwbetz-gh/cayman-hugo-theme). Deployed to [GitHub Pages](https://pages.github.com/)."
Expand All @@ -24,8 +24,8 @@ pygmentsStyle = "pygments"
url = "/"
weight = 1
[[menu.nav]]
name = "Utilities"
url = "/utils/"
name = "Demo"
url = "/demo/"
weight = 2
[[menu.nav]]
name = "About"
Expand Down
2 changes: 1 addition & 1 deletion docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: uniq2
[![codebeat badge](https://codebeat.co/badges/855266ea-99d4-4d80-ac43-81a1712f0f90)](https://codebeat.co/projects/github-com-tamada-uniq2-master)
[![Go Report Card](https://goreportcard.com/badge/github.com/tamada/uniq2)](https://goreportcard.com/report/github.com/tamada/uniq2)
[![License](https://img.shields.io/badge/License-WTFPL-blue.svg)](https://github.com/tamada/uniq2/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-0.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v0.1.0)
[![Version](https://img.shields.io/badge/Version-1.0.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.0)

## Description

Expand Down
49 changes: 49 additions & 0 deletions docs/content/demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Demo
---

## Demo

```sh
$ cat -n testdata/test1.txt
1 a1
2 a1 # <- is the duplicate of the previous line.
3 a2
4 a2 # <- is the duplicate of the previous line.
5 a3
6 a4
7 a1 # <- is the duplicate of the first line.
8 A1
$ uniq testdata/test1.txt
a1
a2
a3
a4
A1
$ uniq -a testdata/test1.txt # same result as uniq command.
a1
a2
a3
a4
a1 # <- this line is not deleted.
A1
$ uniq -i testdata/test1.txt # ignore case
a1
a2
a3
a4
$ uniq -d testdata/test1.txt # print delete lines.
a1
a2
a1
```

## Delete duplicate entries in PATH

```sh
export PATH=$(echo $PATH | tr : '\n' | uniq2 | paste -s -d : -)
```

* `tr : '\n'` replaces `:` to `\n` of data from STDIN,
* `uniq2` deletes duplicate lines from the result of `tr`, and
* `paste -s -d : -` joins given strings with `:`.
27 changes: 0 additions & 27 deletions docs/content/utils.md

This file was deleted.