Skip to content

Commit

Permalink
chore: remove remaining legacy client and update tasks to use machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 17, 2024
1 parent cd4e346 commit f794793
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 34 deletions.
16 changes: 0 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/pkg/app"
"github.com/uselagoon/lagoon-cli/pkg/graphql"
"github.com/uselagoon/lagoon-cli/pkg/lagoon/environments"
"github.com/uselagoon/lagoon-cli/pkg/output"
"github.com/uselagoon/lagoon-cli/pkg/updatecheck"
)
Expand Down Expand Up @@ -282,9 +281,6 @@ func Prompt(prompt string) string {
return GetInput()
}

// global the clients
var eClient environments.Client

// FormatType .
type FormatType string

Expand All @@ -309,12 +305,6 @@ func validateToken(lagoon string) {
os.Exit(1)
}
}
// set up the clients
eClient, err = environments.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
outputOptions.Debug = debugEnable
}

Expand All @@ -333,12 +323,6 @@ func validateTokenE(lagoon string) error {
if err = loginToken(); err != nil {
return fmt.Errorf("couldn't refresh token: %w", err)
}
// set up the clients
eClient, err = environments.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
return err
}
outputOptions.Debug = debugEnable
// fallback if token is expired or there was no token to begin with
return nil
Expand Down
153 changes: 135 additions & 18 deletions cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,62 @@ var runDrushArchiveDump = &cobra.Command{
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
taskResult, err := eClient.RunDrushArchiveDump(cmdProjectName, cmdProjectEnvironment)
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
if err != nil {
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)
if err != nil {
return err
}
raw := `mutation runArdTask ($environment: Int!) {
taskDrushArchiveDump(environment: $environment) {
id
}
}`
rawResp, err := lc.ProcessRaw(context.TODO(), raw, map[string]interface{}{
"environment": environment.ID,
})
if err != nil {
return err
}
r, err := json.Marshal(rawResp)
if err != nil {
return err
}
var resultMap map[string]interface{}
err = json.Unmarshal([]byte(taskResult), &resultMap)
err = json.Unmarshal([]byte(r), &resultMap)
if err != nil {
return err
}
resultData := output.Result{
Result: "success",
ResultData: resultMap,
if resultMap["taskDrushArchiveDump"] != nil {
resultData := output.Result{
Result: "success",
ResultData: resultMap["taskDrushArchiveDump"].(map[string]interface{}),
}
output.RenderResult(resultData, outputOptions)
} else {
return fmt.Errorf("unable to determine status of task")
}
output.RenderResult(resultData, outputOptions)
return nil
},
}
Expand All @@ -158,23 +197,62 @@ var runDrushSQLDump = &cobra.Command{
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
taskResult, err := eClient.RunDrushSQLDump(cmdProjectName, cmdProjectEnvironment)
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
if err != nil {
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)
if err != nil {
return err
}
raw := `mutation runSqlDump ($environment: Int!) {
taskDrushSqlDump(environment: $environment) {
id
}
}`
rawResp, err := lc.ProcessRaw(context.TODO(), raw, map[string]interface{}{
"environment": environment.ID,
})
if err != nil {
return err
}
r, err := json.Marshal(rawResp)
if err != nil {
return err
}
var resultMap map[string]interface{}
err = json.Unmarshal([]byte(taskResult), &resultMap)
err = json.Unmarshal([]byte(r), &resultMap)
if err != nil {
return err
}
resultData := output.Result{
Result: "success",
ResultData: resultMap,
if resultMap["taskDrushSqlDump"] != nil {
resultData := output.Result{
Result: "success",
ResultData: resultMap["taskDrushSqlDump"].(map[string]interface{}),
}
output.RenderResult(resultData, outputOptions)
} else {
return fmt.Errorf("unable to determine status of task")
}
output.RenderResult(resultData, outputOptions)
return nil
},
}
Expand All @@ -187,23 +265,62 @@ var runDrushCacheClear = &cobra.Command{
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
taskResult, err := eClient.RunDrushCacheClear(cmdProjectName, cmdProjectEnvironment)
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
if err != nil {
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)
if err != nil {
return err
}
raw := `mutation runCacheClear ($environment: Int!) {
taskDrushCacheClear(environment: $environment) {
id
}
}`
rawResp, err := lc.ProcessRaw(context.TODO(), raw, map[string]interface{}{
"environment": environment.ID,
})
if err != nil {
return err
}
r, err := json.Marshal(rawResp)
if err != nil {
return err
}
var resultMap map[string]interface{}
err = json.Unmarshal([]byte(taskResult), &resultMap)
err = json.Unmarshal([]byte(r), &resultMap)
if err != nil {
return err
}
resultData := output.Result{
Result: "success",
ResultData: resultMap,
if resultMap["taskDrushCacheClear"] != nil {
resultData := output.Result{
Result: "success",
ResultData: resultMap["taskDrushCacheClear"].(map[string]interface{}),
}
output.RenderResult(resultData, outputOptions)
} else {
return fmt.Errorf("unable to determine status of task")
}
output.RenderResult(resultData, outputOptions)
return nil
},
}
Expand Down

0 comments on commit f794793

Please sign in to comment.