Return errors when dumping
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kolaente 2020-06-20 11:37:51 +02:00
parent fba333866d
commit c12bac0c96
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 14 additions and 10 deletions

View File

@ -17,6 +17,7 @@
package cmd package cmd
import ( import (
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/dump" "code.vikunja.io/api/pkg/modules/dump"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"time" "time"
@ -34,6 +35,8 @@ var dumpCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
filename := "vikunja-dump_" + time.Now().Format("2006-01-02_15-03-05") + ".zip" filename := "vikunja-dump_" + time.Now().Format("2006-01-02_15-03-05") + ".zip"
dump.Dump(filename) if err := dump.Dump(filename); err != nil {
log.Critical(err.Error())
}
}, },
} }

View File

@ -22,6 +22,7 @@ import (
"code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/version" "code.vikunja.io/api/pkg/version"
"fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
"io" "io"
"os" "os"
@ -33,10 +34,10 @@ import (
const compressionUsed = zip.Deflate const compressionUsed = zip.Deflate
// Dump creates a zip file with all vikunja files at filename // Dump creates a zip file with all vikunja files at filename
func Dump(filename string) { func Dump(filename string) error {
dumpFile, err := os.Create(filename) dumpFile, err := os.Create(filename)
if err != nil { if err != nil {
log.Criticalf("Error opening dump file: %s", err) return fmt.Errorf("error opening dump file: %s", err)
} }
defer dumpFile.Close() defer dumpFile.Close()
@ -47,7 +48,7 @@ func Dump(filename string) {
log.Info("Start dumping config file...") log.Info("Start dumping config file...")
err = writeFileToZip(viper.ConfigFileUsed(), dumpWriter) err = writeFileToZip(viper.ConfigFileUsed(), dumpWriter)
if err != nil { if err != nil {
log.Criticalf("Error saving config file: %s", err) return fmt.Errorf("error saving config file: %s", err)
} }
log.Info("Dumped config file") log.Info("Dumped config file")
@ -55,7 +56,7 @@ func Dump(filename string) {
log.Info("Start dumping version file...") log.Info("Start dumping version file...")
err = writeBytesToZip("VERSION", []byte(version.Version), dumpWriter) err = writeBytesToZip("VERSION", []byte(version.Version), dumpWriter)
if err != nil { if err != nil {
log.Criticalf("Error saving version: %s", err) return fmt.Errorf("error saving version: %s", err)
} }
log.Info("Dumped version") log.Info("Dumped version")
@ -63,12 +64,12 @@ func Dump(filename string) {
log.Info("Start dumping database...") log.Info("Start dumping database...")
data, err := db.Dump() data, err := db.Dump()
if err != nil { if err != nil {
log.Criticalf("Error saving database data: %s", err) return fmt.Errorf("error saving database data: %s", err)
} }
for t, d := range data { for t, d := range data {
err = writeBytesToZip("database/"+t+".json", d, dumpWriter) err = writeBytesToZip("database/"+t+".json", d, dumpWriter)
if err != nil { if err != nil {
log.Criticalf("Error writing database table %s: %s", t, err) return fmt.Errorf("error writing database table %s: %s", t, err)
} }
} }
log.Info("Dumped database") log.Info("Dumped database")
@ -77,19 +78,19 @@ func Dump(filename string) {
log.Info("Start dumping files...") log.Info("Start dumping files...")
allFiles, err := files.Dump() allFiles, err := files.Dump()
if err != nil { if err != nil {
log.Criticalf("Error saving file: %s", err) return fmt.Errorf("error saving file: %s", err)
} }
for fid, fcontent := range allFiles { for fid, fcontent := range allFiles {
err = writeBytesToZip("files/"+strconv.FormatInt(fid, 10), fcontent, dumpWriter) err = writeBytesToZip("files/"+strconv.FormatInt(fid, 10), fcontent, dumpWriter)
if err != nil { if err != nil {
log.Criticalf("Error writing file %d: %s", fid, err) return fmt.Errorf("error writing file %d: %s", fid, err)
} }
} }
log.Infof("Dumped files") log.Infof("Dumped files")
log.Info("Done creating dump") log.Info("Done creating dump")
log.Infof("Dump file saved at %s", filename) log.Infof("Dump file saved at %s", filename)
return nil
} }
func writeFileToZip(filename string, writer *zip.Writer) error { func writeFileToZip(filename string, writer *zip.Writer) error {