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

Commit

Permalink
Merge pull request #7 from tamada/introduce_docker
Browse files Browse the repository at this point in the history
Introduce docker
  • Loading branch information
tamada committed Mar 11, 2020
2 parents 094a8fb + 4bc54a6 commit 2131211
Show file tree
Hide file tree
Showing 15 changed files with 591 additions and 356 deletions.
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

21 changes: 21 additions & 0 deletions Dockerfile
@@ -0,0 +1,21 @@
FROM alpine:3.10.1
ARG version="1.1.0"
LABEL maintainer="Haruai Tamada" \
uniq2-version="1.0.2" \
description="Deleting duplicate lines"

RUN adduser -D uniq2 \
&& apk --no-cache add curl=7.66.0-r0 tar=1.32-r0 \
&& curl -s -L -O https://github.com/tamada/uniq2/releases/download/v${version}/uniq2-${version}_linux_amd64.tar.gz \
&& tar xfz uniq2-${version}_linux_amd64.tar.gz \
&& mv uniq2-${version} /opt \
&& ln -s /opt/uniq2-${version} /opt/uniq2 \
&& ln -s /opt/uniq2/uniq2 /usr/local/bin/uniq2 \
&& rm uniq2-${version}_linux_amd64.tar.gz

ENV HOME="home/uniq2"

WORKDIR /home/uniq2
USER uniq2

ENTRYPOINT [ "uniq2" ]
6 changes: 3 additions & 3 deletions Makefile
@@ -1,6 +1,6 @@
GO=go
NAME := uniq2
VERSION := 1.0.2
VERSION := 1.1.0
DIST := $(NAME)-$(VERSION)

all: test build
Expand All @@ -12,8 +12,8 @@ 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
@sed 's/const VERSION = .*/const VERSION = "${VERSION}"/g' cmd/uniq2/main.go > a; mv a cmd/uniq2/main.go
@sed 's/ARG version=".*"/ARG version="${VERSION}"/g' Dockerfile > a ; mv a Dockerfile
@echo "Replace version to \"${VERSION}\""

test: setup
Expand Down
20 changes: 18 additions & 2 deletions README.md
@@ -1,9 +1,9 @@
[![Build Status](https://travis-ci.org/tamada/uniq2.svg?branch=master)](https://travis-ci.org/tamada/uniq2)
[![Build Status](https://github.com/tamada/uniq2/workflows/build/badge.svg?branch=master)](https://github.com/tamada/uniq2/actions?workflow=build)
[![Coverage Status](https://coveralls.io/repos/github/tamada/uniq2/badge.svg?branch=master)](https://coveralls.io/github/tamada/uniq2?branch=master)
[![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-1.0.2-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.2)
[![Version](https://img.shields.io/badge/Version-1.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.1.0)

# uniq2

Expand Down Expand Up @@ -49,6 +49,22 @@ INPUT gives file name of input. If argument is single dash
OUTPUT represents the destination.
```
## :whale: Docker
```sh
docker run --rm -v $PWD:/home/uniq2 tamada/uniq2:1.0.3 [OPTIONS] [ARGUMENTS...]
```
The meaning of the options of above command are as follows.
* `--rm`
* remove container after running Docker.
* `-v $PWD:/home/uniq2`
* share volumen `$PWD` in the host OS to `/home/uniq2` in the container OS.
* Note that `$PWD` must be the absolute path.
[![Docker](https://img.shields.io/badge/docker-tamada%2Funiq2%3Alatest-blue?logo=docker&style=social)](https://hub.docker.com/r/tamada/uniq2)
## License
[WTFPL](https://github.com/tamada/uniq2/blob/master/LICENSE)
32 changes: 32 additions & 0 deletions adjacent_uniq.go
@@ -0,0 +1,32 @@
package uniq2

/*
AdjacentUniqer is an implementation of uniqer for adjacent lines.
*/
type AdjacentUniqer struct {
prev string
firstLine bool
}

/*
NewAdjacentUniqer creates an instance of AdjacentUniqer.
*/
func NewAdjacentUniqer() *AdjacentUniqer {
return &AdjacentUniqer{prev: "", firstLine: true}
}

/*
StreamLine tries to remove the duplicated line.
*/
func (au *AdjacentUniqer) StreamLine(line string) bool {
if au.firstLine {
au.prev = line
au.firstLine = false
return true
}
if au.prev == line {
return false
}
au.prev = line
return true
}
69 changes: 42 additions & 27 deletions cmd/uniq2/main.go
Expand Up @@ -5,13 +5,16 @@ import (
"os"

flag "github.com/spf13/pflag"
"github.com/tamada/uniq2/lib"
"github.com/tamada/uniq2"
)

const VERSION = "1.0.2"
/*
VERSION shows version of uniq2.
*/
const VERSION = "1.1.0"

func printHelp(appName string) {
fmt.Printf(`%s [OPTIONS] [INPUT [OUTPUT]]
func helpMessage(appName string) string {
return fmt.Sprintf(`%s [OPTIONS] [INPUT [OUTPUT]]
OPTIONS
-a, --adjacent delete only adjacent duplicated lines.
-d, --delete-lines only prints deleted lines.
Expand All @@ -20,46 +23,58 @@ OPTIONS
INPUT gives file name of input. If argument is single dash ('-')
or absent, the program read strings from stdin.
OUTPUT represents the destination.
`, appName)
OUTPUT represents the destination.`, appName)
}

func perform(flags *flag.FlagSet, opts *lib.Options) int {
var args, err = lib.NewArguments(opts, flags.Args()[1:])
defer args.Close()
func printError(err error, statusCode int) int {
if err == nil {
err = args.Perform()
return 0
}
fmt.Println(err.Error())
return statusCode
}

func perform(flags *flag.FlagSet, opts *uniq2.Parameters) int {
var args, err = uniq2.NewArguments(flags.Args()[1:])
if err != nil {
fmt.Println(err.Error())
return 1
return printError(err, 1)
}
return 0
defer args.Close()
err = args.Perform(opts)
return printError(err, 2)
}

func goMain() int {
func goMain(args []string) int {
// defer profile.Start(profile.ProfilePath(".")).Stop()

var flags, opts = buildFlagSet()
var err = flags.Parse(os.Args)
if err == nil {
return perform(flags, opts)
var err = flags.Parse(args)
if err != nil {
return printError(err, 1)
}
fmt.Println(err.Error())
if opts.helpFlag {
return printError(fmt.Errorf(helpMessage("uniq2")), 0)
}
return perform(flags, opts.params)
}

return 0
type options struct {
params *uniq2.Parameters
helpFlag bool
}

func buildFlagSet() (*flag.FlagSet, *lib.Options) {
var opts = lib.Options{}
func buildFlagSet() (*flag.FlagSet, *options) {
var opts = options{params: &uniq2.Parameters{}}
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")
flags.Usage = func() { fmt.Println(helpMessage("uniq2")) }
flags.BoolVarP(&opts.params.Adjacent, "adjacent", "a", false, "delete only the adjacent duplicate lines")
flags.BoolVarP(&opts.params.DeleteLines, "delete-lines", "d", false, "only prints deleted lines")
flags.BoolVarP(&opts.params.IgnoreCase, "ignore-case", "i", false, "case sensitive")
flags.BoolVarP(&opts.helpFlag, "help", "h", false, "print this message")
return flags, &opts
}

func main() {
// separates main function in order to run defers before exit.
var exitStatus = goMain()
var exitStatus = goMain(os.Args)
os.Exit(exitStatus)
}
25 changes: 25 additions & 0 deletions cmd/uniq2/main_test.go
@@ -0,0 +1,25 @@
package main

func Example_uniq2() {
goMain([]string{"uniq2", "-i", "../../testdata/test1.txt"})
// Output:
// a1
// a2
// a3
// a4
}

func Example_printHelp() {
goMain([]string{"uniq2", "--help"})
// Output:
// 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.
}
4 changes: 2 additions & 2 deletions docs/content/_index.md
Expand Up @@ -2,12 +2,12 @@
title: uniq2
---

[![Build Status](https://travis-ci.org/tamada/uniq2.svg?branch=master)](https://travis-ci.org/tamada/uniq2)
[![Build Status](https://github.com/tamada/uniq2/workflows/build/badge.svg?branch=master)](https://github.com/tamada/uniq2/actions?workflow=build)
[![Coverage Status](https://coveralls.io/repos/github/tamada/uniq2/badge.svg?branch=master)](https://coveralls.io/github/tamada/uniq2?branch=master)
[![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-1.0.2-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.0.2)
[![Version](https://img.shields.io/badge/Version-1.1.0-yellowgreen.svg)](https://github.com/tamada/uniq2/releases/tag/v1.1.0)

## Description

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Expand Up @@ -2,6 +2,4 @@ module github.com/tamada/uniq2

go 1.14

require (
github.com/spf13/pflag v1.0.5
)
require github.com/spf13/pflag v1.0.5

0 comments on commit 2131211

Please sign in to comment.