Add helper functions for writing files
This commit is contained in:
parent
ba3af60858
commit
ee8ae0c1f3
|
@ -12,7 +12,6 @@ package monitor
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
@ -49,7 +48,7 @@ func (cert *discoveredCert) pemChain() []byte {
|
|||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
func (cert *discoveredCert) json() []byte {
|
||||
func (cert *discoveredCert) json() any {
|
||||
object := map[string]any{
|
||||
"tbs_sha256": hex.EncodeToString(cert.TBSSHA256[:]),
|
||||
"pubkey_sha256": hex.EncodeToString(cert.PubkeySHA256[:]),
|
||||
|
@ -65,21 +64,17 @@ func (cert *discoveredCert) json() []byte {
|
|||
object["not_after"] = nil
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("encoding certificate as JSON failed unexpectedly: %w", err))
|
||||
}
|
||||
return jsonBytes
|
||||
return object
|
||||
}
|
||||
|
||||
func (cert *discoveredCert) save() error {
|
||||
if err := writeFile(cert.CertPath, cert.pemChain(), 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeFile(cert.JSONPath, cert.json(), 0666); err != nil {
|
||||
if err := writeJSONFile(cert.JSONPath, cert.json(), 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeFile(cert.TextPath, []byte(cert.Text()), 0666); err != nil {
|
||||
if err := writeTextFile(cert.TextPath, cert.Text(), 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -12,6 +12,7 @@ package monitor
|
|||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
@ -36,6 +37,19 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func writeTextFile(filename string, fileText string, perm os.FileMode) error {
|
||||
return writeFile(filename, []byte(fileText), perm)
|
||||
}
|
||||
|
||||
func writeJSONFile(filename string, data any, perm os.FileMode) error {
|
||||
fileBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileBytes = append(fileBytes, '\n')
|
||||
return writeFile(filename, fileBytes, perm)
|
||||
}
|
||||
|
||||
func fileExists(filename string) bool {
|
||||
_, err := os.Lstat(filename)
|
||||
return err == nil
|
||||
|
|
|
@ -38,10 +38,5 @@ func loadStateFile(filePath string) (*stateFile, error) {
|
|||
}
|
||||
|
||||
func (file *stateFile) store(filePath string) error {
|
||||
fileBytes, err := json.Marshal(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileBytes = append(fileBytes, '\n')
|
||||
return writeFile(filePath, fileBytes, 0666)
|
||||
return writeJSONFile(filePath, file, 0666)
|
||||
}
|
||||
|
|
|
@ -65,11 +65,7 @@ func storeSTHInDir(dirPath string, sth *ct.SignedTreeHead) error {
|
|||
if fileExists(filePath) {
|
||||
return nil
|
||||
}
|
||||
fileBytes, err := json.Marshal(sth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeFile(filePath, fileBytes, 0666)
|
||||
return writeJSONFile(filePath, sth, 0666)
|
||||
}
|
||||
|
||||
func removeSTHFromDir(dirPath string, sth *ct.SignedTreeHead) error {
|
||||
|
|
Loading…
Reference in New Issue