Skip to content

Commit ca98f73

Browse files
committed
refactor: extract method to create CT index file
1 parent 51a8096 commit ca98f73

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

internal/certificatetransparency/logmetrics.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,11 @@ func (m *LogMetrics) LoadCTIndex(ctIndexFilePath string) {
161161
if readErr != nil {
162162
// Create the file if it doesn't exist
163163
if os.IsNotExist(readErr) {
164-
log.Printf("Specified CT index file does not exist: '%s'\n", ctIndexFilePath)
165-
log.Println("Creating CT index file now!")
166-
file, createErr := os.Create(ctIndexFilePath)
167-
if createErr != nil {
164+
err := createCTIndexFile(ctIndexFilePath, m)
165+
if err != nil {
168166
log.Printf("Error creating CT index file: '%s'\n", ctIndexFilePath)
169-
log.Panicln(createErr)
167+
log.Panicln(err)
170168
}
171-
172-
var marshalErr error
173-
bytes, marshalErr = json.Marshal(m.index)
174-
if marshalErr != nil {
175-
return
176-
}
177-
_, writeErr := file.Write(bytes)
178-
if writeErr != nil {
179-
log.Printf("Error writing to CT index file: '%s'\n", ctIndexFilePath)
180-
log.Panicln(writeErr)
181-
}
182-
file.Close()
183169
} else {
184170
// If the file exists but we can't read it, log the error and panic
185171
log.Panicln(readErr)
@@ -192,7 +178,31 @@ func (m *LogMetrics) LoadCTIndex(ctIndexFilePath string) {
192178
log.Panicln(jerr)
193179
}
194180

195-
log.Println("Sucessfuly loaded saved CT indexes")
181+
log.Println("Successfully loaded saved CT indexes")
182+
}
183+
184+
func createCTIndexFile(ctIndexFilePath string, m *LogMetrics) error {
185+
log.Printf("Specified CT index file does not exist: '%s'\n", ctIndexFilePath)
186+
log.Println("Creating CT index file now!")
187+
188+
file, createErr := os.Create(ctIndexFilePath)
189+
if createErr != nil {
190+
log.Printf("Error creating CT index file: '%s'\n", ctIndexFilePath)
191+
log.Panicln(createErr)
192+
}
193+
194+
bytes, marshalErr := json.Marshal(m.index)
195+
if marshalErr != nil {
196+
return marshalErr
197+
}
198+
_, writeErr := file.Write(bytes)
199+
if writeErr != nil {
200+
log.Printf("Error writing to CT index file: '%s'\n", ctIndexFilePath)
201+
log.Panicln(writeErr)
202+
}
203+
file.Close()
204+
205+
return nil
196206
}
197207

198208
// SaveCertIndexesAtInterval saves the index of CTLogs at given intervals.

0 commit comments

Comments
 (0)