capture error on defer, doc string, lint

This commit is contained in:
grumbulon 2023-02-04 20:46:28 -05:00 committed by Gitea
parent 92100e27a0
commit ae3e7a1ae6
2 changed files with 12 additions and 3 deletions

View file

@ -11,7 +11,7 @@ import (
var errEmptyFile = errors.New("will not save empty file to FS")
// makeLocal takes a type path and then saves a zone file to either tmp or a permanent location
// makeLocal takes a type path and then saves a zone file to either tmp or a permanent location.
func makeLocal(where path, zone *ZoneRequest) error {
var (
path string
@ -20,7 +20,7 @@ func makeLocal(where path, zone *ZoneRequest) error {
err error
)
if _, err := os.Stat(fmt.Sprintf(zone.FileName, zone.User)); !os.IsNotExist(err) {
if _, err = os.Stat(fmt.Sprintf(zone.FileName, zone.User)); !os.IsNotExist(err) {
return fmt.Errorf("file %s already exists: %w", zone.FileName, err)
}
@ -33,13 +33,17 @@ func makeLocal(where path, zone *ZoneRequest) error {
path = fmt.Sprintf("/tmp/tmpfile-%s-%s", zone.RawFileName, zone.User)
case Perm:
c, err = internal.ReadConfig()
if err != nil {
logHandler(genericResponseFields{"error": err.Error(), "message": "no config file defined"})
return fmt.Errorf("unable to parse directory: %w", err)
}
path = fmt.Sprintf("%s/%s/", c.ZoneDir, zone.RawFileName)
file = zone.RawFileName
if err = os.MkdirAll(path, 0o750); err != nil {
logHandler(genericResponseFields{"error": err.Error(), "message": "unable to make directory for zone files"})

View file

@ -138,7 +138,12 @@ func (zone *ZoneRequest) parse() error {
func (zone *ZoneRequest) save(path) error {
// clean up the tmp file
defer os.Remove(filepath.Clean("/tmp/" + zone.FileName))
defer func() (err error) {
if err = os.Remove(filepath.Clean("/tmp/" + zone.FileName)); err != nil {
return
}
return
}()
return makeLocal(Perm, zone)
}