1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-21 14:29:40 +02:00

chore(code): use int ranges in loops BE-10990 (#12028)

This commit is contained in:
andres-portainer 2024-07-10 19:22:47 -03:00 committed by GitHub
parent 468c12c75b
commit 31bdb948a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 183 additions and 169 deletions

View file

@ -50,13 +50,12 @@ var funcmap = map[StackRemoteOperation]buildCmdFunc{
// build the unpacker cmd for stack based on stackOperation
func (d *stackDeployer) buildUnpackerCmdForStack(stack *portainer.Stack, operation StackRemoteOperation, opts unpackerCmdBuilderOptions) ([]string, error) {
fn := funcmap[operation]
if fn == nil {
return nil, fmt.Errorf("unknown stack operation %s", operation)
}
registriesStrings := getRegistry(opts.registries, d.dataStore)
registriesStrings := generateRegistriesStrings(opts.registries, d.dataStore)
envStrings := getEnv(stack.Env)
return fn(stack, opts, registriesStrings, envStrings), nil
@ -64,72 +63,74 @@ func (d *stackDeployer) buildUnpackerCmdForStack(stack *portainer.Stack, operati
// deploy [-u username -p password] [--skip-tls-verify] [--env KEY1=VALUE1 --env KEY2=VALUE2] <git-repo-url> <ref> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildDeployCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdDeploy)
cmd := []string{UnpackerCmdDeploy}
cmd = appendGitAuthIfNeeded(cmd, stack)
cmd = appendSkipTLSVerifyIfNeeded(cmd, stack)
cmd = append(cmd, env...)
cmd = append(cmd, registries...)
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.GitConfig.ReferenceName)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd,
stack.GitConfig.URL,
stack.GitConfig.ReferenceName,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// undeploy [-u username -p password] [-k] <git-repo-url> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildUndeployCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdUndeploy)
cmd := []string{UnpackerCmdUndeploy}
cmd = appendGitAuthIfNeeded(cmd, stack)
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd, stack.GitConfig.URL,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// deploy [-u username -p password] [--skip-tls-verify] [-k] [--env KEY1=VALUE1 --env KEY2=VALUE2] <git-repo-url> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildComposeStartCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdDeploy)
cmd := []string{UnpackerCmdDeploy}
cmd = appendGitAuthIfNeeded(cmd, stack)
cmd = appendSkipTLSVerifyIfNeeded(cmd, stack)
cmd = append(cmd, "-k")
cmd = append(cmd, env...)
cmd = append(cmd, registries...)
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.GitConfig.ReferenceName)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd, stack.GitConfig.URL,
stack.GitConfig.ReferenceName,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// undeploy [-u username -p password] [-k] <git-repo-url> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildComposeStopCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdUndeploy)
cmd := []string{UnpackerCmdUndeploy}
cmd = appendGitAuthIfNeeded(cmd, stack)
cmd = append(cmd, "-k")
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd,
"-k",
stack.GitConfig.URL,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// swarm-deploy [-u username -p password] [--skip-tls-verify] [-f] [-r] [--env KEY1=VALUE1 --env KEY2=VALUE2] <git-repo-url> <git-ref> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildSwarmDeployCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdSwarmDeploy)
cmd := []string{UnpackerCmdSwarmDeploy}
cmd = appendGitAuthIfNeeded(cmd, stack)
cmd = appendSkipTLSVerifyIfNeeded(cmd, stack)
if opts.pullImage {
cmd = append(cmd, "-f")
}
@ -137,85 +138,76 @@ func buildSwarmDeployCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions,
if opts.prune {
cmd = append(cmd, "-r")
}
cmd = append(cmd, env...)
cmd = append(cmd, registries...)
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.GitConfig.ReferenceName)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd, stack.GitConfig.URL,
stack.GitConfig.ReferenceName,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// swarm-undeploy [-k] <project-name> <destination>
func buildSwarmUndeployCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdSwarmUndeploy)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
return cmd
return []string{UnpackerCmdSwarmUndeploy, stack.Name, opts.composeDestination}
}
// swarm-deploy [-u username -p password] [-f] [-r] [-k] [--skip-tls-verify] [--env KEY1=VALUE1 --env KEY2=VALUE2] <git-repo-url> <project-name> <destination> <compose-file-path> [<more-file-paths>...]
func buildSwarmStartCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdSwarmDeploy, "-f", "-r", "-k")
cmd := []string{UnpackerCmdSwarmDeploy, "-f", "-r", "-k"}
cmd = appendSkipTLSVerifyIfNeeded(cmd, stack)
cmd = append(cmd, getEnv(stack.Env)...)
cmd = append(cmd, registries...)
cmd = append(cmd, stack.GitConfig.URL)
cmd = append(cmd, stack.GitConfig.ReferenceName)
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
cmd = append(cmd, stack.EntryPoint)
cmd = appendAdditionalFiles(cmd, stack.AdditionalFiles)
return cmd
cmd = append(cmd, stack.GitConfig.URL,
stack.GitConfig.ReferenceName,
stack.Name,
opts.composeDestination,
stack.EntryPoint,
)
return append(cmd, stack.AdditionalFiles...)
}
// swarm-undeploy [-k] <project-name> <destination>
func buildSwarmStopCmd(stack *portainer.Stack, opts unpackerCmdBuilderOptions, registries []string, env []string) []string {
cmd := []string{}
cmd = append(cmd, UnpackerCmdSwarmUndeploy, "-k")
cmd = append(cmd, stack.Name)
cmd = append(cmd, opts.composeDestination)
return cmd
return []string{UnpackerCmdSwarmUndeploy, "-k", stack.Name, opts.composeDestination}
}
func appendGitAuthIfNeeded(cmd []string, stack *portainer.Stack) []string {
if stack.GitConfig.Authentication != nil && len(stack.GitConfig.Authentication.Password) != 0 {
cmd = append(cmd, "-u", stack.GitConfig.Authentication.Username, "-p", stack.GitConfig.Authentication.Password)
if stack.GitConfig.Authentication == nil || stack.GitConfig.Authentication.Password == "" {
return cmd
}
return cmd
return append(cmd, "-u", stack.GitConfig.Authentication.Username, "-p", stack.GitConfig.Authentication.Password)
}
func appendSkipTLSVerifyIfNeeded(cmd []string, stack *portainer.Stack) []string {
if stack.GitConfig.TLSSkipVerify {
cmd = append(cmd, "--skip-tls-verify")
if !stack.GitConfig.TLSSkipVerify {
return cmd
}
return cmd
return append(cmd, "--skip-tls-verify")
}
func appendAdditionalFiles(cmd []string, files []string) []string {
for i := 0; i < len(files); i++ {
cmd = append(cmd, files[i])
}
return cmd
}
func getRegistry(registries []portainer.Registry, dataStore dataservices.DataStore) []string {
func generateRegistriesStrings(registries []portainer.Registry, dataStore dataservices.DataStore) []string {
cmds := []string{}
for _, registry := range registries {
if registry.Authentication {
err := registryutils.EnsureRegTokenValid(dataStore, &registry)
if err == nil {
username, password, err := registryutils.GetRegEffectiveCredential(&registry)
if err == nil {
cmd := fmt.Sprintf("--registry=%s:%s:%s", username, password, registry.URL)
cmds = append(cmds, cmd)
}
if err := registryutils.EnsureRegTokenValid(dataStore, &registry); err != nil {
continue
}
username, password, err := registryutils.GetRegEffectiveCredential(&registry)
if err != nil {
continue
}
cmds = append(cmds, fmt.Sprintf("--registry=%s:%s:%s", username, password, registry.URL))
}
}