Return exit code from cmd.Main instead of exiting directly

This allows the calling code to do custom cleanup.
This commit is contained in:
Andrew Ayer 2016-06-03 07:21:08 -07:00
parent 6db3f7564c
commit 2c8cb1f402
3 changed files with 8 additions and 8 deletions

View File

@ -190,5 +190,5 @@ func main() {
} }
} }
cmd.Main(*stateDir, processEntry) os.Exit(cmd.Main(*stateDir, processEntry))
} }

View File

@ -122,7 +122,7 @@ func saveEvidence (logUri string, firstSTH *ct.SignedTreeHead, secondSTH *ct.Sig
return firstFilename, secondFilename, proofFilename, nil return firstFilename, secondFilename, proofFilename, nil
} }
func Main (argStateDir string, processCallback certspotter.ProcessCallback) { func Main (argStateDir string, processCallback certspotter.ProcessCallback) int {
stateDir = argStateDir stateDir = argStateDir
var logs []certspotter.LogInfo var logs []certspotter.LogInfo
@ -130,13 +130,13 @@ func Main (argStateDir string, processCallback certspotter.ProcessCallback) {
logFile, err := os.Open(*logsFilename) logFile, err := os.Open(*logsFilename)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: Error opening logs file for reading: %s: %s\n", os.Args[0], *logsFilename, err) fmt.Fprintf(os.Stderr, "%s: Error opening logs file for reading: %s: %s\n", os.Args[0], *logsFilename, err)
os.Exit(1) return 1
} }
defer logFile.Close() defer logFile.Close()
var logFileObj certspotter.LogInfoFile var logFileObj certspotter.LogInfoFile
if err := json.NewDecoder(logFile).Decode(&logFileObj); err != nil { if err := json.NewDecoder(logFile).Decode(&logFileObj); err != nil {
fmt.Fprintf(os.Stderr, "%s: Error decoding logs file: %s: %s\n", os.Args[0], *logsFilename, err) fmt.Fprintf(os.Stderr, "%s: Error decoding logs file: %s: %s\n", os.Args[0], *logsFilename, err)
os.Exit(1) return 1
} }
logs = logFileObj.Logs logs = logFileObj.Logs
} else if *underwater { } else if *underwater {
@ -147,13 +147,13 @@ func Main (argStateDir string, processCallback certspotter.ProcessCallback) {
if err := os.Mkdir(stateDir, 0777); err != nil && !os.IsExist(err) { if err := os.Mkdir(stateDir, 0777); err != nil && !os.IsExist(err) {
fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], stateDir, err) fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], stateDir, err)
os.Exit(1) return 1
} }
for _, subdir := range []string{"certs", "sths", "evidence"} { for _, subdir := range []string{"certs", "sths", "evidence"} {
path := filepath.Join(stateDir, subdir) path := filepath.Join(stateDir, subdir)
if err := os.Mkdir(path, 0777); err != nil && !os.IsExist(err) { if err := os.Mkdir(path, 0777); err != nil && !os.IsExist(err) {
fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], path, err) fmt.Fprintf(os.Stderr, "%s: Error creating state directory: %s: %s\n", os.Args[0], path, err)
os.Exit(1) return 1
} }
} }
@ -268,5 +268,5 @@ func Main (argStateDir string, processCallback certspotter.ProcessCallback) {
} }
} }
os.Exit(exitCode) return exitCode
} }

View File

@ -48,5 +48,5 @@ func processEntry (scanner *certspotter.Scanner, entry *ct.LogEntry) {
func main() { func main() {
flag.Parse() flag.Parse()
cmd.Main(*stateDir, processEntry) os.Exit(cmd.Main(*stateDir, processEntry))
} }