Skip to content

Commit

Permalink
Updated cmds that output multiline values + refactored to use machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Apr 18, 2024
1 parent 6662fa5 commit 8833403
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 141 deletions.
60 changes: 46 additions & 14 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -224,24 +225,55 @@ var getProjectKeyCmd = &cobra.Command{
Use: "project-key",
Aliases: []string{"pk"},
Short: "Get a projects public key",
Run: func(cmd *cobra.Command, args []string) {
getProjectFlags := parseGetFlags(*cmd.Flags())
if getProjectFlags.Project == "" {
fmt.Println("Missing arguments: Project name is not defined")
cmd.Help()
os.Exit(1)
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
}
returnedJSON, err := pClient.GetProjectKey(getProjectFlags.Project, revealValue)
handleError(err)
var dataMain output.Table
err = json.Unmarshal([]byte(returnedJSON), &dataMain)
handleError(err)
reveal, err := cmd.Flags().GetBool("reveal")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName); err != nil {
return err
}

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

projectKey, err := l.GetProjectKeyByName(context.TODO(), cmdProjectName, reveal, lc)
projectKeys := []string{projectKey.PublicKey}
if projectKey.PrivateKey != "" {
projectKeys = append(projectKeys, strings.TrimSuffix(projectKey.PrivateKey, "\n"))
outputOptions.MultiLine = true
}

var data []output.Data
data = append(data, projectKeys)

dataMain := output.Table{
Header: []string{"PublicKey"},
Data: data,
}

if len(dataMain.Data) == 0 {
output.RenderInfo(fmt.Sprintf("No project-key for project '%s'", getProjectFlags.Project), outputOptions)
os.Exit(0)
outputOptions.Error = fmt.Sprintf("No project-key for project '%s'", cmdProjectName)
}
output.RenderOutput(dataMain, outputOptions)

if projectKey.PrivateKey != "" {
dataMain.Header = append(dataMain.Header, "PrivateKey")
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

Expand Down
31 changes: 15 additions & 16 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ 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/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
Expand Down Expand Up @@ -66,13 +63,14 @@ var listDeployTargetsCmd = &cobra.Command{
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,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
deploytargets, err := lagoon.ListDeployTargets(context.TODO(), lc)
deploytargets, err := l.ListDeployTargets(context.TODO(), lc)
if err != nil {
return err
}
Expand All @@ -94,6 +92,7 @@ var listDeployTargetsCmd = &cobra.Command{
returnNonEmptyString(fmt.Sprintf("%v", deploytarget.MonitoringConfig)),
})
}
outputOptions.MultiLine = true
output.RenderOutput(output.Table{
Header: []string{
"ID",
Expand Down Expand Up @@ -239,10 +238,8 @@ var listVariablesCmd = &cobra.Command{
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
if cmdProjectName == "" {
fmt.Println("Missing arguments: Project name is not defined")
cmd.Help()
os.Exit(1)
if err := requiredInputCheck("Project name", cmdProjectName); err != nil {
return err
}
reveal, err := cmd.Flags().GetBool("reveal")
if err != nil {
Expand All @@ -253,17 +250,18 @@ var listVariablesCmd = &cobra.Command{
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,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)
in := &schema.EnvVariableByProjectEnvironmentNameInput{
in := &ls.EnvVariableByProjectEnvironmentNameInput{
Project: cmdProjectName,
Environment: cmdProjectEnvironment,
}
envvars, err := lagoon.GetEnvVariablesByProjectEnvironmentName(context.TODO(), in, lc)
envvars, err := l.GetEnvVariablesByProjectEnvironmentName(context.TODO(), in, lc)
if err != nil {
return err
}
Expand All @@ -280,6 +278,7 @@ var listVariablesCmd = &cobra.Command{
env = append(env, returnNonEmptyString(fmt.Sprintf("%v", envvar.Name)))
if reveal {
env = append(env, fmt.Sprintf("%v", envvar.Value))
outputOptions.MultiLine = true
}
data = append(data, env)
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@ var getUserKeysCmd = &cobra.Command{
if err != nil {
return err
}
if userEmail == "" {
fmt.Println("Missing arguments: Email address is not defined")
return nil
if err := requiredInputCheck("Email address", userEmail); err != nil {
return err
}

current := lagoonCLIConfig.Current
Expand All @@ -274,7 +273,9 @@ var getUserKeysCmd = &cobra.Command{
&token,
debug)
userKeys, err := l.GetUserSSHKeysByEmail(context.TODO(), userEmail, lc)
handleError(err)
if err != nil {
return err
}
if len(userKeys.SSHKeys) == 0 {
output.RenderInfo(fmt.Sprintf("No SSH keys for user '%s'", strings.ToLower(userEmail)), outputOptions)
return nil
Expand All @@ -296,6 +297,7 @@ var getUserKeysCmd = &cobra.Command{
Data: data,
}

outputOptions.MultiLine = true
output.RenderOutput(dataMain, outputOptions)
return nil
},
Expand Down Expand Up @@ -360,7 +362,7 @@ var getAllUserKeysCmd = &cobra.Command{
Header: []string{"ID", "Email", "Name", "Type", "Value"},
Data: data,
}

outputOptions.MultiLine = true
output.RenderOutput(dataMain, outputOptions)
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions 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.20
github.com/uselagoon/machinery v0.0.21-0.20240418053856-64532c59a80d
golang.org/x/crypto v0.21.0
golang.org/x/term v0.18.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -40,4 +40,4 @@ require (
golang.org/x/sys v0.18.0 // indirect
)

// replace github.com/uselagoon/machinery => ../machinery
//replace github.com/uselagoon/machinery => ../machinery
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/uselagoon/machinery v0.0.20 h1:4pGGX/Y5UwbT86TU9vaP7QyjGk8wvikUVDvkTpesvbs=
github.com/uselagoon/machinery v0.0.20/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
github.com/uselagoon/machinery v0.0.21-0.20240418053856-64532c59a80d h1:tBLPNkjps1mBkCupbvaWsko+ZuQO157RnWLTWON044k=
github.com/uselagoon/machinery v0.0.21-0.20240418053856-64532c59a80d/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down

This file was deleted.

19 changes: 0 additions & 19 deletions internal/lagoon/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,3 @@ func (c *Client) ListDeployTargets(
Response: deploytargets,
})
}

// GetEnvVariablesByProjectEnvironmentName queries the Lagoon API for a envvars by project environment and unmarshals the response.
func (c *Client) GetEnvVariablesByProjectEnvironmentName(
ctx context.Context, in *schema.EnvVariableByProjectEnvironmentNameInput, envkeyvalue *[]schema.EnvKeyValue) error {

req, err := c.newRequest("_lgraphql/variables/getEnvVariablesByProjectEnvironmentName.graphql",
map[string]interface{}{
"input": in,
})
if err != nil {
return err
}

return c.client.Run(ctx, req, &struct {
Response *[]schema.EnvKeyValue `json:"getEnvVariablesByProjectEnvironmentName"`
}{
Response: envkeyvalue,
})
}
7 changes: 0 additions & 7 deletions internal/lagoon/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
type Variables interface {
AddOrUpdateEnvVariableByName(ctx context.Context, in *schema.EnvVariableByNameInput, envvar *schema.UpdateEnvVarResponse) error
DeleteEnvVariableByName(ctx context.Context, in *schema.DeleteEnvVariableByNameInput, envvar *schema.DeleteEnvVarResponse) error
GetEnvVariablesByProjectEnvironmentName(ctx context.Context, in *schema.EnvVariableByProjectEnvironmentNameInput, envvar *[]schema.EnvKeyValue) error
}

func AddOrUpdateEnvVariableByName(ctx context.Context, in *schema.EnvVariableByNameInput, v Variables) (*schema.UpdateEnvVarResponse, error) {
Expand All @@ -21,9 +20,3 @@ func DeleteEnvVariableByName(ctx context.Context, in *schema.DeleteEnvVariableBy
envvar := schema.DeleteEnvVarResponse{}
return &envvar, v.DeleteEnvVariableByName(ctx, in, &envvar)
}

// ListEnvVars gets info of envvars in lagoon.
func GetEnvVariablesByProjectEnvironmentName(ctx context.Context, in *schema.EnvVariableByProjectEnvironmentNameInput, v Variables) (*[]schema.EnvKeyValue, error) {
envvar := []schema.EnvKeyValue{}
return &envvar, v.GetEnvVariablesByProjectEnvironmentName(ctx, in, &envvar)
}
5 changes: 0 additions & 5 deletions internal/schema/envVar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ type DeleteEnvVariableByNameInput struct {
Name string `json:"name"`
}

type EnvVariableByProjectEnvironmentNameInput struct {
Environment string `json:"environment,omitempty"`
Project string `json:"project"`
}

type UpdateEnvVarResponse struct {
EnvKeyValue
}
Expand Down
54 changes: 0 additions & 54 deletions pkg/lagoon/projects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package projects
import (
"encoding/json"
"fmt"
"strings"

"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/graphql"
Expand All @@ -21,7 +19,6 @@ type Projects struct {
type Client interface {
ListAllProjects() ([]byte, error)
ListProjectVariables(string, bool) ([]byte, error)
GetProjectKey(string, bool) ([]byte, error)
GetProjectInfo(string) ([]byte, error)
DeleteProject(string) ([]byte, error)
AddProject(string, string) ([]byte, error)
Expand Down Expand Up @@ -222,54 +219,3 @@ func processProjectUpdate(projectByName []byte, jsonPatch string) (api.UpdatePro
}
return projectUpdate, nil
}

// GetProjectKey will get basic info about a project
func (p *Projects) GetProjectKey(projectName string, revealValue bool) ([]byte, error) {
// get project info from lagoon
project := api.Project{
Name: projectName,
}
keyFragment := `fragment Project on Project {
publicKey
}`
if revealValue {
keyFragment = `fragment Project on Project {
privateKey
publicKey
}`
}
projectByName, err := p.api.GetProjectByName(project, keyFragment)
if err != nil {
return []byte(""), err
}
returnResult, err := processProjectKey(projectByName, revealValue)
if err != nil {
return []byte(""), err
}
return returnResult, nil
}

func processProjectKey(projectByName []byte, revealValue bool) ([]byte, error) {
var project api.Project
err := json.Unmarshal([]byte(projectByName), &project)
if err != nil {
return []byte(""), err
}
// get the key, but strip the newlines we don't need
projectData := []string{
strings.TrimSuffix(project.PublicKey, "\n"),
}
if revealValue {
projectData = append(projectData, strings.TrimSuffix(project.PrivateKey, "\n"))
}
var data []output.Data
data = append(data, projectData)
dataMain := output.Table{
Header: []string{"PublicKey"},
Data: data,
}
if revealValue {
dataMain.Header = append(dataMain.Header, "PrivateKey")
}
return json.Marshal(dataMain)
}
Loading

0 comments on commit 8833403

Please sign in to comment.