refactor Import()

This commit is contained in:
2024-12-22 11:29:12 +01:00
parent dc328afa44
commit be79886e89
3 changed files with 52 additions and 29 deletions

View File

@@ -289,14 +289,14 @@ func (db *DB) Del(attr *DbAttr) error {
return err return err
} }
func (db *DB) Import(attr *DbAttr) error { func (db *DB) Import(attr *DbAttr) (string, error) {
// open json file into attr.Val // open json file into attr.Val
if err := attr.GetFileValue(); err != nil { if err := attr.GetFileValue(); err != nil {
return err return "", err
} }
if attr.Val == "" { if attr.Val == "" {
return errors.New("empty json file") return "", errors.New("empty json file")
} }
var entries DbEntries var entries DbEntries
@@ -304,21 +304,21 @@ func (db *DB) Import(attr *DbAttr) error {
newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05") newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05")
if err := json.Unmarshal([]byte(attr.Val), &entries); err != nil { 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) { if fileExists(db.Dbfile) {
// backup the old file // backup the old file
err := os.Rename(db.Dbfile, newfile) err := os.Rename(db.Dbfile, newfile)
if err != nil { 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 // should now be a new db file
if err := db.Open(); err != nil { if err := db.Open(); err != nil {
return cleanError(newfile, err) return "", cleanError(newfile, err)
} }
defer db.Close() defer db.Close()
@@ -345,28 +345,9 @@ func (db *DB) Import(attr *DbAttr) error {
}) })
if err != nil { if err != nil {
return cleanError(newfile, err) return "", cleanError(newfile, err)
} }
fmt.Printf("backed up database file to %s\n", newfile) return fmt.Sprintf("backed up database file to %s\nimported %d database entries\n",
fmt.Printf("imported %d database entries\n", len(entries)) newfile, len(entries)), nil
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()
} }

36
app/io.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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()
}

View File

@@ -279,7 +279,13 @@ func Import(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown // errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true cmd.SilenceUsage = true
return conf.DB.Import(&attr) out, err := conf.DB.Import(&attr)
if err != nil {
return err
}
fmt.Print(out)
return nil
}, },
} }