Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 5374c95

Browse files
authored
Add delay parameter to modify the default pause before printing the output (#22)
1 parent 828849b commit 5374c95

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

cli/cli2cloud/cli2cloud.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
"google.golang.org/grpc"
1111
"google.golang.org/grpc/credentials/insecure"
1212
"log"
13+
"strconv"
1314
"time"
1415
)
1516

1617
const (
1718
//serverIP = "localhost:50051" // local dev
1819
serverIP = "cli2cloud.com:50051" // production
1920
randomPasswordLength = 16
21+
defaultDelayLength = 3
2022
)
2123

2224
type stringFlag struct {
@@ -34,7 +36,7 @@ func (sf *stringFlag) String() string {
3436
return sf.value
3537
}
3638

37-
func sendPipedMessages(c proto.Cli2CloudClient, ctx context.Context, password *string) error {
39+
func sendPipedMessages(c proto.Cli2CloudClient, ctx context.Context, password *string, delay time.Duration) error {
3840
stream, err := c.Publish(ctx)
3941
if err != nil {
4042
return err
@@ -63,8 +65,8 @@ func sendPipedMessages(c proto.Cli2CloudClient, ctx context.Context, password *s
6365
}
6466

6567
fmt.Printf("Share and monitor it live from https://cli2cloud.com/%s%s\n\n", clientId.Id, keyURLSuffix)
66-
// Wait 2 seconds for user to copy the client ID
67-
time.Sleep(2 * time.Second)
68+
// Wait delay seconds for user to copy the client ID
69+
time.Sleep(delay * time.Second)
6870

6971
// Create a messages stream which is reading from both Stdout and Stdin
7072
streamMessages := make(chan string)
@@ -97,10 +99,14 @@ func sendPipedMessages(c proto.Cli2CloudClient, ctx context.Context, password *s
9799
return err
98100
}
99101

100-
func parseFlags() *string {
102+
func parseFlags() (*string, time.Duration) {
101103
var passwordFlag stringFlag
102104
flag.Var(&passwordFlag, "encrypt", "Password to encrypt your data with.")
103105
generatePassword := flag.Bool("encrypt-random", false, "Generate a random password to encrypt your data.")
106+
107+
var delayFlag stringFlag
108+
flag.Var(&delayFlag, "delay", "Delay before printing the command output to copy the client ID.")
109+
104110
flag.Parse()
105111

106112
if passwordFlag.set && passwordFlag.value == "" {
@@ -124,11 +130,21 @@ func parseFlags() *string {
124130
fmt.Printf("Your password: %s\n", *password)
125131
}
126132

127-
return password
133+
var delay int
134+
if delayFlag.set {
135+
delay, err = strconv.Atoi(delayFlag.value)
136+
if err != nil || delay < 0 {
137+
log.Fatal("Delay parameter argument is non parseable ", err)
138+
}
139+
} else {
140+
delay = defaultDelayLength
141+
}
142+
143+
return password, time.Duration(delay)
128144
}
129145

130146
func main() {
131-
password := parseFlags()
147+
password, delay := parseFlags()
132148

133149
conn, err := grpc.Dial(serverIP, grpc.WithTransportCredentials(insecure.NewCredentials()))
134150
if err != nil {
@@ -139,7 +155,7 @@ func main() {
139155
ctx, cancel := context.WithCancel(context.Background())
140156
defer cancel()
141157

142-
if err := sendPipedMessages(client, ctx, password); err != nil {
158+
if err := sendPipedMessages(client, ctx, password, delay); err != nil {
143159
log.Fatal("Error while sending to server.", err)
144160
}
145161

0 commit comments

Comments
 (0)