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