refactor: Prevent using panic where error return value can be used

This commit is contained in:
Felicitas Pojtinger
2021-12-05 19:21:07 +01:00
parent 197e2f50de
commit bc0a267bb7
4 changed files with 24 additions and 14 deletions

View File

@@ -327,7 +327,12 @@ func archive(
hdr.PAXRecords = map[string]string{}
}
hdr.PAXRecords[pax.STFSRecordUncompressedSize] = strconv.Itoa(int(hdr.Size))
if signature := sign(); signature != "" {
signature, err := sign()
if err != nil {
return err
}
if signature != "" {
hdr.PAXRecords[pax.STFSRecordSignature] = signature
}
hdr.Size = int64(fileSizeCounter.BytesRead)
@@ -619,7 +624,7 @@ func sign(
src io.Reader,
signatureFormat string,
identity interface{},
) (io.ReadCloser, func() string, error) {
) (io.Reader, func() (string, error), error) {
switch signatureFormat {
case signatureFormatMinisignKey:
identity, ok := identity.(minisign.PrivateKey)
@@ -629,8 +634,8 @@ func sign(
signer := minisign.NewReader(src)
return io.NopCloser(signer), func() string {
return base64.StdEncoding.EncodeToString(signer.Sign(identity))
return signer, func() (string, error) {
return base64.StdEncoding.EncodeToString(signer.Sign(identity)), nil
}, nil
case signatureFormatPGPKey:
identities, ok := identity.(openpgp.EntityList)
@@ -660,21 +665,21 @@ func sign(
hash := sig.Hash.New()
return io.NopCloser(io.TeeReader(src, hash)), func() string {
return io.TeeReader(src, hash), func() (string, error) {
if err := sig.Sign(hash, signingKey.PrivateKey, config); err != nil {
panic(err)
return "", err
}
out := &bytes.Buffer{}
if err := sig.Serialize(out); err != nil {
panic(err)
return "", err
}
return base64.StdEncoding.EncodeToString(out.Bytes())
return base64.StdEncoding.EncodeToString(out.Bytes()), nil
}, nil
case noneKey:
return io.NopCloser(src), func() string {
return ""
return src, func() (string, error) {
return "", nil
}, nil
default:
return nil, nil, errUnsupportedSignatureFormat

View File

@@ -23,7 +23,7 @@ var driveEjectCmd = &cobra.Command{
f, err := os.OpenFile(viper.GetString(driveFlag), os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
return err
}
defer f.Close()

View File

@@ -24,13 +24,13 @@ var driveTellCmd = &cobra.Command{
f, err := os.OpenFile(viper.GetString(driveFlag), os.O_RDONLY, os.ModeCharDevice)
if err != nil {
panic(err)
return err
}
defer f.Close()
currentRecord, err := controllers.GetCurrentRecordFromTape(f)
if err != nil {
panic(err)
return err
}
fmt.Println(currentRecord)

View File

@@ -234,7 +234,12 @@ func update(
hdr.PAXRecords = map[string]string{}
}
hdr.PAXRecords[pax.STFSRecordUncompressedSize] = strconv.Itoa(int(hdr.Size))
if signature := sign(); signature != "" {
signature, err := sign()
if err != nil {
return err
}
if signature != "" {
hdr.PAXRecords[pax.STFSRecordSignature] = signature
}
hdr.Size = int64(fileSizeCounter.BytesRead)