diff --git a/README.md b/README.md index 6feb2a9..9752c3b 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,10 @@ the command reference. | Example | Description | |---|---| | [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 | ## Related Libraries diff --git a/examples/applications/main.go b/examples/applications/main.go new file mode 100644 index 0000000..b94bf7f --- /dev/null +++ b/examples/applications/main.go @@ -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 ") + 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) +} diff --git a/examples/calendar/main.go b/examples/calendar/main.go new file mode 100644 index 0000000..59c7f44 --- /dev/null +++ b/examples/calendar/main.go @@ -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"]) +} diff --git a/examples/crm/main.go b/examples/crm/main.go new file mode 100644 index 0000000..3850227 --- /dev/null +++ b/examples/crm/main.go @@ -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) +} diff --git a/examples/subtasks/main.go b/examples/subtasks/main.go new file mode 100644 index 0000000..d44f4fc --- /dev/null +++ b/examples/subtasks/main.go @@ -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) + // } +}