refactor: use struct instead of map for dsc responses

This commit is contained in:
Jan Bader
2025-01-02 23:47:53 +01:00
parent d919f5ce9e
commit fb741e2615

36
main.go
View File

@ -19,6 +19,21 @@ type config struct {
ImportDirectories []string
}
type FileInfo struct {
Exists bool `json:"exists"`
Items []Item `json:"items"`
File string `json:"file"`
}
type Item struct {
ID string `json:"id"`
Name string `json:"name"`
Direction string `json:"direction"`
State string `json:"state"`
Created int64 `json:"created"`
ItemDate int64 `json:"item_date"`
}
func main() {
fmt.Println("##################### START #####################")
fmt.Println(" Docspell Consumedir Cleaner - v0.1 beta")
@ -111,18 +126,18 @@ func main() {
return nil
}
var fileExistsResponse []map[string]interface{}
var fileExistsResponse FileInfo
if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
fmt.Printf("ERROR parsing response: %v\n", err)
return nil
}
if fileExistsResponse[0]["exists"].(bool) {
if fileExistsResponse.Exists {
// File exists in Docspell
items := fileExistsResponse[0]["items"].([]interface{})
item := items[0].(map[string]interface{})
itemID := item["id"].(string)
itemName := item["name"].(string)
items := fileExistsResponse.Items
item := items[0]
itemID := item.ID
itemName := item.Name
// Get item details
cmd = exec.Command("dsc", "-f", "json", "item", "get", itemID)
@ -150,14 +165,13 @@ func main() {
fmt.Printf("File already exists: '%s @ %s/app/item/%s'\n", itemName, dsUrl, itemID)
state := item["state"].(string)
state := item.State
if state == "confirmed" {
itemDate := item["item_date"]
if itemDate == nil {
itemDate := item.ItemDate
if itemDate == 0 {
fmt.Println("... but has no date - not doing anything.")
} else {
timestamp := int64(itemDate.(float64)) / 1000
date := time.Unix(timestamp, 0)
date := time.Unix(itemDate/1000, 0)
curDir := filepath.Join(cfg.ArchiveDirectory, folder, date.Format("2006/01"))
if err := os.MkdirAll(curDir, 0755); err != nil {