Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MMulthaupt committed Oct 9, 2019
0 parents commit 992cb1b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 0.1.0 (2019-10-09)
- Initial release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/setlog/cb

go 1.13

require github.com/atotto/clipboard v0.1.2
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
52 changes: 52 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 992cb1b

Please sign in to comment.