Skip to content

Commit 992cb1b

Browse files
committed
Initial commit
0 parents  commit 992cb1b

6 files changed

Lines changed: 103 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 0.1.0 (2019-10-09)
2+
- Initial release.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [year] [fullname]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# cb
2+
## Usage
3+
Copy file contents to clipboard:
4+
```sh
5+
cb file.txt
6+
```
7+
8+
Copy stdin contents to clipboard:
9+
```sh
10+
echo foo | cb
11+
```
12+
13+
Copy stdin contents to clipboard, truncating 1 trailing line break if it exists:
14+
```sh
15+
echo foo | cb -n
16+
```
17+
18+
## Installation
19+
```sh
20+
go get -u github.com/setlog./cb
21+
```

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/setlog/cb
2+
3+
go 1.13
4+
5+
require github.com/atotto/clipboard v0.1.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
2+
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=

main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
"github.com/atotto/clipboard"
10+
)
11+
12+
func main() {
13+
flagTruncNewLine := flag.Bool("n", false, "Set to truncate one trailing line break if it exists.")
14+
flag.Parse()
15+
16+
var cb string
17+
var err error
18+
if flag.NArg() == 0 {
19+
data, ioErr := ioutil.ReadAll(os.Stdin)
20+
cb, err = string(data), ioErr
21+
} else if flag.NArg() == 1 {
22+
filePath := flag.Arg(0)
23+
cb, err = readTextFile(filePath)
24+
} else {
25+
fatalf("provide file path or input on stdin")
26+
}
27+
if err != nil {
28+
fatalf("%v", err)
29+
}
30+
31+
if *flagTruncNewLine && len(cb) > 0 && cb[len(cb)-1] == '\n' {
32+
cb = cb[:len(cb)-1]
33+
}
34+
35+
err = clipboard.WriteAll(cb)
36+
if err != nil {
37+
fatalf("%v", err)
38+
}
39+
}
40+
41+
func readTextFile(filePath string) (string, error) {
42+
data, err := ioutil.ReadFile(filePath)
43+
if err != nil {
44+
return "", err
45+
}
46+
return string(data), err
47+
}
48+
49+
func fatalf(formatMessage string, args ...interface{}) {
50+
fmt.Printf(formatMessage+"\n", args...)
51+
os.Exit(1)
52+
}

0 commit comments

Comments
 (0)