2022-03-30 01:30:29 +02:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
2022-04-03 18:18:10 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-03-30 01:30:29 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/rumanzo/bt2qbt/internal/options"
|
2022-04-03 18:18:10 +02:00
|
|
|
"io/ioutil"
|
2022-03-30 01:30:29 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-04-03 18:18:10 +02:00
|
|
|
func ProcessLabels(opts *options.Opts, newtags []string) error {
|
|
|
|
categories := map[string]map[string]string{}
|
|
|
|
|
|
|
|
// check if categories is new file. If it exists it must be unmarshaled. Default categories file contains only {}
|
|
|
|
var categoriesIsNew bool
|
|
|
|
file, err := os.OpenFile(opts.Categories, os.O_RDWR, 0644)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
categoriesIsNew = true
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Unexpected error while open categories.json. Error:\n%v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !categoriesIsNew {
|
|
|
|
dataRaw, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Unexpected error while read categories.json. Error:\n%v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Can't close categories.json. Error:\n%v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(dataRaw, &categories)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Unexpected error while unmarshaling categories.json. Error:\n%v\n", err))
|
|
|
|
}
|
2022-03-30 01:30:29 +02:00
|
|
|
}
|
2022-04-03 18:18:10 +02:00
|
|
|
|
|
|
|
for _, tag := range newtags {
|
|
|
|
if _, ok := categories[tag]; !ok { // append only if key doesn't already exist
|
|
|
|
categories[tag] = map[string]string{"save_path": ""}
|
2022-03-30 01:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 18:18:10 +02:00
|
|
|
|
|
|
|
if !categoriesIsNew {
|
|
|
|
err = os.Rename(opts.Categories, opts.Categories+".bak")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Can't move categories.json to categories.bak. Error:\n%v\n", err))
|
2022-03-30 01:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 18:18:10 +02:00
|
|
|
|
|
|
|
newCategories, err := json.Marshal(categories)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Can't marshal categories. Error:\n%v\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(opts.Categories, newCategories, 0644)
|
2022-03-30 01:30:29 +02:00
|
|
|
if err != nil {
|
2022-04-03 18:18:10 +02:00
|
|
|
return errors.New(fmt.Sprintf("Can't write categories.json. Error:\n%v\n", err))
|
2022-03-30 01:30:29 +02:00
|
|
|
}
|
2022-04-03 18:18:10 +02:00
|
|
|
|
|
|
|
return nil
|
2022-03-30 01:30:29 +02:00
|
|
|
}
|