Skip to content

acookin/trustcenter #818

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
65 changes: 65 additions & 0 deletions cmd/cli/cmd/trustcenter/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package trustcenter

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

var createCmd = &cobra.Command{
Use: "create",
Short: "create a new trustcenter",
Run: func(cmd *cobra.Command, args []string) {
err := create(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(createCmd)

// command line flags for the create command
createCmd.Flags().StringP("custom-domain-id", "d", "", "custom domain id for the trustcenter")
createCmd.Flags().StringSliceP("tags", "t", []string{}, "tags associated with the trustcenter")
}

// createValidation validates the required fields for the command
func createValidation() (input openlaneclient.CreateTrustCenterInput, err error) {
// validation of required fields for the create command
// output the input struct with the required fields and optional fields based on the command line flags

customDomainID := cmd.Config.String("custom-domain-id")
if customDomainID != "" {
input.CustomDomainID = &customDomainID
}

tags := cmd.Config.Strings("tags")
if len(tags) > 0 {
input.Tags = tags
}

return input, nil
}

// create a new trustcenter
func create(ctx context.Context) error {
// attempt to setup with token, otherwise fall back to JWT with session
client, err := cmd.TokenAuth(ctx, cmd.Config)
if err != nil || client == nil {
// setup http client
client, err = cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)
}

input, err := createValidation()
cobra.CheckErr(err)

o, err := client.CreateTrustCenter(ctx, input)
cobra.CheckErr(err)

return consoleOutput(o)
}
54 changes: 54 additions & 0 deletions cmd/cli/cmd/trustcenter/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package trustcenter

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete an existing trustcenter",
Run: func(cmd *cobra.Command, args []string) {
err := delete(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(deleteCmd)

deleteCmd.Flags().StringP("id", "i", "", "trustcenter id to delete")
}

// deleteValidation validates the required fields for the command
func deleteValidation() (string, error) {
id := cmd.Config.String("id")
if id == "" {
return "", cmd.NewRequiredFieldMissingError("id")
}

return id, nil
}

// delete an existing trustcenter in the platform
func delete(ctx context.Context) error {
// attempt to setup with token, otherwise fall back to JWT with session
client, err := cmd.TokenAuth(ctx, cmd.Config)
if err != nil || client == nil {
// setup http client
client, err = cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)
}

id, err := deleteValidation()
cobra.CheckErr(err)

o, err := client.DeleteTrustCenter(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}
2 changes: 2 additions & 0 deletions cmd/cli/cmd/trustcenter/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package trustcenter is our cobra cli for trustcenter endpoints
package trustcenter
52 changes: 52 additions & 0 deletions cmd/cli/cmd/trustcenter/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package trustcenter

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "get an existing trustcenter",
Run: func(cmd *cobra.Command, args []string) {
err := get(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(getCmd)

getCmd.Flags().StringP("id", "i", "", "trustcenter id to query")
}

// get an existing trustcenter in the platform
func get(ctx context.Context) error {
// attempt to setup with token, otherwise fall back to JWT with session
client, err := cmd.TokenAuth(ctx, cmd.Config)
if err != nil || client == nil {
// setup http client
client, err = cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)
}
// filter options
id := cmd.Config.String("id")

// if a trustcenter ID is provided, filter on that trustcenter, otherwise get all
if id != "" {
o, err := client.GetTrustCenterByID(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}

// get all will be filtered for the authorized organization(s)
o, err := client.GetAllTrustCenters(ctx)
cobra.CheckErr(err)

return consoleOutput(o)
}
126 changes: 126 additions & 0 deletions cmd/cli/cmd/trustcenter/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package trustcenter

import (
"encoding/json"
"strings"

"github.com/spf13/cobra"

"github.com/theopenlane/utils/cli/tables"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

// command represents the base trustcenter command when called without any subcommands
var command = &cobra.Command{
Use: "trustcenter",
Short: "the subcommands for working with trustcenters",
}

func init() {
cmd.RootCmd.AddCommand(command)
}

// consoleOutput prints the output in the console
func consoleOutput(e any) error {
// check if the output format is JSON and print the trustcenters in JSON format
if strings.EqualFold(cmd.OutputFormat, cmd.JSONOutput) {
return jsonOutput(e)
}

// check the type of the trustcenters and print them in a table format
switch v := e.(type) {
case *openlaneclient.GetAllTrustCenters:
var nodes []*openlaneclient.GetAllTrustCenters_TrustCenters_Edges_Node

for _, i := range v.TrustCenters.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetTrustCenters:
var nodes []*openlaneclient.GetTrustCenters_TrustCenters_Edges_Node

for _, i := range v.TrustCenters.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetTrustCenterByID:
e = v.TrustCenter
case *openlaneclient.CreateTrustCenter:
e = v.CreateTrustCenter.TrustCenter
case *openlaneclient.UpdateTrustCenter:
e = v.UpdateTrustCenter.TrustCenter
case *openlaneclient.DeleteTrustCenter:
deletedTableOutput(v)
return nil
}

s, err := json.Marshal(e)
cobra.CheckErr(err)

var list []openlaneclient.GetAllTrustCenters_TrustCenters_Edges_Node

err = json.Unmarshal(s, &list)
if err != nil {
var in openlaneclient.GetAllTrustCenters_TrustCenters_Edges_Node
err = json.Unmarshal(s, &in)
cobra.CheckErr(err)

list = append(list, in)
}

tableOutput(list)

return nil
}

// jsonOutput prints the output in a JSON format
func jsonOutput(out any) error {
s, err := json.Marshal(out)
cobra.CheckErr(err)

return cmd.JSONPrint(s)
}

// tableOutput prints the output in a table format
func tableOutput(out []openlaneclient.GetAllTrustCenters_TrustCenters_Edges_Node) {
// create a table writer
writer := tables.NewTableWriter(command.OutOrStdout(), "ID", "Slug", "CustomDomainID", "OwnerID", "Tags", "CreatedAt", "UpdatedAt")
for _, i := range out {
customDomainID := ""
if i.CustomDomainID != nil {
customDomainID = *i.CustomDomainID
}

ownerID := ""
if i.OwnerID != nil {
ownerID = *i.OwnerID
}

createdAt := ""
if i.CreatedAt != nil {
createdAt = i.CreatedAt.Format("2006-01-02 15:04:05")
}

updatedAt := ""
if i.UpdatedAt != nil {
updatedAt = i.UpdatedAt.Format("2006-01-02 15:04:05")
}

writer.AddRow(i.ID, i.Slug, customDomainID, ownerID, strings.Join(i.Tags, ","), createdAt, updatedAt)
}

writer.Render()
}

// deleteTableOutput prints the deleted id in a table format
func deletedTableOutput(e *openlaneclient.DeleteTrustCenter) {
writer := tables.NewTableWriter(command.OutOrStdout(), "DeletedID")

writer.AddRow(e.DeleteTrustCenter.DeletedID)

writer.Render()
}
75 changes: 75 additions & 0 deletions cmd/cli/cmd/trustcenter/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package trustcenter

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "update an existing trustcenter",
Run: func(cmd *cobra.Command, args []string) {
err := update(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(updateCmd)

updateCmd.Flags().StringP("id", "i", "", "trustcenter id to update")

// command line flags for the update command
updateCmd.Flags().StringP("custom-domain-id", "d", "", "custom domain id for the trustcenter")
updateCmd.Flags().StringSliceP("tags", "t", []string{}, "tags associated with the trustcenter")
updateCmd.Flags().StringSliceP("append-tags", "a", []string{}, "append tags to the trustcenter")
}

// updateValidation validates the required fields for the command
func updateValidation() (id string, input openlaneclient.UpdateTrustCenterInput, err error) {
id = cmd.Config.String("id")
if id == "" {
return id, input, cmd.NewRequiredFieldMissingError("id")
}

customDomainID := cmd.Config.String("custom-domain-id")
if customDomainID != "" {
input.CustomDomainID = &customDomainID
}

tags := cmd.Config.Strings("tags")
if len(tags) > 0 {
input.Tags = tags
}

appendTags := cmd.Config.Strings("append-tags")
if len(appendTags) > 0 {
input.AppendTags = appendTags
}

return id, input, nil
}

// update an existing trustcenter in the platform
func update(ctx context.Context) error {
// attempt to setup with token, otherwise fall back to JWT with session
client, err := cmd.TokenAuth(ctx, cmd.Config)
if err != nil || client == nil {
// setup http client
client, err = cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)
}

id, input, err := updateValidation()
cobra.CheckErr(err)

o, err := client.UpdateTrustCenter(ctx, id, input)
cobra.CheckErr(err)

return consoleOutput(o)
}
1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
_ "github.com/theopenlane/core/cmd/cli/cmd/switchcontext"
_ "github.com/theopenlane/core/cmd/cli/cmd/task"
_ "github.com/theopenlane/core/cmd/cli/cmd/template"
_ "github.com/theopenlane/core/cmd/cli/cmd/trustcenter"
_ "github.com/theopenlane/core/cmd/cli/cmd/user"
_ "github.com/theopenlane/core/cmd/cli/cmd/usersetting"
_ "github.com/theopenlane/core/cmd/cli/cmd/version"
Expand Down
Loading