package main
import (
"os"
"path/filepath"
"github.com/navidrome/navidrome/plugins/pdk/go/host"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
type TestLibraryInput struct {
Operation string `json:"operation"`
LibraryID int32 `json:"library_id,omitempty"`
MountPoint string `json:"mount_point,omitempty"`
FilePath string `json:"file_path,omitempty"`
Content string `json:"content,omitempty"`
Target string `json:"target,omitempty"`
}
type TestLibraryOutput struct {
Library *host.Library `json:"library,omitempty"`
Libraries []host.Library `json:"libraries,omitempty"`
FileContent string `json:"file_content,omitempty"`
DirEntries []string `json:"dir_entries,omitempty"`
Error *string `json:"error,omitempty"`
}
func ndTestLibrary() int32 {
var input TestLibraryInput
if err := pdk.InputJSON(&input); err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
switch input.Operation {
case "get_library":
library, err := host.LibraryGetLibrary(input.LibraryID)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestLibraryOutput{Library: library})
return 0
case "get_all_libraries":
libraries, err := host.LibraryGetAllLibraries()
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestLibraryOutput{Libraries: libraries})
return 0
case "read_file":
fullPath := filepath.Join(input.MountPoint, input.FilePath)
content, err := os.ReadFile(fullPath)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestLibraryOutput{FileContent: string(content)})
return 0
case "list_dir":
entries, err := os.ReadDir(input.MountPoint)
if err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
var names []string
for _, entry := range entries {
names = append(names, entry.Name())
}
pdk.OutputJSON(TestLibraryOutput{DirEntries: names})
return 0
case "write_file":
fullPath := filepath.Join(input.MountPoint, input.FilePath)
if err := os.WriteFile(fullPath, []byte(input.Content), 0600); err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestLibraryOutput{})
return 0
case "symlink":
fullPath := filepath.Join(input.MountPoint, input.FilePath)
if err := os.Symlink(input.Target, fullPath); err != nil {
errStr := err.Error()
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
pdk.OutputJSON(TestLibraryOutput{})
return 0
default:
errStr := "unknown operation: " + input.Operation
pdk.OutputJSON(TestLibraryOutput{Error: &errStr})
return 0
}
}
func main() {}