From 992cb1b5e25d5d15d93b078a5d28de82da4bd96b Mon Sep 17 00:00:00 2001 From: Martin Multhaupt Date: Wed, 9 Oct 2019 16:37:22 +0200 Subject: [PATCH] Initial commit --- CHANGES.md | 2 ++ LICENSE | 21 +++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 CHANGES.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..5336802 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,2 @@ +# 0.1.0 (2019-10-09) +- Initial release. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aa2645 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcab973 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# cb +## Usage +Copy file contents to clipboard: +```sh +cb file.txt +``` + +Copy stdin contents to clipboard: +```sh +echo foo | cb +``` + +Copy stdin contents to clipboard, truncating 1 trailing line break if it exists: +```sh +echo foo | cb -n +``` + +## Installation +```sh +go get -u github.com/setlog./cb +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..854196c --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/setlog/cb + +go 1.13 + +require github.com/atotto/clipboard v0.1.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..243ea9b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY= +github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..8bdc1b6 --- /dev/null +++ b/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "os" + + "github.com/atotto/clipboard" +) + +func main() { + flagTruncNewLine := flag.Bool("n", false, "Set to truncate one trailing line break if it exists.") + flag.Parse() + + var cb string + var err error + if flag.NArg() == 0 { + data, ioErr := ioutil.ReadAll(os.Stdin) + cb, err = string(data), ioErr + } else if flag.NArg() == 1 { + filePath := flag.Arg(0) + cb, err = readTextFile(filePath) + } else { + fatalf("provide file path or input on stdin") + } + if err != nil { + fatalf("%v", err) + } + + if *flagTruncNewLine && len(cb) > 0 && cb[len(cb)-1] == '\n' { + cb = cb[:len(cb)-1] + } + + err = clipboard.WriteAll(cb) + if err != nil { + fatalf("%v", err) + } +} + +func readTextFile(filePath string) (string, error) { + data, err := ioutil.ReadFile(filePath) + if err != nil { + return "", err + } + return string(data), err +} + +func fatalf(formatMessage string, args ...interface{}) { + fmt.Printf(formatMessage+"\n", args...) + os.Exit(1) +}