Skip to content

Commit

Permalink
Merge pull request #3 from madflojo/tempfiles
Browse files Browse the repository at this point in the history
Fixing code examples
  • Loading branch information
madflojo authored Jan 29, 2023
2 parents f53d6d8 + 7a2bd7a commit ad7679b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
45 changes: 16 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ Overall, testcerts simplifies the process of generating and managing test certif
The `GenerateCertsToFile` function generates an X.509 certificate and key and writes them to the file paths provided.

```go
package main

import (
"github.com/madflojo/testcerts"
)

func main() {
err := testcerts.GenerateCertsToFile("/tmp/cert.pem", "/tmp/key.pem")
func TestSomething(t *testing.T) {
err := testcerts.GenerateCertsToFile("/tmp/cert", "/tmp/key")
if err != nil {
// handle error
// do stuff
}

_ = something.Run("/tmp/cert", "/tmp/key")
// do more testing
}
```

Expand All @@ -36,17 +33,14 @@ func main() {
The `GenerateCertsToTempFile` function generates an X.509 certificate and key and writes them to randomly generated files in the directory provided or the system's temporary directory if none is provided. The function returns the file paths of the generated files.

```go
package main

import (
"github.com/madflojo/testcerts"
)

func main() {
func TestSomething(t *testing.T) {
certFile, keyFile, err := testcerts.GenerateCertsToTempFile("/tmp/")
if err != nil {
// handle error
// do stuff
}

_ = something.Run(certFile, keyFile)
// do more testing
}
```

Expand All @@ -55,21 +49,14 @@ func main() {
The `GenerateCerts` function generates an X.509 certificate and key and returns them as byte slices.

```go
package main

import (
"fmt"

"github.com/madflojo/testcerts"
)

func main() {
func TestSomething(t *testing.T) {
cert, key, err := testcerts.GenerateCerts()
if err != nil {
// handle error
// do stuff
}
fmt.Printf("Certificate: %s\n", cert)
fmt.Printf("Key: %s\n", key)

_ = something.Run(cert, key)
// do more testing
}
```

Expand Down
20 changes: 6 additions & 14 deletions testcerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@ named file in a specified or temporary directory.
For example, to generate and save a certificate and key to a temporary directory:
package main
import (
"fmt"
"log"
"testcerts"
)
func main() {
certPath, keyPath, err := testcerts.GenerateCertsToTempFile("")
func TestSomething(t *testing.T) {
certFile, keyFile, err := testcerts.GenerateCertsToTempFile("/tmp/")
if err != nil {
log.Fatal(err)
// do stuff
}
fmt.Println("Certificate written to:", certPath)
fmt.Println("Key written to:", keyPath)
_ = something.Run(certFile, keyFile)
// do more testing
}
This will create a temporary certificate and key and print the paths to where the files were written.
Expand Down

0 comments on commit ad7679b

Please sign in to comment.