Skip to content

Commit

Permalink
Merge pull request #13 from spiegel-im-spiegel/refactoring-by-golangc…
Browse files Browse the repository at this point in the history
…i-lint

Refactoring by golangci lint
  • Loading branch information
spiegel-im-spiegel authored Feb 10, 2019
2 parents 53bf19d + f3d4231 commit 25f282c
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 244 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
*.so
*.dylib

# Folders
vendor/
dist/

# Test binary, build with `go test -c`
*.test

Expand All @@ -16,3 +12,8 @@ dist/

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

# Other folders and files
vendor/
dist/
go.sum
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ builds:
goarm:
- "6"
main: ./cli/mklink/
ldflags: -s -w -X github.com/spiegel-im-spiegel/mklink/cli/mklink/facade.Version={{ .Version }}
ldflags: -s -w -X github.com/spiegel-im-spiegel/mklink/cli/mklink/facade.Version=v{{ .Version }}
binary: mklink

archive:
Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@
[![GitHub license](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/spiegel-im-spiegel/mklink/master/LICENSE)
[![GitHub release](http://img.shields.io/github/release/spiegel-im-spiegel/mklink.svg)](https://github.com/spiegel-im-spiegel/mklink/releases/latest)

## Install
## Declare [gpgpdump] module

See [go.mod](https://github.com/spiegel-im-spiegel/mklink/blob/master/go.mod) file.

### Module Requirement Graph

```
$ export GO111MODULE=on
$ go get github.com/spiegel-im-spiegel/mklink@latest
$ go mod graph
github.com/spiegel-im-spiegel/mklink github.com/PuerkitoBio/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/atotto/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/inconshreveable/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/mattn/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/pkg/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spf13/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spf13/[email protected]
github.com/spiegel-im-spiegel/mklink github.com/spiegel-im-spiegel/[email protected]
github.com/spiegel-im-spiegel/mklink golang.org/x/[email protected]
github.com/spiegel-im-spiegel/mklink golang.org/x/[email protected]
github.com/spiegel-im-spiegel/[email protected] github.com/mattn/[email protected]
github.com/PuerkitoBio/[email protected] github.com/andybalholm/[email protected]
github.com/PuerkitoBio/[email protected] golang.org/x/[email protected]
github.com/andybalholm/[email protected] golang.org/x/[email protected]
```

## Usage
Expand Down
52 changes: 32 additions & 20 deletions cli/mklink/facade/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package facade

import (
"bufio"
"fmt"
"io"
"os"
"runtime"
Expand All @@ -11,15 +10,14 @@ import (
"github.com/spiegel-im-spiegel/gocli/exitcode"
"github.com/spiegel-im-spiegel/gocli/rwi"
"github.com/spiegel-im-spiegel/mklink"
"github.com/spiegel-im-spiegel/mklink/cli/mklink/interactive"
"github.com/spiegel-im-spiegel/mklink/cli/mklink/makelink"
)

var (
//Name is applicatin name
Name = "mklink"
//Version is version for applicatin
Version string
Version = "dev-version"
)

var (
Expand All @@ -37,12 +35,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
//parse options
if versionFlag {
cui.OutputErr(Name)
if len(Version) > 0 {
cui.OutputErr(fmt.Sprintf(" v%s", Version))
}
cui.OutputErrln()
return nil
return cui.OutputErrln(Name, Version)
}

strStyle, err := cmd.Flags().GetString("style")
Expand All @@ -60,37 +53,41 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
}
var log io.Writer
if len(logfile) > 0 {
file, err := os.OpenFile(logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
file, err := os.Create(logfile)
if err != nil {
return err
}
defer file.Close()
log = file
}

lnk := makelink.New(style, log)
if interactiveFlag {
i, err := interactive.New(style, log)
if err != nil {
return err
}
return i.Run()
return interactiveMode(ui, lnk)
}

lnk := makelink.New(style, cui.Writer(), log)
if len(args) > 0 {
for _, arg := range args {
err := lnk.MakeLink(arg)
r, err := lnk.MakeLink(arg)
if err != nil {
return err
}
if err := ui.WriteFrom(r); err != nil {
return err
}
_ = ui.Outputln()
}
} else {
scanner := bufio.NewScanner(cui.Reader())
for scanner.Scan() {
err := lnk.MakeLink(scanner.Text())
r, err := lnk.MakeLink(scanner.Text())
if err != nil {
return err
}
if err := ui.WriteFrom(r); err != nil {
return err
}
_ = ui.Outputln()
}
if err := scanner.Err(); err != nil {
return err
Expand All @@ -115,13 +112,13 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
defer func() {
//panic hundling
if r := recover(); r != nil {
cui.OutputErrln("Panic:", r)
_ = cui.OutputErrln("Panic:", r)
for depth := 0; ; depth++ {
pc, _, line, ok := runtime.Caller(depth)
if !ok {
break
}
cui.OutputErrln(" ->", depth, ":", runtime.FuncForPC(pc).Name(), ": line", line)
_ = cui.OutputErrln(" ->", depth, ":", runtime.FuncForPC(pc).Name(), ": line", line)
}
exit = exitcode.Abnormal
}
Expand All @@ -134,3 +131,18 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
}
return
}

/* Copyright 2017-2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
15 changes: 15 additions & 0 deletions cli/mklink/facade/facade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@ func TestPipeUrlErr(t *testing.T) {
t.Errorf("Execute(pipe) = \"%v\", want \"%v\".", exit, exitcode.Abnormal)
}
}

/* Copyright 2017-2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
62 changes: 62 additions & 0 deletions cli/mklink/facade/interactive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package facade

import (
"bytes"
"fmt"
"io"

"github.com/atotto/clipboard"
"github.com/pkg/errors"
"github.com/spiegel-im-spiegel/gocli/prompt"
"github.com/spiegel-im-spiegel/gocli/rwi"
"github.com/spiegel-im-spiegel/mklink/cli/mklink/makelink"
)

func interactiveMode(ui *rwi.RWI, cxt *makelink.Context) error {
p := prompt.New(
rwi.New(
rwi.WithReader(ui.Reader()),
rwi.WithWriter(ui.Writer()),
),
func(url string) (string, error) {
if url == "q" || url == "quit" {
return "", prompt.ErrTerminate
}
r, err := cxt.MakeLink(url)
if err != nil {
return err.Error(), nil
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil {
return "", err
}
fmt.Fprintln(buf)
res := buf.String()
return res, clipboard.WriteAll(res)
},
prompt.WithPromptString("mklink> "),
prompt.WithHeaderMessage("Input 'q' or 'quit' to stop"),
)
if !p.IsTerminal() {
return errors.New("not terminal (or pipe?)")
}
if err := p.Run(); err != nil {
return err
}
return nil
}

/* Copyright 2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
38 changes: 15 additions & 23 deletions cli/mklink/facade/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestVersionMin(t *testing.T) {
result := "mklink\n"
result := "mklink dev-version\n"

outBuf := new(bytes.Buffer)
outErrBuf := new(bytes.Buffer)
Expand All @@ -30,25 +30,17 @@ func TestVersionMin(t *testing.T) {
}
}

func TestVersionNum(t *testing.T) {
Version = "TestVersion"
result := "mklink vTestVersion\n"

outBuf := new(bytes.Buffer)
outErrBuf := new(bytes.Buffer)
ui := rwi.New(rwi.WithWriter(outBuf), rwi.WithErrorWriter(outErrBuf))
args := []string{"-v"}

exit := Execute(ui, args)
if exit != exitcode.Normal {
t.Errorf("Execute(version) = \"%v\", want \"%v\".", exit, exitcode.Normal)
}
str := outBuf.String()
if str != "" {
t.Errorf("Execute(version) = \"%v\", want \"%v\".", str, "")
}
str = outErrBuf.String()
if str != result {
t.Errorf("Execute(version) = \"%v\", want \"%v\".", str, result)
}
}
/* Copyright 2017-2019 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Loading

0 comments on commit 25f282c

Please sign in to comment.