Documentation
¶
Index ¶
- Constants
- func GetAPIKeyFromHelper(ctx context.Context, helperCmd string) (string, error)
- func GetAPIKeyFromHelperWithCache(ctx context.Context, helperCmd string, refreshInterval time.Duration) (string, error)
- func GetTemplateByBytes(name string, data map[string]interface{}) ([]byte, error)
- func GetTemplateByString(name string, data map[string]interface{}) (string, error)
- func IsCommandAvailable(cmd string) bool
- func LoadTemplates(files embed.FS) error
- func LoadTemplatesFromDir(dir string) error
- func NewTemplateByString(format string, data map[string]interface{}) (string, error)
- type Data
Constants ¶
const ( // HelperTimeout is the maximum time to wait for the API key helper script to execute HelperTimeout = 10 * time.Second // DefaultRefreshInterval is the default interval for refreshing API keys DefaultRefreshInterval = 900 * time.Second // 15 minutes )
Variables ¶
This section is empty.
Functions ¶
func GetAPIKeyFromHelper ¶ added in v1.4.0
GetAPIKeyFromHelper executes a shell command to dynamically generate an API key. The command is executed in /bin/sh with a timeout controlled by the provided context. It returns the trimmed output from stdout, or an error if the command fails.
On timeout, it kills the entire process group (shell and all descendants) using a two-phase approach: SIGTERM for graceful termination, then SIGKILL if needed.
Security note: The returned API key is sensitive and should not be logged.
func GetAPIKeyFromHelperWithCache ¶ added in v1.4.0
func GetAPIKeyFromHelperWithCache( ctx context.Context, helperCmd string, refreshInterval time.Duration, ) (string, error)
GetAPIKeyFromHelperWithCache executes a shell command to dynamically generate an API key, with file-based caching support. The API key is cached for the specified refresh interval. If refreshInterval is 0, the cache is disabled and the command is executed every time.
The cache is stored in ~/.config/codegpt/.cache/ directory with restrictive permissions (0600).
Parameters:
- ctx: Context for controlling execution and timeouts
- helperCmd: The shell command to execute
- refreshInterval: How long to cache the API key (0 to disable caching)
Returns the API key from cache if still valid, otherwise executes the helper command.
Security note: The returned API key is sensitive and should not be logged. Cache files are stored with 0600 permissions but contain the API key in JSON format.
func GetTemplateByBytes ¶ added in v0.1.0
GetTemplateByBytes returns the parsed template as a byte slice. It returns an error if the template processing fails.
func GetTemplateByString ¶ added in v0.1.0
GetTemplateByString returns the parsed template as a string. It returns an error if the template processing fails.
func IsCommandAvailable ¶
IsCommandAvailable checks if a given command is available in the system's PATH. It takes a string argument 'cmd' which represents the command to check. It returns true if the command is found, otherwise false.
func LoadTemplates ¶
LoadTemplates loads all the templates found in the templates directory from the embedded filesystem. It returns an error if reading the directory or parsing any template fails.
func LoadTemplatesFromDir ¶ added in v0.16.0
LoadTemplatesFromDir loads all the templates found in the specified directory from the filesystem. It returns an error if reading the directory or parsing any template fails.
func NewTemplateByString ¶ added in v0.1.0
NewTemplateByString parses a template from a string and executes it with the provided data. It returns the resulting string or an error if the template parsing or execution fails.
Types ¶
type Data ¶
type Data map[string]interface{}
Data defines a custom type for the template data.
func ConvertToMap ¶ added in v0.2.1
ConvertToMap takes a slice of strings in the format "key=value" and converts it into a Data map. Each string in the slice is split into a key and value pair using the first occurrence of the "=" character. If a string does not contain the "=" character, it is ignored. The resulting map contains the keys and values from the input slice.
Args:
args ([]string): A slice of strings where each string is in the format "key=value".
Returns:
Data: A map where the keys and values are derived from the input slice.