From ae752695784ed0aa531442f6281bb92c2657db1d Mon Sep 17 00:00:00 2001 From: Spiegel Date: Sun, 10 Feb 2019 12:05:02 +0900 Subject: [PATCH 1/2] Remove temporary file --- go.sum | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 go.sum diff --git a/go.sum b/go.sum deleted file mode 100644 index 83e96cc..0000000 --- a/go.sum +++ /dev/null @@ -1,27 +0,0 @@ -github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk= -github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg= -github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= -github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= -github.com/atotto/clipboard v0.1.1 h1:WSoEbAS70E5gw8FbiqFlp69MGsB6dUb4l+0AGGLiVGw= -github.com/atotto/clipboard v0.1.1/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/mattn/go-encoding v0.0.2 h1:OC1L+QXLJge9n7yIE3R5Os/UNasUeFvK3Sa4NjbDi6c= -github.com/mattn/go-encoding v0.0.2/go.mod h1:WUNsdPQLK4JYRzkn8IAdmYKFYGGJ4/9YPxdPoMumPgY= -github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= -github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spiegel-im-spiegel/gocli v0.8.1 h1:Yv0/ohht9I3CFgTclf/d1ohsq4QJW5ABXoznBqCkdiY= -github.com/spiegel-im-spiegel/gocli v0.8.1/go.mod h1:begB6B5rKPaPUr0ecaQp1kAcneZysuXdvdRoL7qtG8c= -golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY= -golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= From f3d4231bac641dc24dba7468233ffca207216d63 Mon Sep 17 00:00:00 2001 From: Spiegel Date: Sun, 10 Feb 2019 14:43:39 +0900 Subject: [PATCH 2/2] Refactoring by golangci-lint --- .gitignore | 9 +-- .goreleaser.yml | 2 +- README.md | 23 ++++++- cli/mklink/facade/facade.go | 52 +++++++++------ cli/mklink/facade/facade_test.go | 15 +++++ cli/mklink/facade/interactive.go | 62 ++++++++++++++++++ cli/mklink/facade/version_test.go | 38 +++++------ cli/mklink/interactive/interractive.go | 89 -------------------------- cli/mklink/main.go | 15 +++++ cli/mklink/makelink/makelink.go | 61 +++++++++--------- cli/mklink/makelink/makelink_test.go | 70 ++++++++++---------- go.mod | 13 ++-- mklink.go | 18 +++++- mklink_test.go | 23 ++++++- style.go | 15 +++++ style_test.go | 15 +++++ 16 files changed, 303 insertions(+), 217 deletions(-) create mode 100644 cli/mklink/facade/interactive.go delete mode 100644 cli/mklink/interactive/interractive.go diff --git a/.gitignore b/.gitignore index 2d89766..c83d876 100644 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,6 @@ *.so *.dylib -# Folders -vendor/ -dist/ - # Test binary, build with `go test -c` *.test @@ -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 diff --git a/.goreleaser.yml b/.goreleaser.yml index e6f68c9..efa8f92 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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: diff --git a/README.md b/README.md index 22308da..bc5643d 100644 --- a/README.md +++ b/README.md @@ -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/goquery@v1.5.0 +github.com/spiegel-im-spiegel/mklink github.com/atotto/clipboard@v0.1.1 +github.com/spiegel-im-spiegel/mklink github.com/inconshreveable/mousetrap@v1.0.0 +github.com/spiegel-im-spiegel/mklink github.com/mattn/go-encoding@v0.0.2 +github.com/spiegel-im-spiegel/mklink github.com/pkg/errors@v0.8.1 +github.com/spiegel-im-spiegel/mklink github.com/spf13/cobra@v0.0.3 +github.com/spiegel-im-spiegel/mklink github.com/spf13/pflag@v1.0.3 +github.com/spiegel-im-spiegel/mklink github.com/spiegel-im-spiegel/gocli@v0.9.1 +github.com/spiegel-im-spiegel/mklink golang.org/x/net@v0.0.0-20190206173232-65e2d4e15006 +github.com/spiegel-im-spiegel/mklink golang.org/x/text@v0.3.0 +github.com/spiegel-im-spiegel/gocli@v0.9.1 github.com/mattn/go-isatty@v0.0.4 +github.com/PuerkitoBio/goquery@v1.5.0 github.com/andybalholm/cascadia@v1.0.0 +github.com/PuerkitoBio/goquery@v1.5.0 golang.org/x/net@v0.0.0-20181114220301-adae6a3d119a +github.com/andybalholm/cascadia@v1.0.0 golang.org/x/net@v0.0.0-20180218175443-cbe0f9307d01 ``` ## Usage diff --git a/cli/mklink/facade/facade.go b/cli/mklink/facade/facade.go index 96409c9..b126fd0 100644 --- a/cli/mklink/facade/facade.go +++ b/cli/mklink/facade/facade.go @@ -2,7 +2,6 @@ package facade import ( "bufio" - "fmt" "io" "os" "runtime" @@ -11,7 +10,6 @@ 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" ) @@ -19,7 +17,7 @@ var ( //Name is applicatin name Name = "mklink" //Version is version for applicatin - Version string + Version = "dev-version" ) var ( @@ -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") @@ -60,7 +53,7 @@ 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 } @@ -68,29 +61,33 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command { 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 @@ -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 } @@ -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. + */ diff --git a/cli/mklink/facade/facade_test.go b/cli/mklink/facade/facade_test.go index 926d4ed..b5c57e9 100644 --- a/cli/mklink/facade/facade_test.go +++ b/cli/mklink/facade/facade_test.go @@ -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. + */ diff --git a/cli/mklink/facade/interactive.go b/cli/mklink/facade/interactive.go new file mode 100644 index 0000000..e3b1049 --- /dev/null +++ b/cli/mklink/facade/interactive.go @@ -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. + */ diff --git a/cli/mklink/facade/version_test.go b/cli/mklink/facade/version_test.go index 052a91c..38471ac 100644 --- a/cli/mklink/facade/version_test.go +++ b/cli/mklink/facade/version_test.go @@ -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) @@ -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. + */ diff --git a/cli/mklink/interactive/interractive.go b/cli/mklink/interactive/interractive.go deleted file mode 100644 index cd1dac2..0000000 --- a/cli/mklink/interactive/interractive.go +++ /dev/null @@ -1,89 +0,0 @@ -package interactive - -import ( - "bufio" - "fmt" - "io" - "os" - "strings" - - isatty "github.com/mattn/go-isatty" - "github.com/pkg/errors" - "github.com/spiegel-im-spiegel/mklink" - "github.com/spiegel-im-spiegel/mklink/cli/mklink/makelink" -) - -const ( - headerStr = "Input 'q' or 'quit' to stop" - promptStr = "mklink> " -) - -//Context class is context for making link -type Context struct { - *makelink.Context - scanner *bufio.Scanner - writer io.Writer -} - -//New returns new Context instance -func New(s mklink.Style, log io.Writer) (*Context, error) { - if !isTerminal() { - return nil, errors.New("not terminal (or pipe?)") - } - cxt := &Context{Context: makelink.New(s, os.Stdout, log), scanner: bufio.NewScanner(os.Stdin), writer: os.Stdout} - cxt.EnableClipboard() - return cxt, nil -} - -func isTerminal() bool { - if !isatty.IsTerminal(os.Stdin.Fd()) && !isatty.IsCygwinTerminal(os.Stdin.Fd()) { - return false - } - if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) { - return false - } - return true -} - -//Run is running interactive mode -func (c *Context) Run() error { - if c == nil { - return errors.New("nil pointer in interactive.Context.Run() function") - } - - fmt.Fprintln(c.writer, headerStr) - for true { - res, ok := c.prompt() - if !ok { - break - } - if len(res) == 0 { - continue - } - if err := c.MakeLink(res); err != nil { - fmt.Fprintln(c.writer, err) - } - } - - if err := c.scanner.Err(); err != nil { - return err - } - return nil -} - -func (c *Context) prompt() (string, bool) { - if c == nil { - return "", false - } - fmt.Fprint(c.writer, promptStr) - if c.scanner.Scan() { - res := strings.Trim(c.scanner.Text(), "\t ") - switch res { - case "q", "quit": - return res, false - default: - return res, true - } - } - return "", false -} diff --git a/cli/mklink/main.go b/cli/mklink/main.go index 4fb2f39..48e7d42 100644 --- a/cli/mklink/main.go +++ b/cli/mklink/main.go @@ -17,3 +17,18 @@ func main() { os.Args[1:], ).Exit() } + +/* Copyright 2017 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. + */ diff --git a/cli/mklink/makelink/makelink.go b/cli/mklink/makelink/makelink.go index 3e881b1..29cc36c 100644 --- a/cli/mklink/makelink/makelink.go +++ b/cli/mklink/makelink/makelink.go @@ -5,57 +5,54 @@ import ( "fmt" "io" - "github.com/atotto/clipboard" "github.com/pkg/errors" "github.com/spiegel-im-spiegel/mklink" ) //Context class is context for making link type Context struct { - linkStyle mklink.Style - writer io.Writer - log io.Writer - clipbrdFlag bool + linkStyle mklink.Style + log io.Writer } //New returns new Context instance -func New(s mklink.Style, writer io.Writer, log io.Writer) *Context { - return &Context{linkStyle: s, writer: writer, log: log, clipbrdFlag: false} +func New(s mklink.Style, log io.Writer) *Context { + return &Context{linkStyle: s, log: log} } //MakeLink is making link -func (c *Context) MakeLink(url string) error { +func (c *Context) MakeLink(url string) (io.Reader, error) { if c == nil { - return errors.New("nil pointer in makelink.Context.MakeLink() function") + return nil, errors.New("nil pointer in makelink.Context.MakeLink() function") } lnk, err := mklink.New(url) if err != nil { - return err + return nil, err } - buf := new(bytes.Buffer) - io.Copy(c.writer, io.TeeReader(lnk.Encode(c.linkStyle), buf)) - fmt.Fprintln(c.writer) - strLink := buf.String() - if c.clipbrdFlag { - clipboard.WriteAll(strLink) - } - if c.log != nil { - fmt.Fprintln(c.log, strLink) + rRes := lnk.Encode(c.linkStyle) + if c.log == nil { + return rRes, nil } - return nil -} - -//EnableClipboard is enabling to clipboard -func (c *Context) EnableClipboard() { - if c != nil { - c.clipbrdFlag = true + buf := new(bytes.Buffer) + if _, err := io.Copy(c.log, io.TeeReader(rRes, buf)); err != nil { + return buf, err } + fmt.Fprintln(c.log) //new line in logfile + return buf, nil } -//DisableClipboard is disabling to clipboard -func (c *Context) DisableClipboard() { - if c != nil { - c.clipbrdFlag = false - } -} +/* 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. + */ diff --git a/cli/mklink/makelink/makelink_test.go b/cli/mklink/makelink/makelink_test.go index 8aece02..725bb43 100644 --- a/cli/mklink/makelink/makelink_test.go +++ b/cli/mklink/makelink/makelink_test.go @@ -2,65 +2,69 @@ package makelink import ( "bytes" + "io" "testing" "github.com/spiegel-im-spiegel/mklink" ) func TestMakeLink(t *testing.T) { - outBuf := new(bytes.Buffer) logBuf := new(bytes.Buffer) - cxt := New(mklink.StyleMarkdown, outBuf, logBuf) - cxt.EnableClipboard() - err := cxt.MakeLink("https://git.io/vFR5M") + rRes, err := New(mklink.StyleMarkdown, logBuf).MakeLink("https://git.io/vFR5M") if err != nil { - t.Errorf("MakeLink() = \"%v\", want nil error.", err) + t.Errorf("Error of Context.MakeLink() = %v, want nil.", err) + } + outBuf := new(bytes.Buffer) + if _, err := io.Copy(outBuf, rRes); err != nil { + t.Errorf("Error of io.Copy() = %v, want nil.", err) } - res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)\n" + + res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)" str := outBuf.String() if str != res { - t.Errorf("MakeLink() = \"%v\", want \"%v\".", str, res) + t.Errorf("Context.MakeLink() = \"%v\", want \"%v\".", str, res) } str = logBuf.String() - if str != res { - t.Errorf("MakeLink() = \"%v\", want \"%v\".", str, res) + if str != res+"\n" { + t.Errorf("Context.MakeLink() = \"%v\", want \"%v\".", str, res+"\n") } } func TestMakeLinkNil(t *testing.T) { - outBuf := new(bytes.Buffer) - cxt := New(mklink.StyleMarkdown, outBuf, nil) - err := cxt.MakeLink("https://git.io/vFR5M") + rRes, err := New(mklink.StyleMarkdown, nil).MakeLink("https://git.io/vFR5M") if err != nil { - t.Errorf("MakeLink() = \"%v\", want nil error.", err) + t.Errorf("Context.MakeLink() = \"%v\", want nil error.", err) + } + outBuf := new(bytes.Buffer) + if _, err := io.Copy(outBuf, rRes); err != nil { + t.Errorf("Error of io.Copy() = %v, want nil.", err) } - res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)\n" + + res := "[GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink)" str := outBuf.String() if str != res { - t.Errorf("MakeLink() = \"%v\", want \"%v\".", str, res) + t.Errorf("Context.MakeLink() = \"%v\", want \"%v\".", str, res) } } func TestMakeLinkErr(t *testing.T) { - outBuf := new(bytes.Buffer) - cxt := New(mklink.StyleMarkdown, outBuf, nil) - err := cxt.MakeLink("https://foo.bar") + _, err := New(mklink.StyleMarkdown, nil).MakeLink("https://foo.bar") if err == nil { - t.Error("MakeLink() = nil error, not want nil error.") + t.Error("Context.MakeLink() = nil error, not want nil error.") } } -func TestClipbrdflag(t *testing.T) { - cxt := New(mklink.StyleMarkdown, nil, nil) - if cxt.clipbrdFlag { - t.Errorf("clipbrdFlag = %v, want false.", cxt.clipbrdFlag) - } - cxt.EnableClipboard() - if !cxt.clipbrdFlag { - t.Errorf("clipbrdFlag = %v, want true.", cxt.clipbrdFlag) - } - cxt.DisableClipboard() - if cxt.clipbrdFlag { - t.Errorf("clipbrdFlag = %v, want false.", cxt.clipbrdFlag) - } -} +/* 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. + */ diff --git a/go.mod b/go.mod index 940c11f..e0f7ddd 100644 --- a/go.mod +++ b/go.mod @@ -2,16 +2,13 @@ module github.com/spiegel-im-spiegel/mklink require ( github.com/PuerkitoBio/goquery v1.5.0 - github.com/andybalholm/cascadia v1.0.0 github.com/atotto/clipboard v0.1.1 - github.com/inconshreveable/mousetrap v1.0.0 + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mattn/go-encoding v0.0.2 - github.com/mattn/go-isatty v0.0.4 github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.3 - github.com/spf13/pflag v1.0.3 - github.com/spiegel-im-spiegel/gocli v0.8.1 - golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 - golang.org/x/sys v0.0.0-20190204203706-41f3e6584952 - golang.org/x/text v0.3.0 + github.com/spf13/pflag v1.0.3 // indirect + github.com/spiegel-im-spiegel/gocli v0.9.1 + golang.org/x/net v0.0.0-20190206173232-65e2d4e15006 + golang.org/x/text v0.3.0 // indirect ) diff --git a/mklink.go b/mklink.go index 45b3ce1..f80b668 100644 --- a/mklink.go +++ b/mklink.go @@ -10,11 +10,10 @@ import ( "net/http" "strings" - "golang.org/x/net/html/charset" - "github.com/PuerkitoBio/goquery" encoding "github.com/mattn/go-encoding" "github.com/pkg/errors" + "golang.org/x/net/html/charset" ) //Link class is information of URL @@ -136,3 +135,18 @@ func (lnk *Link) String() string { r, _ := lnk.JSON() return fmt.Sprint(r) } + +/* 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. + */ diff --git a/mklink_test.go b/mklink_test.go index a4f3a68..9be8903 100644 --- a/mklink_test.go +++ b/mklink_test.go @@ -21,7 +21,9 @@ func TestEncode(t *testing.T) { for _, tst := range typesTests2 { r := lnk.Encode(tst.t) buf := new(bytes.Buffer) - io.Copy(buf, r) + if _, err := io.Copy(buf, r); err != nil { + t.Errorf("io.Copy() = %v, want nil.", err) + } str := buf.String() if str != tst.name { t.Errorf("Encode(%v) = \"%v\", want \"%v\".", tst.t, str, tst.name) @@ -42,7 +44,9 @@ func TestEncodeNoUTF8(t *testing.T) { for _, tst := range typesTests3 { r := lnk.Encode(tst.t) buf := new(bytes.Buffer) - io.Copy(buf, r) + if _, err := io.Copy(buf, r); err != nil { + t.Errorf("io.Copy() = %v, want nil.", err) + } str := buf.String() if str != tst.name { t.Errorf("Encode(%v) = \"%v\", want \"%v\".", tst.t, str, tst.name) @@ -84,3 +88,18 @@ func ExampleNew() { // Output: // [GitHub - spiegel-im-spiegel/mklink: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/mklink) } + +/* 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. + */ diff --git a/style.go b/style.go index 01400a2..6fb214b 100644 --- a/style.go +++ b/style.go @@ -75,3 +75,18 @@ func (t Style) String() string { } return "unknown" } + +/* Copyright 2017 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. + */ diff --git a/style_test.go b/style_test.go index 3d9f21a..81ff763 100644 --- a/style_test.go +++ b/style_test.go @@ -41,3 +41,18 @@ func TestStyleList(t *testing.T) { t.Errorf("StylesList() = \"%v\", want \"%v\".", str, res) } } + +/* Copyright 2017 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. + */