Skip to content

Commit 5cc9f8a

Browse files
author
Philipp Heckel
committed
Fix 'pcopy join' error, add test for it
1 parent 1acf2ec commit 5cc9f8a

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

cmd/pcopy/join.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func execJoin(c *cli.Context) error {
8888
return err
8989
}
9090
} else {
91-
password, err := readPassword()
91+
password, err := readPassword(c)
9292
if err != nil {
9393
return err
9494
}
@@ -122,44 +122,44 @@ func execJoin(c *cli.Context) error {
122122
}
123123

124124
if !quiet {
125-
printInstructions(configFile, clipboard, info)
125+
printInstructions(c, configFile, clipboard, info)
126126
}
127127

128128
return nil
129129
}
130130

131-
func readPassword() ([]byte, error) {
132-
fmt.Print("Enter password to join clipboard: ")
131+
func readPassword(c *cli.Context) ([]byte, error) {
132+
fmt.Fprint(c.App.ErrWriter, "Enter password to join clipboard: ")
133133
password, err := term.ReadPassword(syscall.Stdin)
134134
if err != nil {
135135
return nil, err
136136
}
137-
fmt.Print("\r")
137+
fmt.Fprint(c.App.ErrWriter, "\r")
138138
return password, nil
139139
}
140140

141-
func printInstructions(configFile string, clipboard string, info *pcopy.ServerInfo) {
141+
func printInstructions(c *cli.Context, configFile string, clipboard string, info *pcopy.ServerInfo) {
142142
clipboardPrefix := ""
143143
if clipboard != pcopy.DefaultClipboard {
144144
clipboardPrefix = fmt.Sprintf(" %s:", clipboard)
145145
}
146146

147147
if clipboard == pcopy.DefaultClipboard {
148-
fmt.Printf("Successfully joined clipboard, config written to %s\n", pcopy.CollapseHome(configFile))
148+
fmt.Fprintf(c.App.ErrWriter, "Successfully joined clipboard, config written to %s\n", pcopy.CollapseHome(configFile))
149149
} else {
150-
fmt.Printf("Successfully joined clipboard as alias '%s', config written to %s\n", clipboard, pcopy.CollapseHome(configFile))
150+
fmt.Fprintf(c.App.ErrWriter, "Successfully joined clipboard as alias '%s', config written to %s\n", clipboard, pcopy.CollapseHome(configFile))
151151
}
152152

153153
if info.Cert != nil {
154-
fmt.Println()
155-
fmt.Println("Warning: The TLS certificate was self-signed and has been pinned.")
156-
fmt.Println("Future communication will be secure, but joining could have been intercepted.")
154+
fmt.Fprintln(c.App.ErrWriter)
155+
fmt.Fprintln(c.App.ErrWriter, "Warning: The TLS certificate was self-signed and has been pinned.")
156+
fmt.Fprintln(c.App.ErrWriter, "Future communication will be secure, but joining could have been intercepted.")
157157
}
158158

159-
fmt.Println()
159+
fmt.Fprintln(c.App.ErrWriter)
160160
if _, err := os.Stat("/usr/bin/pcp"); err == nil {
161-
fmt.Printf("You may now use 'pcp%s' and 'ppaste%s'. See 'pcopy -h' for usage details.\n", clipboardPrefix, clipboardPrefix)
161+
fmt.Fprintf(c.App.ErrWriter, "You may now use 'pcp%s' and 'ppaste%s'. See 'pcopy -h' for usage details.\n", clipboardPrefix, clipboardPrefix)
162162
} else {
163-
fmt.Printf("You may now use 'pcopy copy%s' and 'pcopy paste%s'. See 'pcopy -h' for usage details.\n", clipboardPrefix, clipboardPrefix)
163+
fmt.Fprintf(c.App.ErrWriter, "You may now use 'pcopy copy%s' and 'pcopy paste%s'. See 'pcopy -h' for usage details.\n", clipboardPrefix, clipboardPrefix)
164164
}
165165
}

cmd/pcopy/join_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"heckel.io/pcopy"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
func TestCLI_JoinAndList(t *testing.T) {
11+
_, config := newTestConfig(t)
12+
serverRouter := startTestServerRouter(t, config)
13+
defer serverRouter.Stop()
14+
15+
waitForPortUp(t, "12345")
16+
17+
configDir := t.TempDir()
18+
os.Setenv(pcopy.EnvConfigDir, configDir)
19+
20+
app, _, _, stderr := newTestApp()
21+
if err := runApp(app, "pcopy", "join", "localhost:12345"); err != nil {
22+
t.Fatal(err)
23+
}
24+
25+
assertStrContains(t, stderr.String(), "Successfully joined clipboard, config written to")
26+
assertFileExist(t, filepath.Join(configDir, "default.conf"))
27+
28+
stderr.Reset()
29+
if err := runApp(app, "pcopy", "list"); err != nil {
30+
t.Fatal(err)
31+
}
32+
assertStrContains(t, stderr.String(), "default")
33+
assertStrContains(t, stderr.String(), "localhost:12345")
34+
}

cmd/pcopy/list.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ func execList(c *cli.Context) error {
3737
}
3838

3939
lineFmt := fmt.Sprintf("%%-%ds %%-%ds %%s\n", clipboardMaxLen, serverAddrMaxLen)
40-
fmt.Printf(lineFmt, clipboardHeader, serverAddrHeader, "Config file")
41-
fmt.Printf(lineFmt, strings.Repeat("-", clipboardMaxLen), strings.Repeat("-", serverAddrMaxLen), strings.Repeat("-", configFileMaxLen))
40+
fmt.Fprintf(c.App.ErrWriter, lineFmt, clipboardHeader, serverAddrHeader, "Config file")
41+
fmt.Fprintf(c.App.ErrWriter, lineFmt, strings.Repeat("-", clipboardMaxLen), strings.Repeat("-", serverAddrMaxLen), strings.Repeat("-", configFileMaxLen))
4242
for filename, config := range configs {
4343
clipboard := pcopy.ExtractClipboard(filename)
4444
shortName := pcopy.CollapseHome(filename)
4545
serverAddr := pcopy.CollapseServerAddr(config.ServerAddr)
46-
fmt.Printf(lineFmt, clipboard, serverAddr, shortName)
46+
fmt.Fprintf(c.App.ErrWriter, lineFmt, clipboard, serverAddr, shortName)
4747
}
4848
} else {
49-
fmt.Println("No clipboards found. You can use 'pcopy join' to connect to existing clipboards.")
49+
fmt.Fprintln(c.App.ErrWriter, "No clipboards found. You can use 'pcopy join' to connect to existing clipboards.")
5050
}
5151
return nil
5252
}

cmd/pcopy/main_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ func assertFileNotExist(t *testing.T, filename string) {
143143
}
144144
}
145145

146+
func assertFileExist(t *testing.T, filename string) {
147+
if stat, _ := os.Stat(filename); stat == nil {
148+
t.Fatalf("expected file %s to exist, but it does not", filename)
149+
}
150+
}
151+
146152
func waitForPortUp(t *testing.T, port string) {
147153
success := false
148154
for i := 0; i < 100; i++ {

configs/pcopy.conf.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
# Default: 7d
116116
#
117117
{{$fileExpireAfterStr := durationToHuman .FileExpireAfter -}}
118-
{{if eq "7d" $fileExpireAfterStr}}# FileExpireAfter 7d{{else}}FileExpireAfter {{$fileExpireAfterStr}}{{end}}
118+
{{if or (eq "7d" $fileExpireAfterStr) (not .FileExpireAfter)}}# FileExpireAfter 7d{{else}}FileExpireAfter {{$fileExpireAfterStr}}{{end}}
119119

120120
# Modes that are allowed to be set by the client for uploaded files, read-write ("rw") and read-only ("ro).
121121
# If both modes are set, the client can chose. If no mode is set by the client, the first mode is used as

0 commit comments

Comments
 (0)