Skip to content

Commit

Permalink
chore: merge main and fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Dec 5, 2023
2 parents 7a1b87f + eeb83b5 commit 615e822
Show file tree
Hide file tree
Showing 65 changed files with 870 additions and 772 deletions.
17 changes: 9 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (

// LagoonConfigFlags .
type LagoonConfigFlags struct {
Lagoon string `json:"lagoon,omitempty"`
Hostname string `json:"hostname,omitempty"`
Port string `json:"port,omitempty"`
GraphQL string `json:"graphql,omitempty"`
Token string `json:"token,omitempty"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
Lagoon string `json:"lagoon,omitempty"`
Hostname string `json:"hostname,omitempty"`
Port string `json:"port,omitempty"`
GraphQL string `json:"graphql,omitempty"`
Token string `json:"token,omitempty"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
SSHPortal bool `json:"sshportal,omitempty"`
}

func parseLagoonConfig(flags pflag.FlagSet) LagoonConfigFlags {
Expand Down
23 changes: 16 additions & 7 deletions cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cmd
import (
"context"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
Expand Down Expand Up @@ -41,38 +44,44 @@ var addDeployTargetConfigCmd = &cobra.Command{
return err
}

if cmdProjectName == "" {
return fmt.Errorf("Missing arguments: project is a required flag")
}
if deploytarget == 0 {
return fmt.Errorf("Missing arguments: deploytarget id is a required flag")
}
if pullrequests == "" {
return fmt.Errorf("Missing arguments: pullrequests is a required flag")
}
if branches == "" {
return fmt.Errorf("Missing arguments: branches is a required flag")
}
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,
&token,
debug)
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
addDeployTargetConfig := &schema.AddDeployTargetConfigInput{
addDeployTargetConfig := &s.AddDeployTargetConfigInput{
Project: uint(project.ID),
Weight: weight,
}
if branches != "" {
addDeployTargetConfig.Branches = branches
}
if branches != "" {
if pullrequests != "" {
addDeployTargetConfig.Pullrequests = pullrequests
}
if deploytarget != 0 {
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 := l.AddDeployTargetConfiguration(context.TODO(), addDeployTargetConfig, lc)
if err != nil {
return err
}
Expand Down
174 changes: 155 additions & 19 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,29 @@ package cmd
import (
"context"
"fmt"
s "github.com/uselagoon/machinery/api/schema"
"os"
"strings"

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

// @TODO re-enable this at some point if more environment based commands are made availab;e
// EnvironmentFlags .
// type EnvironmentFlags struct {
// Name string `json:"name,omitempty"`
// }

// func parseEnvironmentFlags(flags pflag.FlagSet) EnvironmentFlags {
// configMap := make(map[string]interface{})
// flags.VisitAll(func(f *pflag.Flag) {
// if flags.Changed(f.Name) {
// configMap[f.Name] = f.Value
// }
// })
// jsonStr, _ := json.Marshal(configMap)
// parsedFlags := EnvironmentFlags{}
// json.Unmarshal(jsonStr, &parsedFlags)
// return parsedFlags
// }
// @TODO re-enable this at some point if more environment based commands are made available
var environmentAutoIdle uint
var environmentAutoIdleProvided bool

var deleteEnvCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"e"},
Short: "Delete an environment",
Run: func(cmd *cobra.Command, args []string) {
// environmentFlags := parseEnvironmentFlags(*cmd.Flags()) //@TODO re-enable this at some point if more environment based commands are made availab;e
// environmentFlags := parseEnvironmentFlags(*cmd.Flags()) //@TODO re-enable this at some point if more environment based commands are made available
if cmdProjectName == "" || cmdProjectEnvironment == "" {
fmt.Println("Missing arguments: Project name or environment name is not defined")
cmd.Help()
Expand All @@ -52,6 +42,142 @@ var deleteEnvCmd = &cobra.Command{
},
}

var updateEnvironmentCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"e"},
Short: "Update an environment",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
deployBaseRef, err := cmd.Flags().GetString("deploy-base-ref")
if err != nil {
return err
}
deployHeadRef, err := cmd.Flags().GetString("deploy-head-ref")
if err != nil {
return err
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
route, err := cmd.Flags().GetString("route")
if err != nil {
return err
}
routes, err := cmd.Flags().GetString("routes")
if err != nil {
return err
}
environmentType, err := cmd.Flags().GetString("environment-type")
if err != nil {
return err
}
deployT, err := cmd.Flags().GetString("deploy-type")
if err != nil {
return err
}
openShift, err := cmd.Flags().GetUint("deploy-target")
if err != nil {
return err
}
deployTitle, err := cmd.Flags().GetString("deploy-title")
if err != nil {
return err
}

cmd.Flags().Visit(checkFlags)

if cmdProjectName == "" || cmdProjectEnvironment == "" {
fmt.Println("Missing arguments: Project name or environment name is not defined")
cmd.Help()
os.Exit(1)
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if project.Name == "" {
err = fmt.Errorf("project not found")
}
handleError(err)
environment, err := l.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if environment.Name == "" {
err = fmt.Errorf("environment not found")
}
handleError(err)

environmentFlags := s.UpdateEnvironmentPatchInput{
DeployBaseRef: nullStrCheck(deployBaseRef),
DeployHeadRef: nullStrCheck(deployHeadRef),
OpenshiftProjectName: nullStrCheck(namespace),
Route: nullStrCheck(route),
Routes: nullStrCheck(routes),
DeployTitle: nullStrCheck(deployTitle),
Openshift: nullIntCheck(openShift),
}
if environmentAutoIdleProvided {
environmentFlags.AutoIdle = &environmentAutoIdle
}
if environmentType != "" {
envType := s.EnvType(strings.ToUpper(environmentType))
if validationErr := s.ValidateType(envType); validationErr != nil {
handleError(validationErr)
}
environmentFlags.EnvironmentType = &envType
}
if deployT != "" {
deployType := s.DeployType(strings.ToUpper(deployT))
if validationErr := s.ValidateType(deployType); validationErr != nil {
handleError(validationErr)
}
environmentFlags.DeployType = &deployType
}

result, err := l.UpdateEnvironment(context.TODO(), environment.ID, environmentFlags, lc)
handleError(err)

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Environment Name": result.Name,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

func nullStrCheck(s string) *string {
if s == "" {
return nil
}
return &s
}

func nullIntCheck(i uint) *uint {
if i == 0 {
return nil
}
return &i
}

func checkFlags(f *pflag.Flag) {
if f.Name == "auto-idle" {
environmentAutoIdleProvided = true
}
}

var listBackupsCmd = &cobra.Command{
Use: "backups",
Aliases: []string{"b"},
Expand Down Expand Up @@ -169,4 +295,14 @@ This returns a direct URL to the backup, this is a signed download link with a l
func init() {
getCmd.AddCommand(getBackupCmd)
getBackupCmd.Flags().StringP("backup-id", "B", "", "The backup ID you want to restore")
updateEnvironmentCmd.Flags().String("deploy-base-ref", "", "Updates the deploy base ref for the selected environment")
updateEnvironmentCmd.Flags().String("deploy-head-ref", "", "Updates the deploy head ref for the selected environment")
updateEnvironmentCmd.Flags().String("deploy-title", "", "Updates the deploy title for the selected environment")
updateEnvironmentCmd.Flags().String("namespace", "", "Update the namespace for the selected environment")
updateEnvironmentCmd.Flags().String("route", "", "Update the route for the selected environment")
updateEnvironmentCmd.Flags().String("routes", "", "Update the routes for the selected environment")
updateEnvironmentCmd.Flags().UintVarP(&environmentAutoIdle, "auto-idle", "a", 1, "Auto idle setting of the environment")
updateEnvironmentCmd.Flags().UintP("deploy-target", "d", 0, "Reference to OpenShift Object this Environment should be deployed to")
updateEnvironmentCmd.Flags().String("environment-type", "", "Update the environment type - production | development")
updateEnvironmentCmd.Flags().String("deploy-type", "", "Update the deploy type - branch | pullrequest | promote")
}
Loading

0 comments on commit 615e822

Please sign in to comment.