scripts

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 43 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scripts is Repo scripts' service: the commands a clone's own files name (its t3.json scripts, its .vscode/tasks.json tasks, and its magex targets), listed for the page, and run on the user's click through scriptrun.

Listing runs nothing in a clone. t3.json and tasks.json are read as files, tasks.json through a JSONC stripper. magex's built-in targets come from `magex help:commands json=true`, run in a scratch folder of the app's own, never in a clone, whose .github/env files magex would load over its environment, PATH included; the answer is kept per copy of magex. The repository's own targets are read from its magefiles/ Go files with go/parser, by the rules magex itself discovers them by, and kept per clone until a file there changes.

A command from the repository's files asks the user before its first run, and again whenever it changes (D13): its trust is the SHA-256 of the canonical JSON of its effective form, with ${workspaceFolder} and ${env:…} left as written, so moving the clone asks nothing. A custom magex target's form holds the SHA-256 of every file under magefiles/. magex's built-in targets need no trust. A run's Before, under the clone's lock, reads the source again and works the hash out afresh, from the files' bytes, and refuses a command whose hash isn't the one the user confirmed, or the one they trusted before: what runs is exactly what the user saw.

The user's own scripts, saved in the app (Save, Delete, Duplicate, Move), show in the clones their scope includes: every clone, one with a go.mod at its root, or the clones of the repositories they chose. They need no trust, since the user wrote them, and run exactly as written, as the login shell's -c whatever its family (a repository's command line, which the app quotes for POSIX shells, runs in sh unless the login shell is one), in the clone, with the clone's repository, folder, and default branch in Variables, where the script says, or else where the setting sends runs.

BranchStatus tells the page's header whether the clone's branch is behind the branch it follows on origin, read with gitread from the clone's own refs and one ls-remote of that branch alone, whose answer is kept five minutes, and never fetches. Pull brings it level on the user's click: git's fast-forward-only pull of that branch, run in the app under the clone's lock, its Before reading the clone again and working out what git runs with, every one of gitread's guards applied, before git runs, so that the pull's own fetch is the one time origin is asked: a clone whose own config holds a setting gitread refuses is refused, and the status says so before offering a pull. It is the one git command the app runs that changes a clone's checkout, and pull.go the one file that holds it.

It owns the script_trust table, the commands the user trusted, and user_scripts, the user's own scripts, in the order the user put them in.

Index

Constants

View Source
const OriginKept = 5 * time.Minute

OriginKept is how long the answer origin gave about a clone's branches is kept: a branch's status within it asks origin nothing, unless refreshed.

Variables

View Source
var (
	// ErrNotSetUp is returned by a run on a Service built without the runner.
	ErrNotSetUp = errors.New("scripts are not set up")

	// ErrNoClone is returned for a folder that isn't a clone in the repository locations.
	ErrNoClone = errors.New("not a clone in the repository locations")

	// ErrInvalidRequest is returned for a run whose script, inputs, arguments, or place
	// isn't one.
	ErrInvalidRequest = errors.New("invalid scripts request")

	// ErrNotFound is returned for a script its source no longer names.
	ErrNotFound = errors.New("the script is gone")

	// ErrCantRun is returned for a script greyed on the page.
	ErrCantRun = errors.New("the script can't run")

	// ErrRelease is returned for a version bump run without Anyway: Release does that.
	ErrRelease = errors.New("version bumps belong to Release")

	// ErrNeedsTrust is returned for a repository command the user hasn't confirmed as it
	// is now.
	ErrNeedsTrust = errors.New("the command needs the user's trust")

	// ErrChanged is returned for a repository command that changed after the user
	// confirmed it, or one of the user's own scripts saved again as its run was starting.
	ErrChanged = errors.New("the command changed since it was confirmed")

	// ErrAsksFirst is returned by Run again for one of the user's scripts set to ask
	// before each run, which only the page asks.
	ErrAsksFirst = errors.New("the script asks before each run")

	// ErrCantPull is returned for a pull refused before it ran: no status was worked out
	// for the clone, it is on another branch than the one its status was worked out on,
	// its branch follows none on origin, git is in the middle of something there, or
	// gitread's guards refused it, as for a clone whose own config holds a setting outside
	// their allowlist.
	ErrCantPull = errors.New("the branch can't be pulled now")
)
View Source
var ErrInvalidScript = errors.New("invalid script")

ErrInvalidScript is returned by Save for a script whose fields aren't one.

Functions

func Restore added in v0.3.0

func Restore(ctx context.Context, db *sql.DB, clk clock.Clock, setAside string) (int, error)

Restore brings back the user's own scripts, which the app lost with a database it set aside at startup (setAside, "" for none): a reset the user asked for, or a database it could not use. It runs only while db holds no script of the user's, reads the database set aside without changing it, whichever build wrote it, and stores every script in it in its order, each checked as Save checks it: one Save would refuse is left out. It stores them all in one transaction, or none, and returns how many it stored. A database set aside that holds no scripts, as one an older build wrote, brings back none.

func Variables

func Variables() []string

Variables names the variables each of the user's scripts runs with: the clone's repository (owner/name, or "" when its origin isn't on GitHub), its folder, and its repository's default branch (or "" when the app can't tell), so a command never has to quote a path.

Types

type BackupPart

type BackupPart struct {
	// contains filtered or unexported fields
}

BackupPart is the user's own scripts' part of a backup of the user's setup: the scripts in portable form, and what importing them does. Core adapts it to a backup.Part; it is a type of its own, apart from the Service, so the window, which binds the Service, never reaches it.

func NewBackupPart

func NewBackupPart(s *Service) *BackupPart

NewBackupPart returns the backup's part over s.

func (*BackupPart) ApplyBackup

func (bp *BackupPart) ApplyBackup(ctx context.Context, tx *sql.Tx, plan ScriptsPlan) (backup.Count, error)

ApplyBackup stores plan's scripts inside tx, an import's transaction, and returns what it changed. Merge appends each script not identical to one here, in order; Replace deletes each script here not identical to one of the file's, adds the rest, and puts them all in the file's order.

func (*BackupPart) PlanBackup

func (bp *BackupPart) PlanBackup(ctx context.Context, in PortableScripts, mode backup.Mode) (ScriptsPlan, error)

PlanBackup works out what importing in, a backup's scripts, does under mode, changing nothing. Each is checked as Save checks it, and one Save would refuse is left out, with a word on why. Merge skips a script identical to one here and appends the others in the file's order; Replace makes the scripts exactly the file's, in its order, keeping each one here identical to one of them as it is.

func (*BackupPart) PortableScripts

func (bp *BackupPart) PortableScripts(ctx context.Context) (PortableScripts, error)

PortableScripts returns the user's own scripts in portable form, in their order.

type BranchGit

type BranchGit interface {
	CurrentBranch(ctx context.Context, dir string) (string, error)
	Upstream(ctx context.Context, dir, branch string) (gitread.Upstream, error)
	InProgress(ctx context.Context, dir string) (gitread.Operation, error)
	LocalBranch(ctx context.Context, dir, branch string) (string, error)
	TrackingBranch(ctx context.Context, dir, branch string) (string, error)
	HasCommit(ctx context.Context, dir, oid string) (bool, error)
	IsAncestor(ctx context.Context, dir, oid, of string) (bool, error)
	CommitsBetween(ctx context.Context, dir, from, to string) (int, error)
	RemoteBranch(ctx context.Context, dir, branch string) (string, error)
	IndexLocked(ctx context.Context, dir string) (bool, error)
	RemoteSetup(ctx context.Context, dir string) (gitread.Setup, error)
	CloneConfig(ctx context.Context, dir string) error
}

BranchGit reads what a clone's branch status and its pull need, through gitread's allowlist: the branch the clone is on, what it follows, what git is in the middle of, whether its index is locked, its commits and origin's branch as the clone keeps them, and origin's one branch the clone's follows, which RemoteBranch asks origin for with one ls-remote of that branch alone, every one of gitread's guards applied; RemoteSetup works out what the pull runs with under those same guards, gitread's own git included, and CloneConfig checks the clone's own config as it does: *gitread.Git.

type BranchState

type BranchState string

BranchState is where a clone's branch stands against the branch it follows on origin.

const (
	// BranchUpToDate is a branch at origin's commit.
	BranchUpToDate BranchState = "upToDate"

	// BranchBehind is a branch origin has commits for, with none of its own: a pull
	// fast-forwards it. A commit of origin's the clone hasn't fetched counts as behind
	// too, since only the pull can tell; it refuses, fast-forward only, a branch that
	// turns out to have commits of its own.
	BranchBehind BranchState = "behind"

	// BranchAhead is a branch with commits origin doesn't have, and none to bring.
	BranchAhead BranchState = "ahead"

	// BranchDiverged is a branch with commits of its own while origin has others: no
	// fast-forward brings it level.
	BranchDiverged BranchState = "diverged"

	// BranchDetached is a clone whose HEAD is detached, on no branch.
	BranchDetached BranchState = "detached"

	// BranchNoUpstream is a branch that follows no branch on origin: none at all, another
	// remote's, or one origin no longer has.
	BranchNoUpstream BranchState = "noUpstream"

	// BranchInProgress is a clone git is in the middle of a merge, a rebase, a
	// cherry-pick, or a revert in.
	BranchInProgress BranchState = "inProgress"

	// BranchUnknown is a status that couldn't be worked out: origin didn't answer, or the
	// clone couldn't be read.
	BranchUnknown BranchState = "unknown"

	// BranchOff is no status at all: the Scripts integration is off, so neither the clone
	// nor origin is read, and nothing is pulled.
	BranchOff BranchState = "off"

	// BranchRefused is a clone a pull would be refused in before git ran, as its own git
	// config holds a setting gitread's guards refuse (gitread's CloneConfig), read from the
	// clone alone: origin isn't asked, and the note says which settings, and where to set
	// them instead.
	BranchRefused BranchState = "refused"

	// BranchNotAsked is a clone whose origin isn't on GitHub, which is asked only when the
	// user asks (refresh): the app reaches nothing but GitHub unless the user clicks. An
	// answer the user asked for is served while it is kept.
	BranchNotAsked BranchState = "notAsked"
)

The states of a clone's branch.

type BranchStatus

type BranchStatus struct {
	// Dir is the clone, with ~; Branch the branch it is on, or "" when HEAD is detached;
	// and Upstream origin's branch it follows, or "" when it follows none there.
	Dir      string `json:"dir"`
	Branch   string `json:"branch"`
	Upstream string `json:"upstream"`

	State BranchState `json:"state"`

	// Local and Origin are the branch's commit and origin's, shortened, or "" when
	// unknown.
	Local  string `json:"local"`
	Origin string `json:"origin"`

	// The note says where the branch stands in a sentence, and for one no pull is
	// offered on, why.
	Note string `json:"note"`

	// CheckedAt is when origin was asked, as RFC 3339, or "" when it wasn't.
	CheckedAt string `json:"checkedAt"`
}

BranchStatus is a clone's branch against origin's, for Repo scripts' header: whether a pull would bring it newer commits. Only a branch that is behind is offered one.

type Clone

type Clone struct {
	Name string `json:"name"`
	Dir  string `json:"dir"`
}

Clone is a clone in the repository locations, as the page's picker lists it: its folder's name, with its parent folder after it when another clone shares the name, and its folder, with ~, which is the clone's key.

type Clones

type Clones interface {
	Roots(ctx context.Context) (localrepos.Roots, error)
	Dirs(ctx context.Context, roots localrepos.Roots) ([]string, error)
	Origin(ctx context.Context, dir string) (name repo.Name, onGitHub bool, err error)
}

Clones finds the clones in the repository locations: Roots is the clone root and the other locations as set now, Dirs lists the clones in them, reading no git (localrepos.Roots.Clones), and Origin reads which GitHub repository a clone's origin is, if any. Core adapts the settings and localrepos.

type Config

type Config struct {
	Clones    Clones
	Git       Git
	BranchGit BranchGit
	Branches  DefaultBranches
	Tools     ToolSource
	Settings  Settings
	Runs      Runs
	History   RunHistory

	// DB holds script_trust and user_scripts.
	DB *sql.DB

	// Env returns the login shell's environment, and whether it was read
	// (shellenv.Reader.Env): runs get it, and ${env:…} reads it. It defaults to the app's
	// own. Shell returns the user's login shell, which runs a shell command
	// (shellenv.Reader.Shell); it defaults to $SHELL, or /bin/sh.
	Env   func() ([]string, bool)
	Shell func() string

	// Runner returns what runs magex's help:commands with env, in the folder dir: a
	// scratch folder of the app's own, made under Scratch ("" is the system's temporary
	// folder). It defaults to an execpath.Exec with env and dir.
	Runner  func(env []string, dir string) execpath.Runner
	Scratch string

	Home   string
	Clock  clock.Clock
	Logger *slog.Logger
}

Config holds a Service's dependencies. Clones is required; a run also needs Runs, trust the database, and a branch's status and its pull BranchGit.

type DefaultBranches

type DefaultBranches interface {
	DefaultBranch(ctx context.Context, name repo.Name) (string, error)
}

DefaultBranches reads the default branch the app last stored for a repository, "" when it stored none. Core adapts the inbox's store.

type Direction

type Direction string

Direction is the way Move moves a script in the order.

const (
	MoveUp   Direction = "up"
	MoveDown Direction = "down"
)

The directions.

type Entry

type Entry struct {
	// Ref names it within the clone, for a run: its source and its name.
	Ref    string `json:"ref"`
	Source Source `json:"source"`

	// Name is its name, Command what it runs, with ${…} as written and a task's
	// dependencies before it, Icon t3.json's icon name, and Detail a task's detail or a
	// target's description.
	Name    string `json:"name"`
	Command string `json:"command"`
	Icon    string `json:"icon"`
	Detail  string `json:"detail"`

	// Custom reports a magex target from the repository's own magefiles, and Args one
	// that takes key=value arguments.
	Custom bool `json:"custom"`
	Args   bool `json:"args"`

	// Runnable reports a script the app can run; Why says, when it can't, why it is
	// greyed.
	Runnable bool   `json:"runnable"`
	Why      string `json:"why"`

	// Release reports a command that bumps a version: Release does that with its
	// checks, and the page points there; running it anyway takes RunRequest.Anyway.
	Release bool `json:"release"`

	// Background reports a task that runs until stopped, and Note what the page says of
	// how it runs, as for dependencies VS Code runs side by side.
	Background bool   `json:"background"`
	Note       string `json:"note"`

	// Trust is what running it asks first; Trusted is the command trusted before, for a
	// changed one, and TrustNote what the question adds, as for a magex target, that it
	// runs the repository's own code. SHA256 is the hash of what the page shows, which a
	// run the user confirmed sends back.
	Trust     Trust  `json:"trust"`
	Trusted   string `json:"trusted"`
	TrustNote string `json:"trustNote"`
	SHA256    string `json:"sha256"`

	// Inputs are the values a task asks for, which a small form fills in before it runs.
	Inputs []Input `json:"inputs"`

	// Mine is the user's own script, as saved, for its menu's Edit and the question Ask
	// before running asks; nil for every other source's.
	Mine *UserScript `json:"mine"`
}

Entry is one script as the page lists it.

type Git

type Git interface {
	CurrentBranch(ctx context.Context, dir string) (string, error)
	OriginHead(ctx context.Context, dir string) (string, error)
}

Git reads which branch a clone is on, and origin's default branch as the clone last learned it, from its own refs and never asking origin: *gitread.Git.

type Icon

type Icon string

Icon is what one of the user's scripts shows beside its name: t3.json's icon names.

const (
	IconPlay      Icon = "play"
	IconTest      Icon = "test"
	IconLint      Icon = "lint"
	IconBuild     Icon = "build"
	IconConfigure Icon = "configure"
)

The icons.

func (Icon) Valid

func (i Icon) Valid() bool

Valid reports whether i is one of the icons.

type Input

type Input struct {
	ID          string    `json:"id"`
	Kind        InputKind `json:"kind"`
	Description string    `json:"description"`
	Default     string    `json:"default"`

	// Options are a pickString's choices; empty for a promptString.
	Options []Option `json:"options"`
}

Input is one value a task asks for: VS Code's pickString or promptString.

type InputKind

type InputKind string

InputKind is the kind of a task's input.

const (
	// InputPick picks one of a list of values.
	InputPick InputKind = "pickString"

	// InputPrompt is a line of text the user types.
	InputPrompt InputKind = "promptString"
)

The input kinds tasks.json can use.

type Intent

type Intent struct {
	Ref    string            `json:"ref"`
	Inputs map[string]string `json:"inputs"`
	Args   []string          `json:"args"`
	Anyway bool              `json:"anyway"`
	SHA256 string            `json:"sha256"`
}

Intent is what a script run sets out to do, written into its run's result as it starts, so Run again works it out again: the script, the values and arguments it ran with, and the hash of the command that ran, "" for one that needs no trust.

type Option

type Option struct {
	Label string `json:"label"`
	Value string `json:"value"`
	Shown string `json:"shown"`
}

Option is one choice of a pickString: its label, the value it fills in, and Shown, what the form shows for it, the label with the value beside it whenever they differ, since the value is what runs.

type Page

type Page struct {
	// Repo is the clone's repository on GitHub, "owner/name", or "" for a folder whose
	// origin isn't on GitHub, where trust is kept by repository, so a repository command
	// asks every time and the question offers no "Don't ask again"; Name is what the page
	// calls it, and Dir its folder, with ~, which every request names it by.
	Repo string `json:"repo"`
	Name string `json:"name"`
	Dir  string `json:"dir"`

	// Branch is the branch the clone is on, or "" when HEAD is detached or unread, and
	// FromBranch that branch when it isn't the repository's default, since a branch can
	// change what the clone's files say: a repository command's question names it.
	Branch     string `json:"branch"`
	FromBranch string `json:"fromBranch"`

	// Switches are the settings' sources, as set now.
	Switches integrationprefs.ScriptSources `json:"switches"`

	// Variables names the variables each of the user's own scripts runs with (Variables),
	// for the editor to name.
	Variables []string `json:"variables"`

	// Off reports the Scripts integration off, and Why says why; the page then has no
	// sections, and nothing runs.
	Off bool   `json:"off"`
	Why string `json:"why"`

	// Sections are the sources: the user's own scripts, t3.json, tasks.json, and magex.
	Sections []Section `json:"sections"`
}

Page is Repo scripts for one clone: its sources in the order the page lists them.

type PortableScript

type PortableScript struct {
	Name    string                  `json:"name"`
	Command string                  `json:"command"`
	Icon    Icon                    `json:"icon"`
	Scope   Scope                   `json:"scope"`
	Repos   []string                `json:"repos"`
	RunsIn  integrationprefs.RunsIn `json:"runsIn"`
	Confirm bool                    `json:"confirm"`
}

PortableScript is one of the user's own scripts in portable form, as a backup holds it: everything Save stores but its ID.

type PortableScripts

type PortableScripts struct {
	Scripts []PortableScript `json:"scripts"`
}

PortableScripts is the user's own scripts in portable form, in their order.

type PullIntent

type PullIntent struct {
	Branch   string `json:"branch"`
	Upstream string `json:"upstream,omitempty"`
	Before   string `json:"before,omitempty"`
}

PullIntent is what a pull sets out to do, written into its run's result as it starts, so Run again pulls the same branch, the one the clone was on, and a pull followed again after a restart (Verifier) is worded as it would have been: the branch, and what the pull's Before read under the clone's lock, the branch on origin it follows and the branch's commit then.

type Question

type Question struct {
	// Ref, Name, and Source name the script.
	Ref    string `json:"ref"`
	Name   string `json:"name"`
	Source Source `json:"source"`

	// Command is what will run, the chosen inputs filled in; the user's environment's
	// values are never shown.
	Command string `json:"command"`

	// Trust is why it asks, ask or changed; Trusted is the command trusted before, and
	// TrustNote what the question adds, as on the page's entry.
	Trust     Trust  `json:"trust"`
	Trusted   string `json:"trusted"`
	TrustNote string `json:"trustNote"`

	// SHA256 is the hash the user confirms, which a run sends back as Confirmed.
	SHA256 string `json:"sha256"`
}

Question is what the window asks before a repository command runs, sent with the error that refused it (QuestionError): the command as it will run, with the chosen inputs filled in, and what the user trusted before.

type QuestionError

type QuestionError struct {
	Question Question
	// contains filtered or unexported fields
}

QuestionError refuses a run until the user answers its Question. Its message says why, and Detail hands the question to the desktop runtime, which sends it to the window.

func (*QuestionError) Detail

func (e *QuestionError) Detail() any

Detail returns the question, for the window.

func (*QuestionError) Error

func (e *QuestionError) Error() string

Error returns the refusal's text.

func (*QuestionError) Unwrap

func (e *QuestionError) Unwrap() error

Unwrap returns the refusal: ErrNeedsTrust or ErrChanged, with the words for the user.

type Rerunner

type Rerunner struct {
	// contains filtered or unexported fields
}

Rerunner works Run again out for script runs (scriptrun.Rerunner): the same script, with the same values and arguments, read again from its source, in the same clone. It asks nothing, so it runs a repository command only while the user trusts it as it is now, by a record kept under the clone's current origin; one confirmed once, or changed since, is refused, and the user runs it from the page, which asks. One of the user's own scripts runs as it is saved now, found by its ID, and is refused once deleted, and while it is set to ask before each run, which only the page asks. A pull pulls the same branch again, its Before checking everything afresh, and is refused once the clone is on another. Core registers it with the runner for scriptrun.KindScript and scriptrun.KindPull. It is its own type, not a method of the Service, which the window binds.

func NewRerunner

func NewRerunner(s *Service) Rerunner

NewRerunner returns the Rerunner of s's runs.

func (Rerunner) Rerun

func (r Rerunner) Rerun(ctx context.Context, run scriptrun.Record) (scriptrun.Spec, error)

Rerun returns the spec of run again, where it ran.

type RunHistory

type RunHistory interface {
	Runs(ctx context.Context, filter scriptrun.Filter) ([]scriptrun.Run, error)
}

RunHistory reads runs back from the runner's history: the scriptrun Service.

type RunRequest

type RunRequest struct {
	// Dir is the clone, as the page lists it (Page.Dir), with ~, and Ref the script
	// (Entry.Ref).
	Dir string `json:"dir"`
	Ref string `json:"ref"`

	// Inputs are the values the form filled in, by input ID; one left out takes its
	// default. Args are a magex target's key=value arguments.
	Inputs map[string]string `json:"inputs"`
	Args   []string          `json:"args"`

	// RunsIn is where it runs, or "" for where the script says, as one of the user's own
	// may, or else the setting.
	RunsIn integrationprefs.RunsIn `json:"runsIn"`

	// Confirmed is the SHA256 of the command the user confirmed in the trust question,
	// as the page showed it (Entry.SHA256), or "" when none was asked; Remember keeps it
	// trusted, the question's "Don't ask again".
	Confirmed string `json:"confirmed"`
	Remember  bool   `json:"remember"`

	// Anyway runs a command that bumps a version, which the page points to Release for.
	Anyway bool `json:"anyway"`
}

RunRequest is a script the window asks to run in one clone.

type Runs

type Runs interface {
	Start(ctx context.Context, spec scriptrun.Spec) (int64, error)
}

Runs starts a run: the scriptrun engine.

type Scope

type Scope string

Scope is which clones one of the user's own scripts shows in.

const (
	// ScopeAll shows the script in every clone.
	ScopeAll Scope = "all"

	// ScopeGo shows it in a clone with a go.mod at its root.
	ScopeGo Scope = "go"

	// ScopeRepos shows it in the clones of the repositories it names.
	ScopeRepos Scope = "repos"
)

The scopes.

func (Scope) Valid

func (s Scope) Valid() bool

Valid reports whether s is one of the scopes.

type ScriptsPlan

type ScriptsPlan struct {
	backup.Plan
	// contains filtered or unexported fields
}

ScriptsPlan is what importing a backup's scripts does, as PlanBackup works it out.

type Section

type Section struct {
	// Source is the source, and Title what the page heads it with.
	Source Source `json:"source"`
	Title  string `json:"title"`

	// Off reports a source turned off in Settings → Integrations, and Found one the
	// clone has: its file, or for magex a copy the app can run.
	Off   bool `json:"off"`
	Found bool `json:"found"`

	// Error says why the source couldn't be read, and Note what the page says under its
	// heading; both may be "".
	Error string `json:"error"`
	Note  string `json:"note"`

	// Entries are its scripts, in the file's order; magex's are the repository's own
	// targets, then the built-ins.
	Entries []Entry `json:"entries"`

	// Common are the refs of magex's common targets, for the page's chips, in their
	// order.
	Common []string `json:"common"`
}

Section is one source's scripts.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service lists each clone's scripts and runs them, asking first for a command from the repository's own files. The window binds it.

func NewService

func NewService(cfg Config) *Service

NewService returns a Service over cfg.

func (*Service) BranchStatus

func (s *Service) BranchStatus(ctx context.Context, dir string, refresh bool) (BranchStatus, error)

BranchStatus returns where the clone at dir, as the picker lists it, with ~, stands against origin: whether its branch is behind the branch it follows on origin, up to date, ahead, or diverged, or why there is nothing to pull. It reads the clone with gitread every time, and asks origin for the branch followed alone with one ls-remote, which it keeps for OriginKept: within it, nothing asks origin again unless refresh is set. It never fetches. A clone that can't be read, or an origin that doesn't answer, is a status that says why, not an error, and a clone whose own config a pull would refuse says so (BranchRefused), asking origin nothing. An origin that isn't on GitHub is asked only with refresh, the user's click, and is otherwise BranchNotAsked unless an answer the user asked for is kept: unasked, the app reaches GitHub alone. While the Scripts integration is off, it reads nothing and says so (BranchOff).

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id int64) error

Delete deletes the user's script id. The runs it made stay in the history.

func (*Service) Duplicate

func (s *Service) Duplicate(ctx context.Context, id int64) (UserScript, error)

Duplicate saves a copy of the user's script id, called "<name> copy", right after it in the order, and returns the copy.

func (*Service) List

func (s *Service) List(ctx context.Context, dir string, refresh bool) (Page, error)

List returns Repo scripts for the clone at dir, as the picker lists it, with ~: its sources in the page's order, as the settings turn them on, each script with what running it asks first, or the page marked off while the scripts are. The user's own scripts are the ones whose scope includes the clone: every clone, one with a go.mod at its root, or the clones of the repositories they chose, by its origin, in any case. It runs nothing in the clone: its files are read, and magex's built-ins are listed in a folder of the app's own, once per copy of magex, a list that failed included, until refresh.

func (*Service) Move

func (s *Service) Move(ctx context.Context, id int64, way Direction, dir string) error

Move moves the user's script id one place up or down their order, as the clone at dir, with ~, lists them: past the nearest script that way that shows there, so the move shows on the page it was made on, or, with dir "", past its neighbor. A script already first or last there stays where it is.

func (*Service) Pull

func (s *Service) Pull(ctx context.Context, dir string) (int64, error)

Pull pulls the branch the clone at dir, as the picker lists it, with ~, was on when its BranchStatus was last worked out, from the branch it follows on origin, and returns the run's ID. It runs in the app, in the clone, under the clone's lock, which the runner takes and a busy clone fails at once. The run's Before reads the clone again under the lock and refuses a clone on another branch, one whose branch follows none on origin, and one git is in the middle of something in; then it works out what git runs with (gitread's RemoteSetup), every one of gitread's guards applied, before anything runs, asking origin nothing: the pull's own fetch is the one time it is asked. git's pull is fast forward only: it never merges or rebases, and a branch with commits of its own, or one origin no longer has, fails with git's own message. Stop is held back for the whole run, and so is a quit, which leaves it going for the next start. Its Finish reads the branch again, and words what the pull did. Like a script, it is refused while the Scripts integration is off.

func (*Service) RepoNames

func (s *Service) RepoNames(ctx context.Context) ([]string, error)

RepoNames returns the GitHub repositories of the clones in the repository locations, owner/name, each once, sorted: what a script for chosen repositories can choose from, and, through core, the launch defaults' targets (the Pinned and muted field and Add defaults). Each clone's origin is read with git, a few at a time, and kept until the clone's .git/config changes, so the editor opens at once after the first time; a clone the listing no longer holds is forgotten. Once the caller has gone (ctx is done, as when the window cancels the call), no more origins are read.

func (*Service) Repos

func (s *Service) Repos(ctx context.Context) ([]Clone, error)

Repos returns the clones in the repository locations, for the page's picker: the ones a script ran in most recently first, then the rest by name. Two clones of the same name, ignoring case, in different locations are told apart by their folder: "example-repo (~/work)".

func (*Service) Run

func (s *Service) Run(ctx context.Context, req RunRequest) (int64, error)

Run runs the script req names in its clone, and returns the run's ID. A command from the repository's files runs only as the user confirmed it, or as they trusted it before: the run's Before, under the clone's lock, reads its source again and refuses a command whose hash isn't that one.

func (*Service) Save

func (s *Service) Save(ctx context.Context, script UserScript) (UserScript, error)

Save saves one of the user's own scripts as the editor filled it in: a new one, at the end of the order, when its ID is 0, else in place of the one with its ID. Its name and command are trimmed and must be there, on one line; its scope, place, and icon must be ones there are; and a script for chosen repositories needs one, each read as owner/name and kept once, in any case. It returns the script as saved.

type Settings

type Settings interface {
	Integrations(ctx context.Context) (integrationprefs.Config, error)
}

Settings reads the integrations' settings: which sources Repo scripts lists, and where runs go. Core adapts the settings.

type Source

type Source string

Source is where a script comes from.

const (
	// SourceYours is the user's own scripts, saved in the app.
	SourceYours Source = "yours"

	// SourceT3 is the repository's t3.json.
	SourceT3 Source = "t3"

	// SourceTasks is the repository's .vscode/tasks.json.
	SourceTasks Source = "tasks"

	// SourceMagex is magex's targets: its built-ins and the repository's own.
	SourceMagex Source = "magex"
)

The sources, in the order the page lists them.

func Sources

func Sources() []Source

Sources returns every source, in the order the page lists them.

func (Source) Valid

func (s Source) Valid() bool

Valid reports whether s is one of Sources.

type ToolFacts

type ToolFacts struct {
	integrations.CopyFacts
}

ToolFacts is what Repo scripts knows of magex: the copy in use, and whether it can run (integrations.CopyFacts).

func ToolFactsOf

func ToolFactsOf(tool integrations.Tool) ToolFacts

ToolFactsOf reads magex's facts from what the settings show of it.

type ToolSource

type ToolSource interface {
	Tools(ctx context.Context) Tools
}

ToolSource reads what the app knows of the tools: magex, and whether mage-x and go-pre-commit are on. Core adapts the integrations service, with ToolsOf. It never fails: a tool it can't read is missing, and off.

type Tools

type Tools struct {
	Magex         ToolFacts
	MageXOn       bool
	GoPreCommitOn bool
}

Tools is what Repo scripts knows of the integrations' tools: magex, and whether mage-x and go-pre-commit are on, either of which turns the scripts on while their switch is automatic.

func ToolsOf

func ToolsOf(mageX, goPreCommit integrations.Tool) Tools

ToolsOf reads the tools from what the settings show of mage-x and go-pre-commit.

type Trust

type Trust string

Trust is what running a script asks of the user first.

const (
	// TrustNone is a script that needs no trust: a built-in magex target, or the user's
	// own.
	TrustNone Trust = "none"

	// TrustTrusted is a repository command the user trusted as it is now.
	TrustTrusted Trust = "trusted"

	// TrustAsk is a repository command the user never trusted.
	TrustAsk Trust = "ask"

	// TrustChanged is a repository command that changed since the user trusted it.
	TrustChanged Trust = "changed"
)

The trust states.

type UserScript

type UserScript struct {
	// ID names it; Save creates one whose ID is 0.
	ID int64 `json:"id"`

	// Name is what the page calls it, and Command the shell command it runs in the
	// clone, as the user would type it in a terminal.
	Name    string `json:"name"`
	Command string `json:"command"`

	// Icon is what shows beside its name.
	Icon Icon `json:"icon"`

	// Scope is which clones it shows in, and Repos, for ScopeRepos, the repositories
	// whose clones those are, owner/name; empty for any other scope.
	Scope Scope    `json:"scope"`
	Repos []string `json:"repos"`

	// RunsIn is where it runs, unless a run asks for the other place, or "" for where the
	// setting sends runs ("As set in Settings").
	RunsIn integrationprefs.RunsIn `json:"runsIn"`

	// Confirm asks before each run. The window asks: it is the user's own setting, which
	// the service leaves to them.
	Confirm bool `json:"confirm"`
}

UserScript is one of the user's own scripts, saved in the app: a command they run from Repo scripts, in every clone its scope includes, needing no trust (D13).

type Verifier

type Verifier struct {
	// contains filtered or unexported fields
}

Verifier gives a pull from before the app restarted, followed again or found ended, what its Spec gave it, from the run's record alone (scriptrun.Verifier): Stop and a quit held back for its whole run, as git may still be writing the clone's files, and its Finish, which words what it did as its Spec's would, or, when its end wasn't seen, settles it from the branch (settleUnseen). Core registers it for scriptrun.KindPull.

func NewVerifier

func NewVerifier(s *Service) Verifier

NewVerifier returns the Verifier of s's pulls.

func (Verifier) Verify

Verify gives a pull from before the app restarted what its Spec gave it. A record whose intent can't be read still has Stop held back, and keeps the runner's own outcome.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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