Skip to content

Commit

Permalink
save secret to clipboard if os is darwin (pbcopy)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Mar 25, 2023
1 parent 92da86c commit 8a1dc67
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 514 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ before:
- go mod download
builds:
- env:
- CGO_ENABLED=1
- CGO_ENABLED=0
ldflags:
- -s -w -X "main.Version={{.Version}}" -X "main.Build={{.ShortCommit}}"
- -a -extldflags "-static"
Expand Down
20 changes: 15 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io"
"runtime"
"strings"

"github.com/lucasepe/locker/cmd/flags"
Expand Down Expand Up @@ -109,14 +110,23 @@ func (c *cmdGet) extractOne(sto kv.Store, fs *flag.FlagSet) error {
return err
}

if c.output.Value == fmtTxt {
if ok := clipboard.Write([]byte(val)); ok {
fmt.Fprintf(fs.Output(), "the secret has been copied to the clipboard")
}
if c.output.Value != fmtTxt {
c.exportFuncMap[c.output.Value](fs.Output(), key, val)
return nil
}

c.exportFuncMap[c.output.Value](fs.Output(), key, val)
if runtime.GOOS != "darwin" {
fmt.Fprint(fs.Output(), val)
return nil
}

if err := clipboard.Write(val); err == nil {
fmt.Fprintf(fs.Output(),
"secret has been copied to the clipboard (namespace: %s, key: %s)\n",
c.namespace.String(), key)
} else {
fmt.Fprint(fs.Output(), val)
}

return nil
}
Expand Down
98 changes: 0 additions & 98 deletions internal/clipboard/clipboard.go

This file was deleted.

90 changes: 16 additions & 74 deletions internal/clipboard/clipboard_darwin.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,26 @@
//go:build darwin && !ios
// +build darwin,!ios

package clipboard

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework Cocoa
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
import "os/exec"

unsigned int clipboard_read_string(void **out);
unsigned int clipboard_read_image(void **out);
int clipboard_write_string(const void *bytes, NSInteger n);
int clipboard_write_image(const void *bytes, NSInteger n);
NSInteger clipboard_change_count();
*/
import "C"
import (
"context"
"time"
"unsafe"
const (
copyCmd = "pbcopy"
)

func read() (buf []byte, err error) {
var (
data unsafe.Pointer
n C.uint
)
n = C.clipboard_read_string(&data)
if data == nil {
return nil, errUnavailable
}
defer C.free(unsafe.Pointer(data))
if n == 0 {
return nil, nil
func Write(text string) error {
cmd := exec.Command(copyCmd)
in, err := cmd.StdinPipe()
if err != nil {
return err
}
return C.GoBytes(data, C.int(n)), nil
}

// write writes the given data to clipboard and
// returns true if success or false if failed.
func write(buf []byte) (bool, error) {
var ok C.int

if len(buf) == 0 {
ok = C.clipboard_write_string(unsafe.Pointer(nil), 0)
} else {
ok = C.clipboard_write_string(unsafe.Pointer(&buf[0]),
C.NSInteger(len(buf)))
if err := cmd.Start(); err != nil {
return err
}
if ok != 0 {
return false, errUnavailable
if _, err := in.Write([]byte(text)); err != nil {
return err
}

return true, nil
}

func watch(ctx context.Context) <-chan []byte {
recv := make(chan []byte, 1)
// not sure if we are too slow or the user too fast :)
ti := time.NewTicker(time.Second)
lastCount := C.long(C.clipboard_change_count())
go func() {
for {
select {
case <-ctx.Done():
close(recv)
return
case <-ti.C:
this := C.long(C.clipboard_change_count())
if lastCount != this {
b := Read()
if b == nil {
continue
}
recv <- b
lastCount = this
}
}
}
}()
return recv
if err := in.Close(); err != nil {
return err
}
return cmd.Wait()
}
42 changes: 0 additions & 42 deletions internal/clipboard/clipboard_darwin.m

This file was deleted.

18 changes: 0 additions & 18 deletions internal/clipboard/clipboard_linux.go

This file was deleted.

Loading

0 comments on commit 8a1dc67

Please sign in to comment.