Add helper functions for writing files

This commit is contained in:
Andrew Ayer 2023-02-18 21:02:05 -05:00
parent ba3af60858
commit ee8ae0c1f3
4 changed files with 20 additions and 20 deletions

View File

@ -12,7 +12,6 @@ package monitor
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"strings" "strings"
@ -49,7 +48,7 @@ func (cert *discoveredCert) pemChain() []byte {
return buffer.Bytes() return buffer.Bytes()
} }
func (cert *discoveredCert) json() []byte { func (cert *discoveredCert) json() any {
object := map[string]any{ object := map[string]any{
"tbs_sha256": hex.EncodeToString(cert.TBSSHA256[:]), "tbs_sha256": hex.EncodeToString(cert.TBSSHA256[:]),
"pubkey_sha256": hex.EncodeToString(cert.PubkeySHA256[:]), "pubkey_sha256": hex.EncodeToString(cert.PubkeySHA256[:]),
@ -65,21 +64,17 @@ func (cert *discoveredCert) json() []byte {
object["not_after"] = nil object["not_after"] = nil
} }
jsonBytes, err := json.Marshal(object) return object
if err != nil {
panic(fmt.Errorf("encoding certificate as JSON failed unexpectedly: %w", err))
}
return jsonBytes
} }
func (cert *discoveredCert) save() error { func (cert *discoveredCert) save() error {
if err := writeFile(cert.CertPath, cert.pemChain(), 0666); err != nil { if err := writeFile(cert.CertPath, cert.pemChain(), 0666); err != nil {
return err return err
} }
if err := writeFile(cert.JSONPath, cert.json(), 0666); err != nil { if err := writeJSONFile(cert.JSONPath, cert.json(), 0666); err != nil {
return err 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 err
} }
return nil return nil

View File

@ -12,6 +12,7 @@ package monitor
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"os" "os"
) )
@ -36,6 +37,19 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
return nil 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 { func fileExists(filename string) bool {
_, err := os.Lstat(filename) _, err := os.Lstat(filename)
return err == nil return err == nil

View File

@ -38,10 +38,5 @@ func loadStateFile(filePath string) (*stateFile, error) {
} }
func (file *stateFile) store(filePath string) error { func (file *stateFile) store(filePath string) error {
fileBytes, err := json.Marshal(file) return writeJSONFile(filePath, file, 0666)
if err != nil {
return err
}
fileBytes = append(fileBytes, '\n')
return writeFile(filePath, fileBytes, 0666)
} }

View File

@ -65,11 +65,7 @@ func storeSTHInDir(dirPath string, sth *ct.SignedTreeHead) error {
if fileExists(filePath) { if fileExists(filePath) {
return nil return nil
} }
fileBytes, err := json.Marshal(sth) return writeJSONFile(filePath, sth, 0666)
if err != nil {
return err
}
return writeFile(filePath, fileBytes, 0666)
} }
func removeSTHFromDir(dirPath string, sth *ct.SignedTreeHead) error { func removeSTHFromDir(dirPath string, sth *ct.SignedTreeHead) error {