Skip to content

Commit

Permalink
Refactor: Refactors most commands to utilize machinery & cleans up …
Browse files Browse the repository at this point in the history
…unused legacy functions (#321)

* First pass for deploy & list

* Additional refactors & cleanup

* Additional refactors & cleanup

* Updated deleteEnv to use machinery + legacy cleanup

* Additional updates

* Committing notification progress

* Additional refactors

* Committing group progress

* Pointed to machinery

* Upated to fix test failure

* Updated go.sum

* Refactored addProjectToGroupCmd & addUserToGroupCmd

* Updated group commands to use machinery

* Updated & refactored all list commands

* Removed legacy test

* Updated project commands

* Refactored notifications + minor updates

* Updated to machinery & refactored variables, tasks & users

* Task refactor progress

* Includes input checks

* Removed legacy funcs + cleanup

* Resolved conflicts

* Updated to latest machinery

* Updated to accomodate null buildImage

* Reverted error handling

* Cleaned up addProjec

* Updated docs

* fix: render an empty data structure instead of a half populated one, and return err in missed err capture

* chore: remove short import references for machinery

* chore: remove additional unused code and fix up some error handling

* chore: remove remaining legacy client and update tasks to use machinery

* chore: remove more unused code

---------

Co-authored-by: shreddedbacon <[email protected]>
  • Loading branch information
CGoodwin90 and shreddedbacon authored Jun 19, 2024
1 parent a386f67 commit adbc9a3
Show file tree
Hide file tree
Showing 124 changed files with 2,130 additions and 8,130 deletions.
55 changes: 30 additions & 25 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cmd
import (
"context"
"fmt"
"strconv"

lclient "github.com/uselagoon/machinery/api/lagoon/client"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
"github.com/uselagoon/machinery/api/lagoon"
"github.com/uselagoon/machinery/api/schema"
)

var deployCmd = &cobra.Command{
Expand Down Expand Up @@ -43,8 +45,8 @@ use 'lagoon deploy latest' instead`,
if err != nil {
return err
}
if cmdProjectName == "" || branch == "" {
return fmt.Errorf("missing arguments: Project name or branch name is not defined")
if err := requiredInputCheck("Project name", cmdProjectName, "Branch name", branch); err != nil {
return err
}

buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
Expand All @@ -58,11 +60,12 @@ use 'lagoon deploy latest' instead`,

if yesNo(fmt.Sprintf("You are attempting to deploy branch '%s' for project '%s', are you sure?", branch, cmdProjectName)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
depBranch := &schema.DeployEnvironmentBranchInput{
Branch: branch,
Expand Down Expand Up @@ -108,8 +111,8 @@ var deployPromoteCmd = &cobra.Command{
if err != nil {
return err
}
if cmdProjectName == "" || sourceEnvironment == "" || destinationEnvironment == "" {
return fmt.Errorf("missing arguments: Project name, source environment, or destination environment is not defined")
if err := requiredInputCheck("Project name", cmdProjectName, "Source environment", sourceEnvironment, "Destination environment", destinationEnvironment); err != nil {
return err
}

buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
Expand All @@ -123,11 +126,12 @@ var deployPromoteCmd = &cobra.Command{

if yesNo(fmt.Sprintf("You are attempting to promote environment '%s' to '%s' for project '%s', are you sure?", sourceEnvironment, destinationEnvironment, cmdProjectName)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
result, err := lagoon.DeployPromote(context.TODO(), &schema.DeployEnvironmentPromoteInput{
SourceEnvironment: sourceEnvironment,
Expand Down Expand Up @@ -170,21 +174,22 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
buildVarMap, err := buildVarsToMap(buildVarStrings)
if err != nil {
return err
}

if cmdProjectName == "" || cmdProjectEnvironment == "" {
return fmt.Errorf("missing arguments: Project name or environment name is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to deploy the latest environment '%s' for project '%s', are you sure?", cmdProjectEnvironment, cmdProjectName)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
result, err := lagoon.DeployLatest(context.TODO(), &schema.DeployEnvironmentLatestInput{
Environment: schema.EnvironmentInput{
Expand Down Expand Up @@ -244,9 +249,8 @@ This pullrequest may not already exist as an environment in lagoon.`,
if err != nil {
return err
}
if cmdProjectName == "" || prTitle == "" || prNumber == 0 || baseBranchName == "" ||
baseBranchRef == "" || headBranchName == "" || headBranchRef == "" {
return fmt.Errorf("missing arguments: Project name, title, number, baseBranchName, baseBranchRef, headBranchName, or headBranchRef is not defined")
if err := requiredInputCheck("Project name", cmdProjectName, "Pullrequest title", prTitle, "Pullrequest number", strconv.Itoa(int(prNumber)), "baseBranchName", baseBranchName, "baseBranchRef", baseBranchRef, "headBranchName", headBranchName, "headBranchRef", headBranchRef); err != nil {
return err
}
buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
if err != nil {
Expand All @@ -263,11 +267,12 @@ This pullrequest may not already exist as an environment in lagoon.`,
}
if yesNo(fmt.Sprintf("You are attempting to deploy pull request '%v' for project '%s', are you sure?", prNumber, cmdProjectName)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)

result, err := lagoon.DeployPullRequest(context.TODO(), &schema.DeployEnvironmentPullrequestInput{
Expand Down
82 changes: 42 additions & 40 deletions cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import (
"strconv"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
"github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"
"github.com/uselagoon/machinery/api/schema"
)

var addDeployTargetCmd = &cobra.Command{
Expand Down Expand Up @@ -65,14 +62,8 @@ var addDeployTargetCmd = &cobra.Command{
return err
}

if name == "" {
return fmt.Errorf("missing arguments: name is not defined")
}
if token == "" {
return fmt.Errorf("missing arguments: token is not defined")
}
if consoleURL == "" {
return fmt.Errorf("missing arguments: console-url is not defined")
if err := requiredInputCheck("Name", name, "Token", token, "Console-url", consoleURL); err != nil {
return err
}

addDeployTarget := &schema.AddDeployTargetInput{
Expand All @@ -96,20 +87,21 @@ var addDeployTargetCmd = &cobra.Command{
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
handleError(err)
return err
}
current := lagoonCLIConfig.Current
lc := client.New(
lagoonToken := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&lagoonToken,
debug)

if yesNo(fmt.Sprintf("You are attempting to add '%s' DeployTarget, are you sure?", addDeployTarget.Name)) {
addDeployTargetResponse, err := lagoon.AddDeployTarget(context.TODO(), addDeployTarget, lc)
if err != nil {
handleError(err)
return err
}

data := []output.Data{}
Expand Down Expand Up @@ -208,16 +200,22 @@ var updateDeployTargetCmd = &cobra.Command{

debug, err := cmd.Flags().GetBool("debug")
if err != nil {
handleError(err)
return err
}

if err := requiredInputCheck("Deploytarget ID", strconv.Itoa(int(id))); err != nil {
return err
}

current := lagoonCLIConfig.Current
lc := client.New(
lagoonToken := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
debug,
)
lagoonCLIConfig.Lagoons[current].Version,
&lagoonToken,
debug)

updateDeployTarget := &schema.UpdateDeployTargetInput{
AddDeployTargetInput: schema.AddDeployTargetInput{
ID: id,
Expand All @@ -235,7 +233,7 @@ var updateDeployTargetCmd = &cobra.Command{
if yesNo(fmt.Sprintf("You are attempting to update '%d' DeployTarget, are you sure?", updateDeployTarget.ID)) {
updateDeployTargetResponse, err := lagoon.UpdateDeployTarget(context.TODO(), updateDeployTarget, lc)
if err != nil {
handleError(err)
return err
}

data := []output.Data{}
Expand Down Expand Up @@ -293,27 +291,31 @@ var deleteDeployTargetCmd = &cobra.Command{

debug, err := cmd.Flags().GetBool("debug")
if err != nil {
handleError(err)
return err
}

if err := requiredInputCheck("Deploytarget name", name); err != nil {
return err
}

current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
debug,
)
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)

deleteDeployTarget := &schema.DeleteDeployTargetInput{
Name: name,
}
if yesNo(fmt.Sprintf("You are attempting to delete DeployTarget '%s', are you sure?", deleteDeployTarget.Name)) {
deleteDeployTargetResponse, err := lagoon.DeleteDeployTarget(context.TODO(), deleteDeployTarget, lc)
if err != nil {
handleError(err)
return err
}

handleError(err)
resultData := output.Result{
Result: deleteDeployTargetResponse.DeleteDeployTarget,
}
Expand Down Expand Up @@ -347,7 +349,6 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
if err := requiredInputCheck("Organization name", organizationName, "Deploy Target", strconv.Itoa(int(deploytarget))); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
Expand All @@ -357,23 +358,24 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
organization, err := lagoon.GetOrganizationByName(context.TODO(), organizationName, lc)
if err != nil {
return err
}
if organization.Name == "" {
return fmt.Errorf("error querying organization by name")
}

deployTargetInput := s.AddDeployTargetToOrganizationInput{
deployTargetInput := schema.AddDeployTargetToOrganizationInput{
DeployTarget: deploytarget,
Organization: organization.ID,
}

deployTargetResponse, err := l.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc)
deployTargetResponse, err := lagoon.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc)
if err != nil {
return err
}

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
Expand Down Expand Up @@ -420,21 +422,21 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
organization, err := lagoon.GetOrganizationByName(context.TODO(), organizationName, lc)
if err != nil {
return err
}
if organization.Name == "" {
return fmt.Errorf("error querying organization by name")
}

deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
deployTargetInput := schema.RemoveDeployTargetFromOrganizationInput{
DeployTarget: deploytarget,
Organization: organization.ID,
}

if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deploytarget, organization.Name)) {
_, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
_, err := lagoon.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit adbc9a3

Please sign in to comment.