cloudflare

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnauthorized = errors.New("unauthorized")

Functions

func ConsoleURL

func ConsoleURL(accountID, workerName string) string

func CreateTail

func CreateTail(accountID, token, workerName string) (tailID, wsURL string, err error)

CreateTail starts a tail session for real-time worker logs. POST /accounts/{accountId}/workers/scripts/{workerName}/tails Returns the tail session ID and WebSocket URL.

func DeleteKV added in v0.0.4

func DeleteKV(accountID, token, namespaceID string) error

DeleteKV deletes a KV namespace.

func DeleteTail

func DeleteTail(accountID, token, workerName, tailID string) error

DeleteTail removes a tail session. DELETE /accounts/{accountId}/workers/scripts/{workerName}/tails/{tailId}

func DeleteWorker

func DeleteWorker(accountID, token, workerName string) error

DeleteWorker deletes a worker script.

func Deploy

func Deploy(params DeployParams) error

Deploy orchestrates the full worker deployment sequence:

  1. Find/create KV namespace (per-worker isolation)
  2. Write refresh token to KV (initial value)
  3. Upload worker with KV binding
  4. Set cron schedule
  5. Store secrets (REFRESH_TOKEN, TIMEZONE, optional NOTIFICATION_CONFIG)

func EnsureAccountAccess

func EnsureAccountAccess(token, accountID string) error

func FindKV

func FindKV(accountID, token, title string) (namespaceID string, err error)

FindKV finds a KV namespace by title. Returns error if not found.

func FindOrCreateKV

func FindOrCreateKV(accountID, token, title string) (namespaceID string, err error)

FindOrCreateKV finds a KV namespace by title, creating it if not found. Returns the namespace ID.

func GenerateTestWorkerCode

func GenerateTestWorkerCode() string

func GenerateWorkerCode

func GenerateWorkerCode() string

func GetWranglerToken

func GetWranglerToken() (string, error)

GetWranglerToken calls `wrangler auth token --json`. Returns a Bearer token for the CF API, or an error. For api_key auth: CF API uses X-Auth-Key + X-Auth-Email headers, but this is legacy — we reject it and suggest switching to API token.

func InstallWrangler

func InstallWrangler() error

InstallWrangler attempts to install wrangler globally. Tries npm first, then brew on macOS.

func IsWranglerInstalled

func IsWranglerInstalled() bool

IsWranglerInstalled checks if wrangler is on PATH.

func RunWranglerLogin

func RunWranglerLogin() error

RunWranglerLogin runs `wrangler login` interactively (needs TTY).

func RunWranglerLogout added in v0.0.2

func RunWranglerLogout() error

RunWranglerLogout runs `wrangler logout` to clear stored credentials.

func SetSchedule

func SetSchedule(accountID, token, workerName, cron string) error

SetSchedule sets cron triggers for a worker. Body is an ARRAY of cron objects: [{"cron": "..."}]

func SetSecret

func SetSecret(accountID, token, workerName, name, value string) error

SetSecret sets a worker secret. Body uses "text" field (NOT "value") and "type": "secret_text".

func UploadWorker

func UploadWorker(accountID, token, workerName, code, kvNSID string) error

UploadWorker uploads worker code as a multipart form with metadata. This is the most complex CF API call — uses multipart/form-data with:

  • Part 1: "worker.js" with Content-Type: application/javascript+module
  • Part 2: "metadata" with Content-Type: application/json (bindings, main_module, etc.)

func WriteKVValue

func WriteKVValue(accountID, token, nsID, key, value string) error

WriteKVValue writes a plain text value to a KV key. Note: body is raw string (NOT JSON-encoded), per CF API spec.

Types

type AuthResult

type AuthResult struct {
	Token     string
	AccountID string
	Source    string // "env", "wrangler", "manual"
}

AuthResult holds the resolved authentication credentials.

func ResolveFromEnv

func ResolveFromEnv() *AuthResult

ResolveFromEnv returns token from CLOUDFLARE_API_TOKEN env var if set.

type AuthState

type AuthState int

AuthState represents the current Cloudflare authentication status.

const (
	AuthFromEnv            AuthState = iota // CLOUDFLARE_API_TOKEN env var is set
	AuthFromWrangler                        // wrangler auth token --json succeeded
	AuthWranglerNeedsLogin                  // wrangler found but not logged in
	AuthWranglerMissing                     // wrangler not installed
)

func DetectAuthState

func DetectAuthState() AuthState

DetectAuthState checks available auth methods without prompting.

func (AuthState) String

func (s AuthState) String() string

String returns a human-readable name for the auth state.

type DeployParams

type DeployParams struct {
	Token              string
	AccountID          string
	WorkerName         string
	WorkerCode         string
	RefreshToken       string
	CronExpression     string
	Timezone           string
	NotificationConfig string // optional JSON: {"slackWebhook":"...","failureOnly":true}
	OnProgress         func(step string)
}

DeployParams holds all parameters needed for a full worker deployment.

type VerifyResult

type VerifyResult struct {
	AccountID   string
	AccountName string
	Email       string
}

func VerifyToken

func VerifyToken(token string) (*VerifyResult, error)

VerifyToken verifies the CF token and returns account info. Supports both dashboard API tokens (/user/tokens/verify) and wrangler OAuth tokens (/user) — OAuth tokens don't work with /tokens/verify.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL