Skip to content

Commit

Permalink
Proper error output. Added type.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRailean committed Jan 17, 2023
1 parent e6264b7 commit a892bf3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
52 changes: 38 additions & 14 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ const (

type summary struct
{
Creating int32
Updating int32
Deleting int32
Total int32
Creating int32 `json:"creating"`
Updating int32 `json:"updating"`
Deleting int32 `json:"deleting"`
Total int32 `json:"total"`
}

type jsonOutput struct
{
Changes diff.EntityChanges
Summary summary
Warnings []string
Errors []string
Changes diff.EntityChanges `json:"changes"`
Summary summary `json:"summary"`
Warnings []string `json:"warnings"`
Errors []string `json:"errors"`
}

var jsonOut jsonOutput
Expand Down Expand Up @@ -94,12 +94,12 @@ func workspaceExists(ctx context.Context, config utils.KongClientConfig, workspa

func getWorkspaceName(workspaceFlag string, targetContent *file.Content) string {
if workspaceFlag != targetContent.Workspace && workspaceFlag != "" {
warning := fmt.Sprintf("Warning: Workspace '%v' specified via --workspace flag is "+
warning := fmt.Sprintf("Workspace '%v' specified via --workspace flag is "+
"different from workspace '%v' found in state file(s).", workspaceFlag, targetContent.Workspace)
if(isJsonOut){
jsonOut.Warnings = append(jsonOut.Warnings, warning)
}else{
cprint.DeletePrintf(warning+"\n")
cprint.DeletePrintf("Warning: "+warning+"\n")
}
return workspaceFlag
}
Expand All @@ -114,6 +114,11 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
isJsonOut = true
jsonOut.Errors = []string{}
jsonOut.Warnings = []string{}
jsonOut.Changes = diff.EntityChanges{
Creating: []diff.EntityState{},
Updating: []diff.EntityState{},
Deleting: []diff.EntityState{},
}
}
targetContent, err := file.GetContentFromFiles(filenames)
if err != nil {
Expand Down Expand Up @@ -231,7 +236,15 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
return err
}

cprint.CreatePrintln("creating workspace", wsConfig.Workspace)
if(isJsonOutput){
workspace := diff.EntityState{
Name: wsConfig.Workspace,
Type: "Workspace",
}
jsonOut.Changes.Creating = append(jsonOut.Changes.Creating, workspace)
}else{
cprint.CreatePrintln("Creating workspace", wsConfig.Workspace)
}
if !dry {
_, err = rootClient.Workspaces.Create(ctx, &kong.Workspace{Name: &wsConfig.Workspace})
if err != nil {
Expand All @@ -258,10 +271,17 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,

totalOps, err := performDiff(ctx, currentState, targetState, dry, parallelism, delay, kongClient)
if err != nil {

if(isJsonOut){
jsonOut.Errors = append(jsonOut.Errors, err.Error())
if errs, ok := err.(utils.ErrArray)
ok {
jsonOut.Errors = append(jsonOut.Errors, errs.ErrorList()...)
} else {
jsonOut.Errors = append(jsonOut.Errors, fmt.Sprintf("%v", err))
}
}else{
return err
}
return err
}
if diffCmdNonZeroExitCode && totalOps > 0 {
os.Exit(exitCodeDiffDetection)
Expand Down Expand Up @@ -336,7 +356,11 @@ func performDiff(ctx context.Context, currentState, targetState *state.KongState
}
totalOps := stats.CreateOps.Count() + stats.UpdateOps.Count() + stats.DeleteOps.Count()

jsonOut.Changes = changes
jsonOut.Changes = diff.EntityChanges{
Creating: append(jsonOut.Changes.Creating, changes.Creating...),
Updating: append(jsonOut.Changes.Updating, changes.Updating...),
Deleting: append(jsonOut.Changes.Deleting, changes.Deleting...),
}
jsonOut.Summary = summary{
Creating: stats.CreateOps.Count(),
Updating: stats.UpdateOps.Count(),
Expand Down
28 changes: 15 additions & 13 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ import (
"github.com/kong/go-kong/kong"
)

type entityState struct
type EntityState struct
{
Name string
OldState any
NewState any
Name string `json:"name"`
Type string `json:"type"`
OldState any `json:"oldState"`
NewState any `json:"newState"`
}

type EntityChanges struct
{
Creating []entityState
Updating []entityState
Deleting []entityState
Creating []EntityState `json:"creating"`
Updating []EntityState `json:"updating"`
Deleting []EntityState `json:"deleting"`
}

var errEnqueueFailed = errors.New("failed to queue event")
Expand Down Expand Up @@ -409,9 +410,9 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJsonOu
}

output := EntityChanges{
Creating: []entityState{},
Updating: []entityState{},
Deleting: []entityState{},
Creating: []EntityState{},
Updating: []EntityState{},
Deleting: []EntityState{},
}

errs := sc.Run(ctx, parallelism, func(e crud.Event) (crud.Arg, error) {
Expand All @@ -420,10 +421,11 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJsonOu

c := e.Obj.(state.ConsoleString)

item := entityState{
OldState: e.Obj,
NewState: e.OldObj,
item := EntityState{
OldState: e.OldObj,
NewState: e.Obj,
Name: c.Console(),
Type: string(e.Kind),
}
switch e.Op {
case crud.Create:
Expand Down
12 changes: 12 additions & 0 deletions utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func (e ErrArray) Error() string {
return res
}

func (e ErrArray) ErrorList() []string {
errList := []string{}
if len(e.Errors) == 0 {
return errList
}

for _, err := range e.Errors {
errList = append(errList, fmt.Sprintf("%v", err))
}
return errList
}

// KongClientConfig holds config details to use to talk to a Kong server.
type KongClientConfig struct {
Address string
Expand Down

0 comments on commit a892bf3

Please sign in to comment.