Documentation
¶
Index ¶
- Constants
- Variables
- func CacheKey(argv []string, keyEnv []string) string
- func Run(ctx context.Context) error
- func SocketPath(path string) (string, error)
- type CLI
- type Cache
- type Chunk
- type Client
- func (c *Client) Clear() (int, error)
- func (c *Client) Close() error
- func (c *Client) Get(key string, expires time.Duration) ([]Chunk, bool, error)
- func (c *Client) Set(key, label string, chunks []Chunk, expires time.Duration) (bool, error)
- func (c *Client) Status() (*Status, error)
- func (c *Client) Stop() error
- type DaemonCmd
- type ExecCmd
- type ExitError
- type Request
- type Response
- type Server
- type Status
- type StatusItem
- type Stream
Constants ¶
const ( OpHello = "hello" OpGet = "get" OpSet = "set" OpStatus = "status" OpClear = "clear" OpStop = "stop" )
Operations of the protocol between the client and the daemon. Messages are newline delimited JSON over a unix domain socket.
const ( DefaultIdleTimeout = 10 * time.Minute DefaultMaxTotalSize = 256 << 20 )
Variables ¶
var ErrAlreadyRunning = errors.New("daemon is already running")
ErrAlreadyRunning is returned by Server.Run when another daemon holds the lock for the same socket.
var Version = "v0.0.1"
Functions ¶
func CacheKey ¶
CacheKey returns the cache key for a command line. The values of the environment variables named in keyEnv are part of the key; the working directory and other environment variables are not.
func SocketPath ¶
SocketPath resolves the socket path. When path is empty, the default path in a private per-user directory is used.
Types ¶
type CLI ¶
type CLI struct {
Socket string `help:"Path to the unix domain socket of the daemon." env:"CMDC_SOCKET" placeholder:"PATH"`
Debug bool `help:"Enable debug log." env:"CMDC_DEBUG"`
Version kong.VersionFlag `help:"Show version."`
Exec ExecCmd `cmd:"" help:"Run a command, or print its cached output."`
Status struct{} `cmd:"" help:"Show the status of the daemon."`
Clear struct{} `cmd:"" help:"Remove all cached outputs."`
Stop struct{} `cmd:"" help:"Stop the daemon."`
Daemon DaemonCmd `cmd:"" hidden:"" help:"Run the daemon in the foreground."`
}
CLI defines the command line interface.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is an in-memory store of command outputs.
func (*Cache) Get ¶
Get returns the chunks stored for key if the entry is younger than both expires and the expiration given when it was stored.
func (*Cache) Set ¶
Set stores chunks for key. It reports false if the entry is not stored because it does not fit in the cache. When the cache is full, the oldest entries are evicted.
func (*Cache) Stats ¶
func (c *Cache) Stats() ([]StatusItem, int64)
Stats returns the unexpired entries, oldest first, and their total size.
type Chunk ¶
Chunk is a piece of command output. A cached result is a sequence of chunks so that stdout and stderr can be replayed in the recorded order.
type Client ¶
Client talks to the daemon over a unix domain socket.
func DialOrStart ¶
DialOrStart connects to the daemon, starting it if it is not running. A daemon of a different version is replaced with the current one.
type DaemonCmd ¶
type DaemonCmd struct {
IdleTimeout time.Duration `help:"Exit after this duration without cached outputs and connections." env:"CMDC_IDLE_TIMEOUT" default:"10m"`
MaxTotalSize int64 `help:"Maximum total size in bytes of cached outputs." env:"CMDC_MAX_TOTAL_SIZE" default:"268435456"`
}
DaemonCmd is the options of the daemon subcommand.
type ExecCmd ¶
type ExecCmd struct {
Expires time.Duration `help:"How long the output is cached (e.g. 30s, 5m, 1h)." short:"e" required:"" env:"CMDC_EXPIRES"`
KeyEnv []string `help:"Names of environment variables whose values are part of the cache key." env:"CMDC_KEY_ENV" placeholder:"NAME"`
MaxSize int64 `help:"Maximum size in bytes of an output to cache." env:"CMDC_MAX_SIZE" default:"10485760"`
Command []string `arg:"" passthrough:"" help:"Command and its arguments."`
}
ExecCmd is the options of the exec subcommand.
type ExitError ¶
ExitError tells the main function to exit with Code. Err is reported to the user if it is not nil.
type Request ¶
type Request struct {
Op string `json:"op"`
Key string `json:"key,omitempty"`
Label string `json:"label,omitempty"`
Expires time.Duration `json:"expires,omitempty"`
Chunks []Chunk `json:"chunks,omitempty"`
}
Request is a message from the client to the daemon.
type Response ¶
type Response struct {
Error string `json:"error,omitempty"`
Version string `json:"version,omitempty"`
PID int `json:"pid,omitempty"`
Hit bool `json:"hit,omitempty"`
Stored bool `json:"stored,omitempty"`
Cleared int `json:"cleared,omitempty"`
Chunks []Chunk `json:"chunks,omitempty"`
Status *Status `json:"status,omitempty"`
}
Response is a message from the daemon to the client.
type Server ¶
type Server struct {
Socket string
// IdleTimeout is the duration after which the server exits when it
// has no cached entries and no connections.
IdleTimeout time.Duration
MaxTotalSize int64
SweepPeriod time.Duration
// contains filtered or unexported fields
}
Server is the resident process which keeps cached outputs in memory. It never executes commands; it only stores what clients send.
type Status ¶
type Status struct {
Running bool `json:"running"`
Socket string `json:"socket"`
PID int `json:"pid,omitempty"`
Version string `json:"version,omitempty"`
Uptime string `json:"uptime,omitempty"`
Entries int `json:"entries"`
TotalSize int64 `json:"total_size"`
Items []StatusItem `json:"items,omitempty"`
}
Status describes the state of the daemon.