refactor: use struct instead of map for dsc get
This commit is contained in:
148
main.go
148
main.go
@ -19,13 +19,13 @@ type config struct {
|
||||
ImportDirectories []string
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
Exists bool `json:"exists"`
|
||||
Items []Item `json:"items"`
|
||||
File string `json:"file"`
|
||||
type FileExistsResult struct {
|
||||
Exists bool `json:"exists"`
|
||||
Items []DocspellItem `json:"items"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
type DocspellItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Direction string `json:"direction"`
|
||||
@ -85,8 +85,6 @@ func main() {
|
||||
os.Exit(-4)
|
||||
}
|
||||
|
||||
dsUrl := cfg.DocspellURL
|
||||
|
||||
fmt.Println("Settings:")
|
||||
uploadMissing := os.Getenv("DS_CC_UPLOAD_MISSING") == "true"
|
||||
if uploadMissing {
|
||||
@ -126,70 +124,16 @@ func main() {
|
||||
return nil
|
||||
}
|
||||
|
||||
var fileExistsResponse FileInfo
|
||||
var fileExistsResponse []FileExistsResult
|
||||
if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
|
||||
fmt.Printf("ERROR parsing response: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if fileExistsResponse.Exists {
|
||||
// File exists in Docspell
|
||||
items := fileExistsResponse.Items
|
||||
item := items[0]
|
||||
itemID := item.ID
|
||||
itemName := item.Name
|
||||
|
||||
// Get item details
|
||||
cmd = exec.Command("dsc", "-f", "json", "item", "get", itemID)
|
||||
output, err = cmd.Output()
|
||||
if fileExistsResponse[0].Exists {
|
||||
err := handleExistingFile(cfg, fileExistsResponse[0])
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR getting item details: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var itemDetails map[string]interface{}
|
||||
if err := json.Unmarshal(output, &itemDetails); err != nil {
|
||||
fmt.Printf("ERROR parsing item details: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
folder := itemDetails["folder"].(map[string]interface{})["name"].(string)
|
||||
extension := filepath.Ext(path)[1:]
|
||||
|
||||
var corr string
|
||||
if corrOrg, ok := itemDetails["corr_org"].(map[string]interface{}); ok && corrOrg["name"] != nil {
|
||||
corr = corrOrg["name"].(string)
|
||||
} else if corrPerson, ok := itemDetails["corr_person"].(map[string]interface{}); ok && corrPerson["name"] != nil {
|
||||
corr = corrPerson["name"].(string)
|
||||
}
|
||||
|
||||
fmt.Printf("File already exists: '%s @ %s/app/item/%s'\n", itemName, dsUrl, itemID)
|
||||
|
||||
state := item.State
|
||||
if state == "confirmed" {
|
||||
itemDate := item.ItemDate
|
||||
if itemDate == 0 {
|
||||
fmt.Println("... but has no date - not doing anything.")
|
||||
} else {
|
||||
date := time.Unix(itemDate/1000, 0)
|
||||
curDir := filepath.Join(cfg.ArchiveDirectory, folder, date.Format("2006/01"))
|
||||
|
||||
if err := os.MkdirAll(curDir, 0755); err != nil {
|
||||
fmt.Printf("ERROR creating directory: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
newPath := filepath.Join(curDir, fmt.Sprintf("%s %s - %s.%s",
|
||||
date.Format("20060102"), corr, itemName, extension))
|
||||
|
||||
if err := os.Rename(path, newPath); err != nil {
|
||||
fmt.Printf("ERROR moving file: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("... moving to archive by date ('%s')\n", curDir)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("... but is not confirmed yet - not doing anything.")
|
||||
fmt.Println("ERROR", err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Files does not exist, yet")
|
||||
@ -229,6 +173,68 @@ func main() {
|
||||
fmt.Println(time.Now().Format(time.RFC1123))
|
||||
}
|
||||
|
||||
func handleExistingFile(cfg config, fileExistsResponse FileExistsResult) error {
|
||||
// File exists in Docspell
|
||||
items := fileExistsResponse.Items
|
||||
item := items[0]
|
||||
itemID := item.ID
|
||||
itemName := item.Name
|
||||
|
||||
// Get item details
|
||||
cmd := exec.Command("dsc", "-f", "json", "item", "get", itemID)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get item details: %w", err)
|
||||
}
|
||||
|
||||
var itemDetails DocspellItemDetails
|
||||
if err := json.Unmarshal(output, &itemDetails); err != nil {
|
||||
return fmt.Errorf("pars item details: %w", err)
|
||||
}
|
||||
|
||||
folder := "null"
|
||||
if itemDetails.Folder != nil {
|
||||
folder = itemDetails.Folder.Name
|
||||
}
|
||||
extension := filepath.Ext(fileExistsResponse.File)[1:]
|
||||
|
||||
var corr string
|
||||
if itemDetails.CorrespondingOrganisation != nil && itemDetails.CorrespondingOrganisation.Name != "" {
|
||||
corr = itemDetails.CorrespondingOrganisation.Name
|
||||
} else if itemDetails.CorrespondingPerson != nil && itemDetails.CorrespondingPerson.Name != "" {
|
||||
corr = itemDetails.CorrespondingPerson.Name
|
||||
}
|
||||
fmt.Printf("File already exists: '%s @ %s/app/item/%s'\n", itemName, cfg.DocspellURL, itemID)
|
||||
|
||||
state := item.State
|
||||
if state != "confirmed" {
|
||||
fmt.Println("... but is not confirmed yet - not doing anything.")
|
||||
return nil
|
||||
}
|
||||
|
||||
itemDate := item.ItemDate
|
||||
if itemDate == 0 {
|
||||
fmt.Println("... but has no date - not doing anything.")
|
||||
return nil
|
||||
}
|
||||
date := time.Unix(itemDate/1000, 0)
|
||||
curDir := filepath.Join(cfg.ArchiveDirectory, folder, date.Format("2006/01"))
|
||||
|
||||
if err := os.MkdirAll(curDir, 0755); err != nil {
|
||||
return fmt.Errorf("create directory: %w", err)
|
||||
}
|
||||
|
||||
subfolder := fmt.Sprintf("%s %s - %s.%s", date.Format("20060102"), corr, itemName, extension)
|
||||
newPath := filepath.Join(curDir, subfolder)
|
||||
|
||||
if err := os.Rename(fileExistsResponse.File, newPath); err != nil {
|
||||
return fmt.Errorf("move file: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("... moving to archive by date ('%s')\n", curDir)
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateConfig(cfg config) {
|
||||
if len(cfg.ImportDirectories) == 0 || cfg.ArchiveDirectory == "" {
|
||||
fmt.Println("FATAL Parameter missing")
|
||||
@ -242,3 +248,15 @@ func validateConfig(cfg config) {
|
||||
os.Exit(-3)
|
||||
}
|
||||
}
|
||||
|
||||
type DocspellItemDetails struct {
|
||||
DocspellItem
|
||||
CorrespondingOrganisation *DocspellEntity `json:"corr-org"`
|
||||
CorrespondingPerson *DocspellEntity `json:"corr-person"`
|
||||
Folder *DocspellEntity `json:"folder"`
|
||||
}
|
||||
|
||||
type DocspellEntity struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
Reference in New Issue
Block a user