diff --git a/app/db.go b/app/db.go index 365cd5e..f5cc4cb 100644 --- a/app/db.go +++ b/app/db.go @@ -289,14 +289,14 @@ func (db *DB) Del(attr *DbAttr) error { return err } -func (db *DB) Import(attr *DbAttr) error { +func (db *DB) Import(attr *DbAttr) (string, error) { // open json file into attr.Val if err := attr.GetFileValue(); err != nil { - return err + return "", err } if attr.Val == "" { - return errors.New("empty json file") + return "", errors.New("empty json file") } var entries DbEntries @@ -304,21 +304,21 @@ func (db *DB) Import(attr *DbAttr) error { newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05") if err := json.Unmarshal([]byte(attr.Val), &entries); err != nil { - return cleanError(newfile, fmt.Errorf("failed to unmarshal json: %w", err)) + return "", cleanError(newfile, fmt.Errorf("failed to unmarshal json: %w", err)) } if fileExists(db.Dbfile) { // backup the old file err := os.Rename(db.Dbfile, newfile) if err != nil { - return fmt.Errorf("failed to rename file %s to %s: %w", db.Dbfile, newfile, err) + return "", fmt.Errorf("failed to rename file %s to %s: %w", db.Dbfile, newfile, err) } } // should now be a new db file if err := db.Open(); err != nil { - return cleanError(newfile, err) + return "", cleanError(newfile, err) } defer db.Close() @@ -345,28 +345,9 @@ func (db *DB) Import(attr *DbAttr) error { }) if err != nil { - return cleanError(newfile, err) + return "", cleanError(newfile, err) } - fmt.Printf("backed up database file to %s\n", newfile) - fmt.Printf("imported %d database entries\n", len(entries)) - - return nil -} - -func cleanError(file string, err error) error { - // remove given [backup] file and forward the given error - os.Remove(file) - return err -} - -func fileExists(filename string) bool { - info, err := os.Stat(filename) - - if err != nil { - // return false on any error - return false - } - - return !info.IsDir() + return fmt.Sprintf("backed up database file to %s\nimported %d database entries\n", + newfile, len(entries)), nil } diff --git a/app/io.go b/app/io.go new file mode 100644 index 0000000..109e770 --- /dev/null +++ b/app/io.go @@ -0,0 +1,36 @@ +/* +Copyright © 2024 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package app + +import "os" + +func cleanError(file string, err error) error { + // remove given [backup] file and forward the given error + os.Remove(file) + return err +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + + if err != nil { + // return false on any error + return false + } + + return !info.IsDir() +} diff --git a/cmd/crud.go b/cmd/crud.go index 6479f62..f7c8755 100644 --- a/cmd/crud.go +++ b/cmd/crud.go @@ -279,7 +279,13 @@ func Import(conf *cfg.Config) *cobra.Command { // errors at this stage do not cause the usage to be shown cmd.SilenceUsage = true - return conf.DB.Import(&attr) + out, err := conf.DB.Import(&attr) + if err != nil { + return err + } + + fmt.Print(out) + return nil }, }