core

package
v0.0.0-...-4df811a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CTX_INIT    = "init"
	CTX_RESOLVE = "resolve"
)
View Source
const (
	CTX_OVERWRITE_LOCAL_FILES = true
	CTX_KEEP_LOCAL_FILES      = !CTX_OVERWRITE_LOCAL_FILES
)

Variables

View Source
var ErrorHookFailed = errors.New("hook failed")

Functions

func Contains

func Contains(arr []string, str string) bool

Contains function tells if a slice of string contains a string

func PrintObject

func PrintObject(object interface{})

PrintObject function prints an object. For debugging.

func Uniq

func Uniq(arr []string) []string

Uniq function removes duplicatos form a slice of strings

Types

type Change

type Change struct {
	Name string     // Secret name or file path
	From string     // Value before, empty if creation
	To   string     // New value, empty if deletion
	Type ChangeType // Type of change (file, secret, version only)
}

Change struct describers sercret/file changes

func GetSecretsChanges

func GetSecretsChanges(
	localSecrets []models.SecretVal,
	newSecrets []models.SecretVal,
) (changes []Change)

GetSecretsChanges function Returns a list of changes for secrets

func (Change) IsFile

func (c Change) IsFile() bool

IsFile method tells if the change is about a file

func (Change) IsSecretAdd

func (c Change) IsSecretAdd() bool

IsSecretAdd method tells if the change is adding a secret

func (Change) IsSecretChange

func (c Change) IsSecretChange() bool

IsSecretChange method tells if the change is changing a secret

func (Change) IsSecretDelete

func (c Change) IsSecretDelete() bool

IsSecretDelete method tells if the change is removing a secret

type ChangeType

type ChangeType string
const (
	ChangeTypeSecretAdd    ChangeType = "secret"
	ChangeTypeSecretChange ChangeType = "change"
	ChangeTypeSecretDelete ChangeType = "delete"
	ChangeTypeFile         ChangeType = "file"
	// This one happens when environment version changed
	// but there is no messages along with it
	ChangeTypeVersion ChangeType = "version"
)

type Changes

type Changes []Change

func (Changes) IsEmpty

func (changes Changes) IsEmpty() bool

IsEmpty method tells if the list is empty

func (Changes) IsSingleVersionChange

func (changes Changes) IsSingleVersionChange() bool

IsSingleVersionChange method return true if the list of changes has single version change, which means the environment changed, but we don't have a message for it

type ChangesByEnvironment

type ChangesByEnvironment struct {
	Environments map[string]Changes
}

ChangesByEnvironment struct is a list of changes grouped by environment

func (ChangesByEnvironment) ChangedEnvironmentsWithoutPayload

func (ce ChangesByEnvironment) ChangedEnvironmentsWithoutPayload() []string

/ Returns a list of all environments that have a different / VersionID but no payload for the user.

type Context

type Context struct {
	Wd                     string
	TmpDir                 string
	ConfigDir              string
	AccessibleEnvironments []models.Environment
	// contains filtered or unexported fields
}

func New

func New(flag string) *Context

Creates a new execution context

When flag equals CTX_INIT, the current working directory is used as the project's root. When flag equals CTX_RESOLVE, the program tries to find the project's root in a parent directory. En error is returned if none is found.

An error will be returned if flag is neither of those values

func (*Context) AddFile

func (ctx *Context) AddFile(
	file keystonefile.FileKey,
	envContentMap map[string][]byte,
) *Context

AddFile method adds a file to keystone file and cache

func (*Context) AddHook

func (c *Context) AddHook(command string)

func (*Context) AddSecret

func (ctx *Context) AddSecret(
	secretName string,
	secretValue map[string]string,
	flag SecretStrictFlag,
) *Context

Sets an env variable to keep track of across environments [secretName] is the name of the variable to set [secretValue] maps environment to the varable value (key is environment name, and value, the value of the variable in that environment)

func (*Context) CachedDotEnvPath

func (c *Context) CachedDotEnvPath() string

CachedDotEnvPath method returns the path of the .env in .keystone

func (*Context) CachedEnvironmentDotEnvPath

func (c *Context) CachedEnvironmentDotEnvPath(environmentName string) string

CachedEnvironmentDotEnvPath method returns the path to the .env for the given environment in cache

func (*Context) CachedEnvironmentFilesPath

func (c *Context) CachedEnvironmentFilesPath(environmentName string) string

CachedEnvironmentFilesPath method returns the path to the files dir for the given environment in cache

func (*Context) CachedEnvironmentPath

func (c *Context) CachedEnvironmentPath(environmentName string) string

CachedEnvironmentPath method returns the path to the environment in cache e.g. `$PWD/.keystone/cache/dev`

func (*Context) CleanUp

func (context *Context) CleanUp()

Remove temporary files

func (*Context) CompareNewFileWhithChanges

func (ctx *Context) CompareNewFileWhithChanges(
	filePath string,
	changesByEnvironment ChangesByEnvironment,
) *Context

CompareNewFileWhithChanges method compares the latest set of changes with a new file

func (*Context) CompareNewSecretWithChanges

func (ctx *Context) CompareNewSecretWithChanges(
	secretName string,
	newSecret map[string]string,
	changesByEnvironment ChangesByEnvironment,
) *Context

CompareNewSecretWithChanges method compares recent changes with new secret name and values

func (*Context) CompareRemovedSecretWithChanges

func (ctx *Context) CompareRemovedSecretWithChanges(
	secretName string,
	changesByEnvironment ChangesByEnvironment,
) *Context

CompareRemovedSecretWithChanges method compares the removed secrete with the latest set of changes

func (*Context) CurrentEnvironment

func (ctx *Context) CurrentEnvironment() string

CurrentEnvironment method returns the current environment name

func (*Context) Destroy

func (ctx *Context) Destroy() error

Removes the keystone.yaml, and the .keystone file

func (*Context) DotKeystonePath

func (c *Context) DotKeystonePath() string

DotKeystonePath method returns the path to the .keystone dir

func (*Context) EnvironmentID

func (ctx *Context) EnvironmentID() string

EnvironmentID method returns the environmentID of the current environment

func (*Context) EnvironmentVersion

func (ctx *Context) EnvironmentVersion() string

EnvironmentVersion method returns the local version of the current environment

func (*Context) EnvironmentVersionByName

func (ctx *Context) EnvironmentVersionByName(name string) string

EnvironmentVersionByName method returns the local version of the environment named `name`

func (*Context) EnvironmentVersionHasChanged

func (ctx *Context) EnvironmentVersionHasChanged(
	name string,
	environmentVersion string,
) bool

EnvironmentVersionHasChanged method indiciates whether the versions differ

func (*Context) EnvironmentsFromConfig

func (ctx *Context) EnvironmentsFromConfig() []environmentsfile.Env

EnvironmentsFromConfig method reads the environmentfile

func (*Context) Err

func (context *Context) Err() *kserrors.Error

Accessor for error

func (*Context) FilesUseEnvironment

func (ctx *Context) FilesUseEnvironment(
	currentEnvironment string,
	targetEnvironment string,
	forceCopy bool,
) *Context

FilesUseEnvironment creates copies of files found in the project’s keystone.yaml file, from the environment `targetEnvironment` in cache.

func (*Context) GetAllSecrets

func (ctx *Context) GetAllSecrets(envName string) map[string]string

GetAllSecrets method returns all the secrets and thei value for the given environment

func (*Context) GetFileContents

func (ctx *Context) GetFileContents(
	fileName string, environmentName string,
) (contents []byte, err error)

GetFileContents returns the file contents for the given envsrionment as a slice of bytes. It returns an error if reading the file fails (Pemission denied, no exists…) or if the file is empty (content length equals 0)

func (*Context) GetHook

func (c *Context) GetHook() (hook *Hook, ok bool)

-- HOOKS

func (*Context) GetLocalFileContents

func (ctx *Context) GetLocalFileContents(
	fileName string,
) (contents []byte, err error)

GetLocalFileContents returns the file contents as a slice of bytes. It returns an error if reading the file fails (Pemission denied, no exists…)

func (*Context) GetProjectID

func (ctx *Context) GetProjectID() string

GetProjectID method returns the project ID from the keystonefile

func (*Context) GetProjectName

func (ctx *Context) GetProjectName() string

GetProjectName method returns the project name from the keysotnefile

func (*Context) GetSecret

func (ctx *Context) GetSecret(secretName string) *Secret

Returns a secret value for everu environments

func (*Context) GetSecrets

func (ctx *Context) GetSecrets() map[string]string

Returns all environments variable for the current environment, a map

func (*Context) HasEnvironment

func (ctx *Context) HasEnvironment(name string) bool

HasEnvironment method returns true environment exists

func (*Context) HasFile

func (ctx *Context) HasFile(fileName string) bool

Returns a boolean indicating wether the file `fileName` exists in the local files

func (*Context) HasSecret

func (ctx *Context) HasSecret(secretName string) bool

Returns a boolean indicating wether the secret `secretName` exists in the local files

func (*Context) Init

func (ctx *Context) Init(project models.Project) *Context

Initialize the projects directory structure.

In the execution context's working directory, it creates: - keystone.yaml - .keystone/ - .keystone/environment - .keystone/cache/ - .keystone/cache/.env - .keystone/cache/dev/ - .keystone/cache/ci/ - .keystone/cache/staging/ - .keystone/cache/prod/

It adds .keystone to .gitignore, creating it if does not exist

func (*Context) IsFileModified

func (ctx *Context) IsFileModified(
	filePath, environment string,
) (isModified bool)

IsFileModified method indicates whether the local version, and the in-cache version for the current environment of a file differ

func (*Context) ListAllFiles

func (ctx *Context) ListAllFiles(environmentName string) []FileDescriptor

/ Returns a list of display friendly FileDescriptors, / for every file that exist in the project, both in the `keystone.yaml` file / and the `.keystone` cache folder.

func (*Context) ListCachedFilesForEnvironment

func (ctx *Context) ListCachedFilesForEnvironment(
	envname string,
) []keystonefile.FileKey

ListCachedFilesForEnvironment method returns a list of all the files present in the cache for the given environment

func (*Context) ListEnvironments

func (ctx *Context) ListEnvironments() []string

ListEnvironments method returns a list of environments that can be found in the cache

func (*Context) ListFiles

func (ctx *Context) ListFiles() []keystonefile.FileKey

ListFiles method returns a list of all files in the keystone file

func (*Context) ListFilesFromCache

func (ctx *Context) ListFilesFromCache() []keystonefile.FileKey

ListFilesFromCache method returns a list of the files present in the cache

func (*Context) ListSecrets

func (ctx *Context) ListSecrets() []Secret

Returns secrets from keystone.yaml, and their value in each environment.

func (*Context) ListSecretsFromCache

func (ctx *Context) ListSecretsFromCache() []Secret

List secret from .keystone/cache, and their value in each environment.

func (*Context) LoadEnvironmentsFile

func (ctx *Context) LoadEnvironmentsFile() *environmentsfile.EnvironmentsFile

LoadEnvironmentsFile method

func (*Context) LocallyModifiedFiles

func (ctx *Context) LocallyModifiedFiles(
	envname string,
) []keystonefile.FileKey

LocallyModifiedFiles returns the list of file whose local content are different than the version in cache for the given environment (e.g. modified by the user)

func (*Context) MarkFileRequired

func (ctx *Context) MarkFileRequired(
	filePath string,
	required bool,
) *Context

MarkFileRequired method changes the required status of a file

func (*Context) MarkSecretRequired

func (ctx *Context) MarkSecretRequired(
	secretName string,
	required bool,
) *Context

MarkSecretRequired method changes the required status of a secret

func (*Context) MissingFilesForEnvironment

func (ctx *Context) MissingFilesForEnvironment(
	environmentName string,
) ([]string, bool)

MissingFilesForEnvironment return a list of missing files for the given environment. The second returned value indicates whether the list contains something or not.

func (*Context) MissingSecretsForEnvironment

func (ctx *Context) MissingSecretsForEnvironment(
	environmentName string,
) ([]string, bool)

MissingSecretsForEnvironment method returns a list of all the secrets that are listed in keystone.yaml and are not in the cache. The second value is true if there are missing things.

func (*Context) MustHaveAccessToEnvironment

func (ctx *Context) MustHaveAccessToEnvironment(
	environmentName string,
) *Context

MustHaveAccessToEnvironment method exits with an error if the user does not have read access to the environment

func (*Context) MustHaveEnvironment

func (ctx *Context) MustHaveEnvironment(name string)

MustHaveEnvironment method exits with an error if the environment does not exist

func (*Context) MustHaveProject

func (ctx *Context) MustHaveProject()

MustHaveProject method sets a context error if the keystonefile does not have a project id

func (*Context) PrepareMessagePayload

func (ctx *Context) PrepareMessagePayload(
	environment models.Environment,
) (models.MessagePayload, error)

Return PayloadContent, with secrets and files of current environment.

func (*Context) PurgeSecrets

func (ctx *Context) PurgeSecrets() *Context

PurgeSecets Removes from the cache of all environments all secrets that are not found in the project’s keystone.yaml This implies that sending the environment to other users will remove those values for them too

func (*Context) RemoveEnvironment

func (ctx *Context) RemoveEnvironment(name string) *Context

RemoveEnvironment method Deprecated

func (*Context) RemoveFile

func (ctx *Context) RemoveFile(
	filePath string,
	force bool,
	purge bool,
	accessibleEnvironments []models.Environment,
) *Context

RemoveFile method removes a file from the keystonefile. `purge` also removes the file from the cache

func (*Context) RemoveForbiddenEnvironments

func (ctx *Context) RemoveForbiddenEnvironments(
	accessibleEnvironments []models.Environment,
)

RemoveForbiddenEnvironments method removes the environments information the current user may not access.

func (*Context) RemoveSecret

func (ctx *Context) RemoveSecret(secretName string, purge bool) *Context

Unsets a previously set environment variable

[varname] The variable to unset It will be removed in all existing environment.

func (*Context) RunHook

func (c *Context) RunHook() error

func (*Context) SaveMessages

func (ctx *Context) SaveMessages(
	MessageByEnvironments models.GetMessageByEnvironmentResponse,
) ChangesByEnvironment

/ Writes the contents of messages in the project's cache / It will replace secrets value with the new ones, / add new secrets and their values / remove secrets that have been removed by other user

func (*Context) SecretIsRequired

func (ctx *Context) SecretIsRequired(secretName string) bool

SecretIsRequired method tells if `secretName` is required.

func (*Context) SetAllSecrets

func (ctx *Context) SetAllSecrets(
	name string,
	secrets map[string]string,
) *Context

SetAllSecrets method sets all the secrets at once in the cache

func (*Context) SetCurrent

func (ctx *Context) SetCurrent(name string) *Context

SetCurrent method changes the current environment

func (*Context) SetError

func (ctx *Context) SetError(err *kserrors.Error) *Context

SetError method sets the context error

func (*Context) SetFile

func (ctx *Context) SetFile(filePath string, content []byte) *Context

SetFile method changes the files content

func (*Context) SetSecret

func (ctx *Context) SetSecret(
	envName string,
	secretName string,
	secretValue string,
) *Context

Sets an existing an secret for a given envitronment [envName] name of the target environment [secretName] [secretValue]

func (*Context) UnsetSecret

func (ctx *Context) UnsetSecret(envName string, secretName string) *Context

UnsetSecret method clears the secret value

func (*Context) UpdateEnvironment

func (ctx *Context) UpdateEnvironment(environment models.Environment) *Context

UpdateEnvironment method updates environment info (e.g. versionID)

type EnvironmentName

type EnvironmentName string

type FileDescriptor

type FileDescriptor struct {
	Required  bool
	Available bool
	Modified  bool
	Path      string
}

type FileStrictFlag

type FileStrictFlag int
const (
	F_REQUIRED FileStrictFlag = iota
	F_OPTIONAL
)

type Hook

type Hook struct {
	Command string
	// contains filtered or unexported fields
}

func (*Hook) Run

func (h *Hook) Run() (err error)

Run method executes the hook. Note: it displays the output of the command to stdout in casse of success and to stderr in case of failure

type Secret

type Secret struct {
	Name      string
	Required  bool
	Values    map[EnvironmentName]SecretValue
	FromCache bool
}

func FilterSecretsFromCache

func FilterSecretsFromCache(
	secretsFromCache []Secret,
	secrets []Secret,
) []Secret

Returns an array of secrets that are in the first list, an not in the second

type SecretStrictFlag

type SecretStrictFlag int
const (
	S_REQUIRED SecretStrictFlag = iota
	S_OPTIONAL
)

type SecretValue

type SecretValue string

Jump to

Keyboard shortcuts

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