Skip to content

Commit

Permalink
refactor: use io.Pipe instead of an array of bytes for posting JSON.
Browse files Browse the repository at this point in the history
Not sure which is more idiomatic go but I liked that it was a bit more
clear as to what was happening.
  • Loading branch information
Adam Simpson committed Jun 11, 2021
1 parent e79b3ca commit cdda7a3
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cmd

import (
"bytes"
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -105,7 +105,7 @@ func addToAgent(cert *ssh.Certificate, key *ecdsa.PrivateKey) {
}

func getCert(url string) (cert Cert, error error) {
x := new(bytes.Buffer)
readData, writeData := io.Pipe()
tokenString, err := util.ReturnToken()

if err != nil {
Expand All @@ -116,9 +116,12 @@ func getCert(url string) (cert Cert, error error) {
tokenString,
}

json.NewEncoder(x).Encode(token)
go func() {
json.NewEncoder(writeData).Encode(token)
writeData.Close()
}()

resp, err := http.Post(url+"/ssh", "application/json", x)
resp, err := http.Post(url+"/ssh", "application/json", readData)

if err != nil {
return cert, err
Expand Down

0 comments on commit cdda7a3

Please sign in to comment.