docs: add library examples for calendar, crm, subtasks, applications

Four new executable examples mirror the oo-cli subcommands at library level so
downstream Go consumers can see the typed API without reading the CLI wiring.
README table updated to list all examples.

Made-with: Cursor
This commit is contained in:
2026-04-24 11:46:09 +01:00
parent 43e385ce9d
commit c7ec0f3e7f
5 changed files with 234 additions and 0 deletions
+4
View File
@@ -566,6 +566,10 @@ the command reference.
| Example | Description | | Example | Description |
|---|---| |---|---|
| [basic](examples/basic/) | List projects and users | | [basic](examples/basic/) | List projects and users |
| [calendar](examples/calendar/) | List calendars and events, create a new event |
| [crm](examples/crm/) | List contacts and opportunities, add company/deal/history note |
| [subtasks](examples/subtasks/) | Create a parent task and attach subtasks |
| [applications](examples/applications/) | Job application folder tree → OnlyOffice CRM (library-level usage of the internal applications package) |
| [`cmd/oo-cli`](cmd/oo-cli/) | Full-featured CLI using all modules | | [`cmd/oo-cli`](cmd/oo-cli/) | Full-featured CLI using all modules |
## Related Libraries ## Related Libraries
+54
View File
@@ -0,0 +1,54 @@
// Package main shows how to drive the internal applications package — the same
// logic that powers `oo-cli applications-sync` — directly from Go code.
//
// export ONLYOFFICE_URL="https://your-instance.onlyoffice.com"
// export ONLYOFFICE_USER="admin@example.com"
// export ONLYOFFICE_PASS="your-password"
// go run ./examples/applications /path/to/cv/applications
//
// Without --apply equivalent, it prints a summary but performs no writes.
package main
import (
"context"
"fmt"
"log"
"os"
onlyoffice "github.com/eslider/go-onlyoffice"
"github.com/eslider/go-onlyoffice/internal/applications"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "usage: applications <applications-root>")
os.Exit(2)
}
root := os.Args[1]
creds := onlyoffice.GetEnvironmentCredentials()
if creds.Url == "" {
fmt.Fprintln(os.Stderr, "ONLYOFFICE_URL is not set")
os.Exit(1)
}
client := onlyoffice.NewClient(creds)
paths, err := applications.Discover(root)
if err != nil {
log.Fatalf("discover: %v", err)
}
fmt.Printf("discovered %d application READMEs under %s\n", len(paths), root)
var apps []applications.Data
for _, p := range paths {
d, err := applications.ParseReadme(p)
if err != nil {
log.Printf("parse %s: %v", p, err)
continue
}
apps = append(apps, d)
}
stats := applications.Sync(context.Background(), client, apps, true /*dryRun*/, true /*verbose*/)
fmt.Printf("\ndry-run stats: %+v\n", stats)
}
+62
View File
@@ -0,0 +1,62 @@
// Package main shows how to list calendars and events, and create a new event
// via the OnlyOffice Calendar API.
//
// export ONLYOFFICE_URL="https://your-instance.onlyoffice.com"
// export ONLYOFFICE_USER="admin@example.com"
// export ONLYOFFICE_PASS="your-password"
// # optional: default calendar for AddEvent
// export ONLYOFFICE_CALENDAR_ID="42"
// go run ./examples/calendar
package main
import (
"context"
"fmt"
"log"
"os"
"time"
onlyoffice "github.com/eslider/go-onlyoffice"
)
func main() {
creds := onlyoffice.GetEnvironmentCredentials()
if creds.Url == "" {
fmt.Fprintln(os.Stderr, "ONLYOFFICE_URL is not set")
os.Exit(1)
}
client := onlyoffice.NewClient(creds)
client.SetDefaults(onlyoffice.GetEnvironmentDefaults())
ctx := context.Background()
start := time.Now().Format("2006-01-02")
end := time.Now().AddDate(0, 1, 0).Format("2006-01-02")
calendars, err := client.ListCalendars(ctx, start, end)
if err != nil {
log.Fatalf("list calendars: %v", err)
}
fmt.Printf("Calendars (%d):\n", len(calendars))
for _, cal := range calendars {
fmt.Printf(" - id=%v title=%v\n", cal["objectId"], cal["title"])
}
events, err := client.ListEvents(ctx, start, end)
if err != nil {
log.Fatalf("list events: %v", err)
}
fmt.Printf("\nEvents in [%s..%s]: %d\n", start, end, len(events))
for _, e := range events {
fmt.Printf(" - %v @ %v\n", e["title"], e["start"])
}
// Create demo event — uncomment to exercise.
// evStart := time.Now().Add(time.Hour).Format(time.RFC3339)
// evEnd := time.Now().Add(2 * time.Hour).Format(time.RFC3339)
// created, err := client.AddEvent(ctx, "", "Library demo", evStart, evEnd, "via examples/calendar", false)
// if err != nil {
// log.Fatalf("add event: %v", err)
// }
// fmt.Printf("created event id=%v\n", created["objectId"])
}
+63
View File
@@ -0,0 +1,63 @@
// Package main lists CRM contacts and opportunities, and shows how to create
// and remove a demo company + opportunity + history note in OnlyOffice CRM.
//
// export ONLYOFFICE_URL="https://your-instance.onlyoffice.com"
// export ONLYOFFICE_USER="admin@example.com"
// export ONLYOFFICE_PASS="your-password"
// go run ./examples/crm
package main
import (
"context"
"fmt"
"log"
"os"
onlyoffice "github.com/eslider/go-onlyoffice"
)
func main() {
creds := onlyoffice.GetEnvironmentCredentials()
if creds.Url == "" {
fmt.Fprintln(os.Stderr, "ONLYOFFICE_URL is not set")
os.Exit(1)
}
client := onlyoffice.NewClient(creds)
ctx := context.Background()
contacts, total, err := client.ListContacts(ctx, 10, 0, "")
if err != nil {
log.Fatalf("list contacts: %v", err)
}
fmt.Printf("CRM contacts: %d (total=%d)\n", len(contacts), total)
for _, c := range contacts {
fmt.Printf(" - id=%v name=%v\n", c["id"], c["displayName"])
}
opps, oppTotal, err := client.ListOpportunities(ctx, 10, 0)
if err != nil {
log.Fatalf("list opportunities: %v", err)
}
fmt.Printf("\nOpportunities: %d (total=%d)\n", len(opps), oppTotal)
for _, o := range opps {
fmt.Printf(" - id=%v title=%v\n", o["id"], o["title"])
}
// Full create/attach/delete cycle — uncomment to exercise.
// company, err := client.CreateCompany(ctx, "Demo GmbH")
// if err != nil { log.Fatalf("create company: %v", err) }
// cid := fmt.Sprint(company["id"])
// defer client.DeleteContact(ctx, cid)
//
// stages, err := client.ListDealStages(ctx)
// if err != nil || len(stages) == 0 { log.Fatalf("no deal stages: %v", err) }
// stageID := int(stages[0]["id"].(float64))
//
// opp, err := client.CreateOpportunity(ctx, "Demo opp", stageID, "", "EUR", "from examples/crm", 0)
// if err != nil { log.Fatalf("create opportunity: %v", err) }
// oid := fmt.Sprint(opp["id"])
// _, _ = client.AddOpportunityMember(ctx, oid, cid)
// _, _ = client.AddHistoryNote(ctx, "opportunity", int(opp["id"].(float64)), "Touched via library example", 0)
// _, _ = client.DeleteOpportunity(ctx, oid)
}
+51
View File
@@ -0,0 +1,51 @@
// Package main creates a parent task and attaches two subtasks using the
// project-tasks API (POST /api/2.0/project/task/{id}.json).
//
// export ONLYOFFICE_URL="https://your-instance.onlyoffice.com"
// export ONLYOFFICE_USER="admin@example.com"
// export ONLYOFFICE_PASS="your-password"
// export ONLYOFFICE_PROJECT_ID="123"
// go run ./examples/subtasks
package main
import (
"context"
"fmt"
"log"
"os"
onlyoffice "github.com/eslider/go-onlyoffice"
)
func main() {
creds := onlyoffice.GetEnvironmentCredentials()
defaults := onlyoffice.GetEnvironmentDefaults()
if creds.Url == "" || defaults.ProjectID == "" {
fmt.Fprintln(os.Stderr, "ONLYOFFICE_URL and ONLYOFFICE_PROJECT_ID must be set")
os.Exit(1)
}
client := onlyoffice.NewClient(creds)
client.SetDefaults(defaults)
ctx := context.Background()
parent, err := client.AddTask(ctx, defaults.ProjectID, "Example parent", "Created by examples/subtasks", 0, "")
if err != nil {
log.Fatalf("add parent task: %v", err)
}
parentID := fmt.Sprint(parent["id"])
fmt.Printf("parent task id=%s\n", parentID)
for _, title := range []string{"first subtask", "second subtask"} {
st, err := client.AddSubtask(ctx, parentID, title)
if err != nil {
log.Fatalf("add subtask %q: %v", title, err)
}
fmt.Printf(" + subtask id=%v title=%v\n", st["id"], st["title"])
}
// Cleanup — uncomment once you verified the subtasks in the OnlyOffice UI.
// if _, err := client.DeleteTask(ctx, parentID); err != nil {
// log.Printf("cleanup: %v", err)
// }
}