Skip to content

Commit

Permalink
1. test coverage
Browse files Browse the repository at this point in the history
2. install.sh
3. build info
  • Loading branch information
liujianping committed Jun 26, 2019
1 parent b1fb616 commit f079d47
Show file tree
Hide file tree
Showing 584 changed files with 293,776 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist/*
bin/
dist/
.vscode/
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ before_install:
- go get github.com/mattn/goveralls
script:
- $GOPATH/bin/goveralls -service=travis-ci
- GOOS=windows go install -mod vendor github.com/liujianping/job
- GOOS=linux go install -mod vendor github.com/liujianping/job
- GOOS=darwin go install -mod vendor github.com/liujianping/job
- GOOS=windows go install -mod vendor github.com/liujianping/ts
- GOOS=linux go install -mod vendor github.com/liujianping/ts
- GOOS=darwin go install -mod vendor github.com/liujianping/ts
deploy:
- provider: script
skip_cleanup: true
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
condition: $TRAVIS_OS_NAME = linux
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
ts
===
timestamp convert & compare tool
[![GoDoc](https://godoc.org/github.com/liujianping/ts?status.svg)](https://godoc.org/github.com/liujianping/ts) [![Go Report Card](https://goreportcard.com/badge/github.com/liujianping/ts)](https://goreportcard.com/report/github.com/liujianping/ts) [![Build Status](https://travis-ci.org/liujianping/ts.svg?branch=master)](https://travis-ci.org/liujianping/ts) [![Version](https://img.shields.io/github/tag/liujianping/ts.svg)](https://github.com/liujianping/ts/releases) [![Coverage Status](https://coveralls.io/repos/github/liujianping/ts/badge.svg?branch=master)](https://coveralls.io/github/liujianping/ts?branch=master)

## Install

### Shell Install support Linux & MacOS

````bash
$: brew tap liujianping/tap && brew install ts
# binary will be $(go env GOPATH)/bin/ts
$: curl -sfL https://raw.githubusercontent.com/liujianping/ts/master/install.sh | sh -s -- -b $(go env GOPATH)/bin

# In alpine linux (as it does not come with curl by default)
$: wget -O - -q https://raw.githubusercontent.com/liujianping/ts/master/install.sh | sh -s

````

//OR
### Brew Install only MacOS
````bash
$: brew tap liujianping/tap && brew install ts
````

$: go get github.com/liujianping/ts
### Source Install
````bash
$: git clone [email protected]:liujianping/ts.git
$: cd ts
$: go install -mod vendor
````

## Quick Start
Expand Down
67 changes: 43 additions & 24 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,51 @@ package cmd

import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"

"github.com/araddon/dateparse"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/x-mod/build"
"github.com/x-mod/errors"
)

var stdin io.Reader
var stderr io.Writer
var stdout io.Writer

func exitForErr(err error) {
if err != nil {
stderr.Write([]byte(err.Error()))
os.Exit(errors.ValueFrom(err))
os.Stderr.Write([]byte(err.Error()))
os.Exit(int(errors.ValueFrom(err)))
}
}

func Main(cmd *cobra.Command, args []string) error {
//version
if viper.GetBool("version") {
fmt.Println(build.String())
return nil
}
//pipe stdin
if len(args) == 0 {
info, err := os.Stdin.Stat()
if err != nil {
return errors.Annotate(err, "stdin stat failed")
}

if info.Mode()&os.ModeNamedPipe != 0 {
d, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return errors.Annotate(err, "stdin read failed")
}
args = append(args, string(d))
}
}
//timezone
if len(viper.GetString("timezone")) > 0 {
loc, err := time.LoadLocation(viper.GetString("timezone"))
if err != nil {
panic(err.Error())
return err
}
time.Local = loc
}
Expand All @@ -43,7 +60,9 @@ func Main(cmd *cobra.Command, args []string) error {
}
for _, arg := range args {
t, err := dateparse.ParseStrict(strings.TrimSpace(arg))
exitForErr(err)
if err != nil {
return errors.Annotate(err, "parse strict")
}
t = t.Add(viper.GetDuration("add"))
t = t.Add(-viper.GetDuration("sub"))
times = append(times, t)
Expand All @@ -52,21 +71,25 @@ func Main(cmd *cobra.Command, args []string) error {
//before compare
if len(viper.GetString("before")) > 0 {
t, err := dateparse.ParseStrict(viper.GetString("before"))
exitForErr(err)
if err != nil {
return errors.Annotate(err, "parse strict")
}
if t.After(times[0]) {
os.Exit(1)
return errors.ValueErr(1)
}
os.Exit(0)
return nil
}

//after compare
if len(viper.GetString("after")) > 0 {
t, err := dateparse.ParseStrict(viper.GetString("after"))
exitForErr(err)
if err != nil {
return errors.Annotate(err, "parse strict")
}
if t.Before(times[0]) {
os.Exit(1)
return errors.ValueErr(1)
}
os.Exit(0)
return nil
}

//convert
Expand Down Expand Up @@ -122,19 +145,15 @@ func Main(cmd *cobra.Command, args []string) error {
dest = time.StampNano
default:
d, err := dateparse.ParseFormat(viper.GetString("format"))
exitForErr(err)
if err != nil {
return errors.Annotate(err, "parse format")
}
dest = d
}
fmt.Fprintln(stdout, tm.Format(dest))
fmt.Fprintln(os.Stdout, tm.Format(dest))
continue
}
fmt.Fprintln(stdout, tm.UnixNano()/1000000)
fmt.Fprintln(os.Stdout, tm.UnixNano()/1000000)
}
return nil
}

func init() {
stdin = os.Stdin
stderr = os.Stderr
stdout = os.Stdout
}
110 changes: 110 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/x-mod/errors"
)

func TestVersion(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("version", "true")
assert.Nil(t, Main(cmd, []string{}))
}

func TestArgs(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
assert.Nil(t, Main(cmd, []string{}))
}

func TestTimezone(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("timezone", "asdfasdf")
assert.NotNil(t, Main(cmd, []string{}))

cmd.Flags().Set("timezone", "Asia/Shanghai")
assert.Nil(t, Main(cmd, []string{}))
}

func TestAddSub(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("add", "1h")
assert.Nil(t, Main(cmd, []string{}))
cmd.Flags().Set("sub", "1h")
assert.Nil(t, Main(cmd, []string{}))
}

func TestBefore(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("before", "2039adfasdf")
assert.NotNil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("before", "2039/12/12")
e1 := Main(cmd, []string{"2019/06/12"})
assert.NotNil(t, e1)
assert.Equal(t, int32(1), errors.ValueFrom(e1), e1.Error())
assert.Nil(t, Main(cmd, []string{"2040/06/12"}))
}

func TestAfter(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("after", "2009fasdfa")
assert.NotNil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("after", "2009/01/01")
e2 := Main(cmd, []string{"2019/06/12"})
assert.NotNil(t, e2)
assert.Equal(t, int32(1), errors.ValueFrom(e2), e2.Error())
assert.Nil(t, Main(cmd, []string{"2008/06/12"}))
}

func TestFormat(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("format", "2039sadfadsfa")
assert.NotNil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "2039/12/12")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "ANSIC")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "UnixDate")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RubyDate")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC822")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC822Z")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC850")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC1123")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC1123Z")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC3339")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "RFC3339Nano")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "Kitchen")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "Stamp")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "StampMilli")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "StampMicro")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
cmd.Flags().Set("format", "StampNano")
assert.Nil(t, Main(cmd, []string{"2019/06/12"}))
}

func TestCovert(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
assert.NotNil(t, Main(cmd, []string{"2019asdfafd"}))
}
33 changes: 3 additions & 30 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/liujianping/ts/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var rootCmd *cobra.Command

func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ts",
Expand All @@ -29,21 +23,6 @@ func RootCmd() *cobra.Command {
(timezone) $: ts -f "2019/06/25 23:30:10" -z "Asia/Shanghai"
`,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("version") {
fmt.Println(version.String())
os.Exit(0)
}
//pipe stdin
if len(args) == 0 {
info, err := os.Stdin.Stat()
exitForErr(err)

if info.Mode()&os.ModeNamedPipe != 0 {
d, err := ioutil.ReadAll(stdin)
exitForErr(err)
args = append(args, string(d))
}
}
exitForErr(Main(cmd, args))
},
}
Expand All @@ -60,14 +39,8 @@ func RootCmd() *cobra.Command {
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
rootCmd = RootCmd()
viper.BindPFlags(rootCmd.Flags())
rootCmd := RootCmd()
exitForErr(viper.BindPFlags(rootCmd.Flags()))
rootCmd.HelpFunc()
exitForErr(rootCmd.Execute())
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.12

require (
github.com/araddon/dateparse v0.0.0-20190622164848-0fb0a474d195
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.2.2
github.com/x-mod/errors v0.1.5
github.com/stretchr/testify v1.3.0
github.com/x-mod/build v0.1.0
github.com/x-mod/errors v0.1.6
)
Loading

0 comments on commit f079d47

Please sign in to comment.