{"modulePath":"github.com/CaliLuke/loom","version":"v1.12.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cli\n\nPackage cli contains helpers used by transport-specific command-line client generators for parsing the command-line flags to identify the service and the method to make a request along with the request payload to be sent.\n\n## Functions\n\n```go\nfunc CarrierFieldLoadCode(f *FlagData, argName, carrier, carrierTypeRef, validate, convert string, defaultValue any, payloadRef string) *jen.Statement\n```\nCarrierFieldLoadCode returns the code that decodes the JSON flag f into the detached carrier variable carrier of type carrierTypeRef, validates it and runs convert, which assigns the payload field argument argName. Errors name argName like FieldLoadCode. The code requires an \"err\" variable.\n\n```go\nfunc CommandUsage(data *CommandData) codegen.Section\n```\nCommandUsage builds the section that can be used to generate the endpoint command usage code.\n\n```go\nfunc FieldLoadCode(f *FlagData, argName, argTypeName, validate string, defaultValue any, payloadRef string) (*jen.Statement, bool)\n```\nFieldLoadCode returns the code used in the build payload function that initializes one of the payload object fields. It returns the initialization code and a boolean indicating whether the code requires an \"err\" variable. payloadRef is the declared builder result type; failed conversion or validation returns its zero value, independently of the payload's schema shape.\n\n```go\nfunc FlagsCode(data []*CommandData) string\n```\nFlagsCode returns a string containing the code that parses the command-line flags to infer the command (service), sub-command (method), and the arguments (method payload) invoked by the tool. It panics if any error occurs during the generation of flag parsing code.\n\n```go\nfunc FlagsCodeStatement(data []*CommandData) *jen.Statement\n```\nFlagsCodeStatement builds the CLI flag parsing statements.\n\n```go\nfunc IsJSONFlagType(typeName string) bool\n```\nIsJSONFlagType reports whether a CLI flag with the given Go type name is decoded as JSON. Primitive byte flags intentionally use their existing raw text codec.\n\n```go\nfunc PayloadBuilderSection(buildFunction *BuildFunctionData, pkgs *codegen.NameScope) codegen.Section\n```\nPayloadBuilderSection builds the section that can be used to generate the payload builder code. pkgs names the struct:pkg:path packages of the payload field types, see codegen.NameScope.PackageName.\n\n```go\nfunc UsageCommands(data []*CommandData) codegen.Section\n```\nUsageCommands builds a section that generates a help text showing the list of allowed commands and sub-commands.\n\n```go\nfunc UsageExamples(data []*CommandData) codegen.Section\n```\nUsageExamples builds a section that generates a help text showing a valid invocation of the CLI tool.\n\n## Types\n\n```go\ntype BuildFunctionData struct {\n\t// Name is the build payload function name.\n\tName string\n\t// Description describes the payload function.\n\tDescription string\n\t// ActualParams is the list of passed build function parameters.\n\tActualParams []string\n\t// FormalParams is the list of build function formal parameter\n\t// names.\n\tFormalParams []string\n\t// ServiceName is the name of the service.\n\tServiceName string\n\t// MethodName is the name of the method.\n\tMethodName string\n\t// ResultType is the fully qualified payload type name.\n\tResultType string\n\t// Fields describes the payload fields.\n\tFields []*FieldData\n\t// PayloadInit contains the data needed to render the function\n\t// body.\n\tPayloadInit *PayloadInitData\n\t// CheckErr is true if the payload initialization code requires an\n\t// \"err error\" variable that must be checked.\n\tCheckErr bool\n}\n```\nBuildFunctionData contains the data needed to generate a constructor function that builds a service method payload type from the command-line flags.\n\n```go\ntype CommandData struct {\n\t// Name of command e.g. \"cellar-storage\"\n\tName string\n\t// VarName is the name of the command variable e.g.\n\t// \"cellarStorage\"\n\tVarName string\n\t// Description is the help text.\n\tDescription string\n\t// Subcommands is the list of endpoint commands.\n\tSubcommands []*SubcommandData\n\t// Example is a valid command invocation, starting with the\n\t// command name.\n\tExample string\n\t// PkgName is the service HTTP client package import name,\n\t// e.g. \"storagec\".\n\tPkgName string\n\t// UsageName is the allocated command usage function name.\n\tUsageName string\n\t// FlagSetName is the allocated command flag-set variable name.\n\tFlagSetName string\n\t// Interceptors contains the data for client interceptors if any.\n\tInterceptors *InterceptorData\n}\n```\nCommandData contains the data needed to render a command.\n\n```go\nfunc AllocateCommandIdentifiers(commands []*CommandData, reserved []string) []*CommandData\n```\nAllocateCommandIdentifiers returns detached command data whose generated client import, usage function, flag-set, and flag-value identifiers are unique among reserved and the other command identifiers in the file.\n\n```go\nfunc BuildCommandData(data *service.Data) *CommandData\n```\nBuildCommandData builds the data needed by CLI code generators to render the parsing of the service command.\n\n```go\ntype FieldData struct {\n\t// Name is the field name, e.g. \"Vintage\"\n\tName string\n\t// VarName is the name of the local variable holding the field\n\t// value, e.g. \"vintage\"\n\tVarName string\n\t// TypeRef is the reference to the type.\n\tTypeRef string\n\t// Init is the code initializing the variable.\n\tInit *jen.Statement\n}\n```\nFieldData contains the data needed to generate the code that initializes a field in the method payload type.\n\n```go\ntype FlagData struct {\n\t// Name is the name of the flag, e.g. \"list-vintage\"\n\tName string\n\t// VarName is the name of the flag variable, e.g. \"listVintage\"\n\tVarName string\n\t// Type is the type of the flag, e.g. INT\n\tType string\n\t// FullName is the flag full name e.g. \"storageAddVintage\"\n\tFullName string\n\t// ValueName is the allocated parser-local variable holding the flag value.\n\tValueName string\n\t// Description is the flag help text.\n\tDescription string\n\t// Required is true if the flag is required.\n\tRequired bool\n\t// Example returns a JSON serialized example value.\n\tExample string\n\t// Default is the default flag input, or nil when none is declared.\n\t// Named primitive and collection flags use JSON text, while byte flags\n\t// use raw text.\n\tDefault any\n\t// Unmarshal is the qualified name of the function that decodes\n\t// the value of a JSON flag, such as \"protojson.Unmarshal\". The\n\t// function takes the JSON bytes and a pointer to the value and\n\t// returns an error. It is \"json.Unmarshal\" of encoding/json/v2\n\t// when empty. The file that holds the decoding code must import\n\t// the package of the function.\n\tUnmarshal string\n}\n```\nFlagData contains the data needed to render a command-line flag.\n\n```go\nfunc NewFlagData(svcn, en, name, typeName, description string, required bool, example, def any) *FlagData\n```\nNewFlagData creates a new FlagData from the given argument attributes.\n\nsvcn is the service name en is the endpoint name name is the flag name typeName is the flag type description is the flag description required determines if the flag is required example is an example value for the flag\n\n```go\ntype InterceptorData struct {\n\t// VarName is the name of the interceptor variable.\n\tVarName string\n\t// PkgName is the package name containing the interceptor type.\n\tPkgName string\n}\n```\nInterceptorData contains the data needed to generate interceptor code.\n\n```go\ntype PayloadInitData struct {\n\t// Code is the payload initialization code.\n\tCode *jen.Statement\n\t// ReturnTypeAttribute if non-empty returns an attribute in the payload\n\t// type that describes the shape of the method payload.\n\tReturnTypeAttribute string\n\t// ReturnTypeAttributePointer is true if the return type attribute\n\t// generated struct field holds a pointer\n\tReturnTypeAttributePointer bool\n\t// ReturnTypeAttributeUnionValue is true if the generated transform\n\t// returns a union pointer for a required value field.\n\tReturnTypeAttributeUnionValue bool\n\t// ReturnTypeAttributeFlag is the build function parameter holding\n\t// the flag that initializes the ReturnTypeAttribute field when that\n\t// field is optional and Code does not leave it nil for an empty\n\t// flag. The build function then runs Code and sets the field only\n\t// when the flag is not empty.\n\tReturnTypeAttributeFlag string\n\t// ReturnIsStruct if true indicates that the method payload is an object.\n\tReturnIsStruct bool\n\t// ReturnTypeName is the fully-qualified name of the payload.\n\tReturnTypeName string\n\t// ReturnTypePkg is the package name where the payload is present.\n\tReturnTypePkg string\n\t// Args is the list of arguments for the constructor.\n\tArgs []*codegen.InitArgData\n\t// ErrorAware is true when Code can fail. Code then records the\n\t// failure through a transformErr variable of type *error that the\n\t// build function declares and checks before returning the payload.\n\tErrorAware bool\n}\n```\nPayloadInitData contains the data needed to generate a constructor function that initializes a service method payload type from the command-ling arguments.\n\n```go\ntype SubcommandData struct {\n\t// Name is the sub-command name e.g. \"add\"\n\tName string\n\t// FullName is the sub-command full name e.g. \"storageAdd\"\n\tFullName string\n\t// PayloadType is the method payload type used by direct flag conversion.\n\tPayloadType string\n\t// UsageName is the allocated sub-command usage function name.\n\tUsageName string\n\t// FlagSetName is the allocated sub-command flag-set variable name.\n\tFlagSetName string\n\t// Description is the help text.\n\tDescription string\n\t// Flags is the list of flags supported by the subcommand.\n\tFlags []*FlagData\n\t// MethodVarName is the endpoint method name, e.g. \"Add\"\n\tMethodVarName string\n\t// ClientInterceptorWrapper is the allocated service endpoint wrapper name.\n\tClientInterceptorWrapper string\n\t// BuildFunction contains the data to generate a payload builder function\n\t// if any. Exclusive with Conversion.\n\tBuildFunction *BuildFunctionData\n\t// Conversion contains the flag value to payload conversion function if\n\t// any. Exclusive with BuildFunction.\n\tConversion *jen.Statement\n\t// Example is a valid command invocation, starting with the command name.\n\tExample string\n\t// Interceptors contains the data for client interceptors if any apply to the endpoint method.\n\tInterceptors *InterceptorData\n}\n```\nSubcommandData contains the data needed to render a sub-command.\n\n```go\nfunc BuildSubcommandData(\n\tdata *service.Data,\n\tm *service.MethodData,\n\tbuildFunction *BuildFunctionData,\n\tflags []*FlagData,\n\twrapper string,\n) *SubcommandData\n```\nBuildSubcommandData builds the data needed by CLI code generators to render the CLI parsing of the service sub-command. The wrapper is the allocated interceptor wrapper of the transport endpoint this command invokes.\n\n","path":"github.com/CaliLuke/loom/codegen/cli","name":"cli","synopsis":"Package cli contains helpers used by transport-specific command-line client generators for parsing the command-line flags to identify the service and the method to make a request along with the request payload to be sent.","isRedistributable":true}
