file

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 16 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 file is the endpoint group for the files in the Object Store: what a deploy writes to a host.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is a file that is not there.
	ErrNotFound = errors.New("file not found")
	// ErrExists is a file that exists with other content.
	ErrExists = errors.New("file exists with other content")
	// ErrProtected is a file that osapi owns.
	ErrProtected = errors.New("file is protected")
	// ErrNoStore is a controller with no Object Store.
	ErrNoStore = errors.New("the Object Store is not configured")
	// ErrNoState is a controller with no file state KV.
	ErrNoState = errors.New("the file state KV is not configured")
)
View Source
var Group = endpoint.Register(endpoint.Define(
	"file",
	"Manage the files in the Object Store",
	endpoint.Area(area.ObjectStore),
	endpoint.SubDoc("stale", "Read the deployments whose file changed since"),
	endpoint.Title("File Management API"),
	endpoint.Tag("file_operations", "File", "Object Store file management operations."),
	endpoint.Uses[Deps](),
	endpoint.Get[ListInput, ListResponse]("list").
		Handle(List).
		ID("GetFiles").
		Describe("List stored files").
		Explain("List all files stored in the Object Store.").
		Returns("List of stored files.").
		Command("List stored files", "").
		Example("").
		Prints(sampleList).
		Example("--json").
		Errors(noStore503),
	endpoint.Post[UploadInput, UploadResponse]("upload").
		Fails("no name", UploadInput{File: content}).
		Fails("no file", UploadInput{Name: "app.conf"}).
		Fails("a name over 255 characters", UploadInput{Name: long, File: content}).
		Fails("a content type that is not raw or template", UploadInput{Name: "app.conf", ContentType: notAContentType, File: content}).
		Fails("an empty file", UploadInput{Name: "app.conf", File: []byte{}}).
		Handle(Upload).
		ID("PostFile").
		Describe("Upload a file").
		Explain("Upload a file to the Object Store.").
		Returns("The file is new.").
		Status(http.StatusCreated).
		Location("name").
		Answers(http.StatusOK, "The file exists with the same content. Nothing changed.").
		Errors(
			endpoint.Conflict(ErrExists,
				"The file exists with different content. Replace it with PUT /api/file/{name}."),
			endpoint.Forbidden(ErrProtected, "The file is an osapi file, which a caller cannot write."),
			noStore503,
		).
		Command("Upload a file to the Object Store",
			"An upload of the same content changes nothing (`Changed: false`). If the\n"+
				"file exists with different content, the command rejects the upload with 409\n"+
				"Conflict. `file update` replaces the file.").
		Example("--name app.conf --file /tmp/app.conf").
		Prints(sampleUpload).
		Check("client file get", "--name app.conf",
			"name", "app.conf", "sha256", "{out.sha256}", "size", "{out.size}", "content_type", "raw").
		Example("--name app.conf.tmpl --file /tmp/app.conf.tmpl --content-type template").
		Prints(sampleUploadTemplate).
		Check("client file get", "--name app.conf.tmpl",
			"name", "app.conf.tmpl", "sha256", "{out.sha256}", "content_type", "template"),
	endpoint.Get[GetInput, InfoResponse]("get").
		Fails("a name over 255 characters", GetInput{Name: long}).
		Handle(Get).
		ID("GetFileByName").
		Describe("Get file metadata").
		Explain("Get metadata for a specific file in the Object Store.").
		Returns("The metadata of the file.").
		Command("Get file metadata", "").
		Example("--name app.conf").
		Prints(sampleInfo).
		Example("--name app.conf --json").
		Errors(endpoint.NotFound(ErrNotFound, "File not found."), noStore503),
	endpoint.Put[UpdateInput, UploadResponse]("update").
		Fails("no file", UpdateInput{Name: "app.conf"}).
		Fails("a name over 255 characters", UpdateInput{Name: long, File: content}).
		Fails("a content type that is not raw or template", UpdateInput{Name: "app.conf", ContentType: notAContentType, File: content}).
		Fails("an empty file", UpdateInput{Name: "app.conf", File: []byte{}}).
		Handle(Update).
		ID("PutFileByName").
		Describe("Replace a file").
		Explain("Replace a file in the Object Store, or create it when the name is new.").
		Returns("The file is new.").
		Status(http.StatusCreated).
		Location("name").
		Answers(http.StatusOK, "The file existed. changed says whether its content changed.").
		Command("Replace a file in the Object Store",
			"The command replaces the content of a stored file, or stores it when the\n"+
				"name is new. The same content changes nothing (`Changed: false`).").
		Example("--name app.conf --file /tmp/app.conf").
		Prints(sampleUpload).
		Check("client file get", "--name app.conf",
			"name", "app.conf", "sha256", "{out.sha256}", "content_type", "raw").
		Errors(
			endpoint.Forbidden(ErrProtected, "The file is an osapi file, which a caller cannot write."),
			noStore503,
		),
	endpoint.Delete[DeleteInput, struct{}]("delete").
		Fails("a name over 255 characters", DeleteInput{Name: long}).
		Handle(Delete).
		ID("DeleteFileByName").
		Describe("Delete a file").
		Explain("Delete a file from the Object Store.").
		Returns("File deleted.").
		Status(http.StatusNoContent).
		Errors(
			endpoint.NotFound(ErrNotFound, "File not found."),
			endpoint.Forbidden(ErrProtected, "The file is an osapi file, which a caller cannot delete."),
			noStore503,
		).
		Command("Delete a file from the Object Store", "").
		Example("--name app.conf").
		CheckFails("client file get", "--name app.conf", "not found").
		Example("--name app.conf --json").
		CheckFails("client file get", "--name app.conf", "not found"),
	endpoint.Get[StaleInput, StaleDeploymentsResponse]("stale").
		Handle(Stale).
		Sub("stale").
		Use("list").
		ID("GetFileStale").
		Describe("List stale file deployments").
		Explain("Returns deployments where the source object in the Object Store has been "+
			"updated since the file was last deployed. Controller-only check that compares "+
			"state KV SHA-256 hashes against current object content.").
		Returns("The stale deployments.").
		Command("List stale file deployments",
			"The list shows the deployments whose object changed after the last\n"+
				"deploy. Those are the files that you must deploy again.").
		Example("").
		Prints(sampleStale).
		Example("--json").
		Errors(
			noStore503,
			endpoint.Unavailable(ErrNoState, "The Object Store or the file state KV is not configured."),
		),
))

Group is the file group's definition.

Functions

func Delete

func Delete(
	ctx context.Context,
	d Deps,
	in DeleteInput,
) (struct{}, error)

Delete removes a file.

Types

type DeleteInput

type DeleteInput struct {
	Name string `json:"name" in:"path" validate:"required,min=1,max=255" doc:"The name of the file in the Object Store."`
}

DeleteInput names the file to delete.

type Deps

type Deps struct {
	// Objects is the Object Store bucket of the files. Nil when the
	// controller has no object bucket; each method then answers 503.
	Objects Objects
	// States is the KV bucket of the deploy state. Nil when it is not set;
	// stale then answers 503.
	States States
}

Deps is what the file group's handlers use.

type GetInput

type GetInput struct {
	Name string `json:"name" in:"path" validate:"required,min=1,max=255" doc:"The name of the file in the Object Store."`
}

GetInput names the file to read.

type Info

type Info struct {
	Name        string            `json:"name"         doc:"The name of the file."                                           example:"nginx.conf"`
	Sha256      string            `` /* 164-byte string literal not displayed */
	Size        int               `` /* 179-byte string literal not displayed */
	ContentType types.ContentType `` /* 189-byte string literal not displayed */
	Source      string            `json:"source"       doc:"Source of the file: 'system' or 'user'."                         example:"user"`
	// contains filtered or unexported fields
}

Info is a stored file.

type InfoResponse

type InfoResponse struct {
	Name        string            `json:"name"         doc:"The name of the file."                                           example:"nginx.conf"`
	Sha256      string            `` /* 164-byte string literal not displayed */
	Size        int               `` /* 179-byte string literal not displayed */
	ContentType types.ContentType `json:"content_type" doc:"How the file should be treated during deploy (raw or template)." example:"raw"`
	// contains filtered or unexported fields
}

InfoResponse is the metadata of a file.

func Get

func Get(
	ctx context.Context,
	d Deps,
	in GetInput,
) (InfoResponse, error)

Get is the metadata of a file.

type ListInput

type ListInput struct{}

ListInput is the input of list, which has none.

type ListResponse

type ListResponse struct {
	Items      []Info `json:"items"       doc:"List of stored files."  empty:"Upload one with osapi client file upload."`
	TotalItems int    `json:"total_items" doc:"Total number of files."                                                   example:"5"`
	// contains filtered or unexported fields
}

ListResponse is each stored file.

func List

func List(
	ctx context.Context,
	d Deps,
	_ ListInput,
) (ListResponse, error)

List is each stored file.

type Objects

type Objects interface {
	Put(ctx context.Context, meta jetstream.ObjectMeta, r io.Reader) (*jetstream.ObjectInfo, error)
	GetBytes(ctx context.Context, name string) ([]byte, error)
	Info(ctx context.Context, name string) (*jetstream.ObjectInfo, error)
	Delete(ctx context.Context, name string) error
	List(ctx context.Context) ([]*jetstream.ObjectInfo, error)
}

Objects is the Object Store bucket of the files.

type StaleDeployment

type StaleDeployment struct {
	ObjectName  string `` /* 145-byte string literal not displayed */
	Hostname    string `json:"hostname"     doc:"Host where the file is deployed."                  example:"web-01"`
	Provider    string `json:"provider"     doc:"Provider that manages this deployment."            example:"service"`
	DeployedAt  string `` /* 161-byte string literal not displayed */
	Path        string `` /* 175-byte string literal not displayed */
	DeployedSha string `` /* 165-byte string literal not displayed */
	CurrentSha  string `` /* 192-byte string literal not displayed */
}

StaleDeployment is a deploy whose object changed since.

type StaleDeploymentsResponse

type StaleDeploymentsResponse struct {
	Items      []StaleDeployment `                          json:"items"       doc:"The stale deployments."             empty:"Every deployment is in sync."`
	TotalItems int               `                          json:"total_items" doc:"Total number of stale deployments."`
	// contains filtered or unexported fields
}

StaleDeploymentsResponse is each stale deploy.

func Stale

Stale is each deploy whose object changed after the deploy, or went: the state KV's SHA-256 of the object that an agent deployed against the object's now. A record that names no object, such as a sysctl file, is never stale, and a record without the object's SHA-256 is stale until the next deploy writes it.

type StaleInput

type StaleInput struct{}

StaleInput is the input of stale, which has none.

type States

type States interface {
	Keys(ctx context.Context) ([]string, error)
	Get(ctx context.Context, key string) (jetstream.KeyValueEntry, error)
}

States is the KV bucket of the deploy state: one entry for each file that an agent deployed.

type UpdateInput

type UpdateInput struct {
	Name        string            `json:"name"         in:"path" validate:"required,min=1,max=255" doc:"The name of the file in the Object Store."`
	ContentType types.ContentType `` /* 209-byte string literal not displayed */
	File        []byte            `json:"file"         in:"form" validate:"required,min=1"         doc:"The file content."`
}

UpdateInput is the new content of a file that the path names.

type UploadInput

type UploadInput struct {
	Name        string            `` /* 216-byte string literal not displayed */
	ContentType types.ContentType `` /* 230-byte string literal not displayed */
	File        []byte            `json:"file"         in:"form" validate:"required,min=1"         doc:"The file content."`
}

UploadInput is a new file: its name, how a deploy treats it, and its content, as a multipart form.

type UploadResponse

type UploadResponse struct {
	Name        string            `` /* 161-byte string literal not displayed */
	Sha256      string            `` /* 215-byte string literal not displayed */
	Size        int               `` /* 230-byte string literal not displayed */
	Changed     bool              `` /* 155-byte string literal not displayed */
	ContentType types.ContentType `` /* 154-byte string literal not displayed */
	// contains filtered or unexported fields
}

UploadResponse is the file that an upload or an update wrote.

func Update

func Update(
	ctx context.Context,
	d Deps,
	in UpdateInput,
) (UploadResponse, error)

Update replaces a file, or creates it when the name is new. The same content answers 200 and writes nothing.

func Upload

func Upload(
	ctx context.Context,
	d Deps,
	in UploadInput,
) (UploadResponse, error)

Upload creates a file. A name that exists with the same content answers 200 and writes nothing; with other content it is ErrExists, and update replaces it.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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