Add nidusctl admin CLI for managing calendar/address-book shares
- tools/nidusctl: subcommands `calendar {share,unshare,shares}` and
`addressbook {share,unshare,shares}`, thin wrapper around internal/db.
Warns (non-fatal) if owner/user isn't in config.yaml.
- internal/db: add SharesOfAddressBook (owner-perspective query, mirrors
SharesOfCalendar) needed by the CLI.
- internal/db: db.Open now creates the parent data directory itself, so
the CLI works standalone without requiring the server to have run first.
- Update Makefile (build bin/nidusctl, new 'nidusctl' target), README and
copilot-instructions with usage docs.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+10
-1
@@ -8,6 +8,8 @@ package db
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -19,8 +21,15 @@ type DB struct {
|
||||
}
|
||||
|
||||
// Open opens (creating if necessary) the SQLite database at path and runs
|
||||
// schema migrations.
|
||||
// schema migrations. The parent directory of path is created if it
|
||||
// doesn't already exist.
|
||||
func Open(path string) (*DB, error) {
|
||||
if dir := filepath.Dir(path); dir != "" && dir != "." {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("creating database directory %q: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening database %q: %w", path, err)
|
||||
|
||||
@@ -178,6 +178,33 @@ func (d *DB) UnshareAddressBook(owner, bookName, sharedWith string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SharesOfAddressBook lists everyone owner's address book bookName has
|
||||
// been shared with.
|
||||
func (d *DB) SharesOfAddressBook(owner, bookName string) ([]AddressBookShare, error) {
|
||||
rows, err := d.conn.Query(`
|
||||
SELECT owner, addressbook_name, shared_with, permission
|
||||
FROM addressbook_shares
|
||||
WHERE owner = ? AND addressbook_name = ?
|
||||
ORDER BY shared_with`,
|
||||
owner, bookName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing address book shares: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var shares []AddressBookShare
|
||||
for rows.Next() {
|
||||
var s AddressBookShare
|
||||
var perm string
|
||||
if err := rows.Scan(&s.Owner, &s.AddressBookName, &s.SharedWith, &perm); err != nil {
|
||||
return nil, fmt.Errorf("scanning address book share: %w", err)
|
||||
}
|
||||
s.Permission = Permission(perm)
|
||||
shares = append(shares, s)
|
||||
}
|
||||
return shares, rows.Err()
|
||||
}
|
||||
|
||||
// AddressBooksSharedWith lists all address books (from any owner) that
|
||||
// have been shared with user.
|
||||
func (d *DB) AddressBooksSharedWith(user string) ([]AddressBookShare, error) {
|
||||
|
||||
Reference in New Issue
Block a user