Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove getminimalprojectbyname where possible #390

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ var addDeployTargetConfigCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
addDeployTargetConfig := &schema.AddDeployTargetConfigInput{
Project: project.ID,
Weight: weight,
Weight: weight,
}
if branches != "" {
addDeployTargetConfig.Branches = branches
Expand All @@ -75,9 +70,9 @@ var addDeployTargetConfigCmd = &cobra.Command{
addDeployTargetConfig.DeployTarget = deploytarget
}
if yesNo(fmt.Sprintf("You are attempting to add a deploytarget configuration to project '%s', are you sure?", cmdProjectName)) {
deployTargetConfig, err := lagoon.AddDeployTargetConfiguration(context.TODO(), addDeployTargetConfig, lc)
deployTargetConfig, err := lagoon.AddDeployTargetConfigurationByProjectName(context.TODO(), cmdProjectName, addDeployTargetConfig, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}
data := []output.Data{}
data = append(data, []string{
Expand Down Expand Up @@ -234,18 +229,10 @@ var deleteDeployTargetConfigCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}

if yesNo(fmt.Sprintf("You are attempting to delete deploytarget configuration with id '%d' from project '%s', are you sure?", id, cmdProjectName)) {
result, err := lagoon.DeleteDeployTargetConfiguration(context.TODO(), int(id), int(project.ID), lc)
result, err := lagoon.DeleteDeployTargetConfigurationByIDAndProjectName(context.TODO(), id, cmdProjectName, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}
resultData := output.Result{
Result: result.DeleteDeployTargetConfig,
Expand Down Expand Up @@ -284,16 +271,9 @@ var listDeployTargetConfigsCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}
deployTargetConfigs, err := lagoon.GetDeployTargetConfigs(context.TODO(), int(project.ID), lc)
deployTargetConfigs, err := lagoon.GetDeployTargetConfigsByProjectName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}
if len(*deployTargetConfigs) == 0 {
return handleNilResults("No deploytarget-configs for project '%s'\n", cmd, cmdProjectName)
Expand Down
34 changes: 7 additions & 27 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,10 @@ var updateEnvironmentCmd = &cobra.Command{
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if project.Name == "" {
err = fmt.Errorf("project not found")
}
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if environment.Name == "" {
err = fmt.Errorf("environment not found")
}

environment, err := lagoon.GetEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}

environmentFlags := schema.UpdateEnvironmentPatchInput{
Expand Down Expand Up @@ -202,16 +193,9 @@ var listBackupsCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}
backupsResult, err := lagoon.GetBackupsForEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
backupsResult, err := lagoon.GetBackupsForEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}
data := []output.Data{}
for _, backup := range backupsResult.Backups {
Expand Down Expand Up @@ -276,13 +260,9 @@ This returns a direct URL to the backup, this is a signed download link with a l
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
backupsResult, err := lagoon.GetBackupsForEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
}
backupsResult, err := lagoon.GetBackupsForEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}
status := ""
for _, backup := range backupsResult.Backups {
Expand Down
10 changes: 1 addition & 9 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,9 @@ var getProjectKeyCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}

projectKey, err := lagoon.GetProjectKeyByName(context.TODO(), cmdProjectName, revealValue, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}
if projectKey.PublicKey == "" {
return handleNilResults("No project-key for project '%s'\n", cmd, cmdProjectName)
Expand Down
19 changes: 2 additions & 17 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,9 @@ var addProjectToGroupCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if len(project.Name) == 0 {
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}
_, err = lagoon.AddProjectToGroup(context.TODO(), projectGroup, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or group exists", err.Error())
}

resultData := output.Result{
Expand Down Expand Up @@ -328,18 +321,10 @@ var deleteProjectFromGroupCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if len(project.Name) == 0 {
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}

if yesNo(fmt.Sprintf("You are attempting to delete project '%s' from group '%s', are you sure?", projectGroup.Project.Name, projectGroup.Groups[0].Name)) {
_, err = lagoon.RemoveGroupsFromProject(context.TODO(), projectGroup, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or group exists", err.Error())
}

resultData := output.Result{
Expand Down
35 changes: 6 additions & 29 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,9 @@ var listDeploymentsCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
deployments, err := lagoon.GetDeploymentsByEnvironmentAndProjectName(context.TODO(), cmdProjectName, cmdProjectEnvironment, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}

deployments, err := lagoon.GetDeploymentsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}

data := []output.Data{}
Expand Down Expand Up @@ -558,17 +550,9 @@ var listTasksCmd = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
tasks, err := lagoon.GetTasksByEnvironmentAndProjectName(context.TODO(), cmdProjectName, cmdProjectEnvironment, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}

tasks, err := lagoon.GetTasksByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}

data := []output.Data{}
Expand Down Expand Up @@ -796,16 +780,9 @@ var listInvokableTasks = &cobra.Command{
&token,
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("No project found for '%s'\n", cmd, cmdProjectName)
}
tasks, err := lagoon.GetInvokableAdvancedTaskDefinitionsByEnvironment(context.TODO(), project.ID, cmdProjectEnvironment, lc)
tasks, err := lagoon.GetInvokableAdvancedTaskDefinitionsByEnvironmentAndProjectName(context.TODO(), cmdProjectName, cmdProjectEnvironment, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project or environment exists", err.Error())
}

data := []output.Data{}
Expand Down
28 changes: 8 additions & 20 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,9 @@ var updateProjectCmd = &cobra.Command{
}
}

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if project.Name == "" {
return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName)
}
projectUpdate, err := lagoon.UpdateProject(context.TODO(), int(project.ID), projectPatch, lc)
projectUpdate, err := lagoon.UpdateProjectByName(context.TODO(), cmdProjectName, projectPatch, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}

resultData := output.Result{
Expand Down Expand Up @@ -573,13 +566,10 @@ var updateProjectMetadata = &cobra.Command{
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
projectResult, err := lagoon.UpdateProjectMetadata(context.TODO(), int(project.ID), key, value, lc)

projectResult, err := lagoon.UpdateProjectMetadataByName(context.TODO(), cmdProjectName, key, value, lc)
if err != nil {
return err
return fmt.Errorf("%v: check if the project exists", err.Error())
}
data := []output.Data{}
metaData, _ := json.Marshal(projectResult.Metadata)
Expand Down Expand Up @@ -630,11 +620,8 @@ var deleteProjectMetadataByKey = &cobra.Command{
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
projectResult, err := lagoon.RemoveProjectMetadataByKey(context.TODO(), int(project.ID), key, lc)

projectResult, err := lagoon.RemoveProjectMetadataByKeyByName(context.TODO(), cmdProjectName, key, lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -690,6 +677,7 @@ var removeProjectFromOrganizationCmd = &cobra.Command{
&token,
debug)

// this can stay for now as `removeProjectFromOrganization` is platform only scoped
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
Expand Down
24 changes: 4 additions & 20 deletions cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ var runDrushArchiveDump = &cobra.Command{
return err
}

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
environment, err := lagoon.GetEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,11 +214,7 @@ var runDrushSQLDump = &cobra.Command{
return err
}

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
environment, err := lagoon.GetEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -287,11 +279,7 @@ var runDrushCacheClear = &cobra.Command{
return err
}

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
environment, err := lagoon.GetEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -476,11 +464,7 @@ Path:
Command: taskCommand,
Service: taskService,
}
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
environment, err := lagoon.GetEnvironmentByNameAndProjectName(context.TODO(), cmdProjectEnvironment, cmdProjectName, lc)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.2
github.com/uselagoon/machinery v0.0.29
github.com/uselagoon/machinery v0.0.30
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.26.0
golang.org/x/term v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c=
github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc=
github.com/uselagoon/machinery v0.0.30 h1:nmdDyeEfts+obg4x/uIzCbHcNqyOkJZL0dUkI0hli48=
github.com/uselagoon/machinery v0.0.30/go.mod h1:RsHzIMOam3hiA4CKR12yANgzdTGy6tz4D19umjMzZyw=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
Expand Down