Added errors to WriteFileDatabase

Signed-off-by: Morten Linderud <morten@linderud.pw>
This commit is contained in:
Morten Linderud 2021-05-20 00:26:59 +02:00
parent 0d121672ca
commit fe514e1af7
No known key found for this signature in database
GPG Key ID: E742683BA08CB2FF
5 changed files with 17 additions and 10 deletions

View File

@ -25,7 +25,9 @@ var removeFileCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
delete(files, args[0]) delete(files, args[0])
sbctl.WriteFileDatabase(sbctl.DBPath, files) if err := sbctl.WriteFileDatabase(sbctl.DBPath, files); err != nil {
return err
}
return nil return nil
}, },
} }

View File

@ -42,7 +42,9 @@ var signAllCmd = &cobra.Command{
checksum := sbctl.ChecksumFile(entry.File) checksum := sbctl.ChecksumFile(entry.File)
entry.Checksum = checksum entry.Checksum = checksum
files[entry.File] = entry files[entry.File] = entry
sbctl.WriteFileDatabase(sbctl.DBPath, files) if err := sbctl.WriteFileDatabase(sbctl.DBPath, files); err != nil {
return err
}
} }
return nil return nil

View File

@ -2,7 +2,6 @@ package main
import ( import (
"errors" "errors"
"log"
"os" "os"
"path/filepath" "path/filepath"
@ -75,7 +74,7 @@ var verifyCmd = &cobra.Command{
} }
return nil return nil
}); err != nil { }); err != nil {
log.Println(err) return err
} }
return nil return nil
}, },

View File

@ -3,7 +3,6 @@ package sbctl
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
) )
@ -27,15 +26,16 @@ func ReadFileDatabase(dbpath string) (SigningEntries, error) {
return files, nil return files, nil
} }
func WriteFileDatabase(dbpath string, files SigningEntries) { func WriteFileDatabase(dbpath string, files SigningEntries) error {
data, err := json.MarshalIndent(files, "", " ") data, err := json.MarshalIndent(files, "", " ")
if err != nil { if err != nil {
log.Fatal(err) return err
} }
err = os.WriteFile(dbpath, data, 0644) err = os.WriteFile(dbpath, data, 0644)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
return nil
} }
func SigningEntryIter(fn func(s *SigningEntry) error) error { func SigningEntryIter(fn func(s *SigningEntry) error) error {

View File

@ -115,7 +115,9 @@ func Sign(file, output string, enroll bool) error {
checksum := ChecksumFile(file) checksum := ChecksumFile(file)
entry.Checksum = checksum entry.Checksum = checksum
files[file] = entry files[file] = entry
WriteFileDatabase(DBPath, files) if err := WriteFileDatabase(DBPath, files); err != nil {
return err
}
} else { } else {
err = SignFile(DBKey, DBCert, file, output, "") err = SignFile(DBKey, DBCert, file, output, "")
// return early if signing fails // return early if signing fails
@ -127,7 +129,9 @@ func Sign(file, output string, enroll bool) error {
if enroll { if enroll {
checksum := ChecksumFile(file) checksum := ChecksumFile(file)
files[file] = &SigningEntry{File: file, OutputFile: output, Checksum: checksum} files[file] = &SigningEntry{File: file, OutputFile: output, Checksum: checksum}
WriteFileDatabase(DBPath, files) if err := WriteFileDatabase(DBPath, files); err != nil {
return err
}
} }
return err return err