-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: import relevant deck packages
Copy the konnect, cprint, crud, diff, dump, file, scripts, state, types, and utils packages from deck into this repository. The source was imported from [email protected]: https://github.com/Kong/deck/releases/tag/v1.29.2
- Loading branch information
Showing
186 changed files
with
38,855 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cprint | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
var ( | ||
// mu is used to synchronize writes from multiple goroutines. | ||
mu sync.Mutex | ||
// DisableOutput disables all output. | ||
DisableOutput bool | ||
) | ||
|
||
func conditionalPrintf(fn func(string, ...interface{}), format string, a ...interface{}) { | ||
if DisableOutput { | ||
return | ||
} | ||
mu.Lock() | ||
defer mu.Unlock() | ||
fn(format, a...) | ||
} | ||
|
||
func conditionalPrintln(fn func(...interface{}), a ...interface{}) { | ||
if DisableOutput { | ||
return | ||
} | ||
mu.Lock() | ||
defer mu.Unlock() | ||
fn(a...) | ||
} | ||
|
||
var ( | ||
createPrintf = color.New(color.FgGreen).PrintfFunc() | ||
deletePrintf = color.New(color.FgRed).PrintfFunc() | ||
updatePrintf = color.New(color.FgYellow).PrintfFunc() | ||
|
||
// CreatePrintf is fmt.Printf with red as foreground color. | ||
CreatePrintf = func(format string, a ...interface{}) { | ||
conditionalPrintf(createPrintf, format, a...) | ||
} | ||
|
||
// DeletePrintf is fmt.Printf with green as foreground color. | ||
DeletePrintf = func(format string, a ...interface{}) { | ||
conditionalPrintf(deletePrintf, format, a...) | ||
} | ||
|
||
// UpdatePrintf is fmt.Printf with yellow as foreground color. | ||
UpdatePrintf = func(format string, a ...interface{}) { | ||
conditionalPrintf(updatePrintf, format, a...) | ||
} | ||
|
||
createPrintln = color.New(color.FgGreen).PrintlnFunc() | ||
deletePrintln = color.New(color.FgRed).PrintlnFunc() | ||
updatePrintln = color.New(color.FgYellow).PrintlnFunc() | ||
bluePrintln = color.New(color.BgBlue).PrintlnFunc() | ||
|
||
// CreatePrintln is fmt.Println with red as foreground color. | ||
CreatePrintln = func(a ...interface{}) { | ||
conditionalPrintln(createPrintln, a...) | ||
} | ||
|
||
// DeletePrintln is fmt.Println with green as foreground color. | ||
DeletePrintln = func(a ...interface{}) { | ||
conditionalPrintln(deletePrintln, a...) | ||
} | ||
|
||
// UpdatePrintln is fmt.Println with yellow as foreground color. | ||
UpdatePrintln = func(a ...interface{}) { | ||
conditionalPrintln(updatePrintln, a...) | ||
} | ||
|
||
BluePrintLn = func(a ...interface{}) { | ||
conditionalPrintln(bluePrintln, a...) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cprint | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/fatih/color" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// captureOutput captures color.Output and returns the recorded output as | ||
// f runs. | ||
// It is not thread-safe. | ||
func captureOutput(f func()) string { | ||
backupOutput := color.Output | ||
defer func() { | ||
color.Output = backupOutput | ||
}() | ||
var out bytes.Buffer | ||
color.Output = &out | ||
f() | ||
return out.String() | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
backup := color.NoColor | ||
color.NoColor = false | ||
exitVal := m.Run() | ||
color.NoColor = backup | ||
os.Exit(exitVal) | ||
} | ||
|
||
func TestPrint(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
DisableOutput bool | ||
Run func() | ||
Expected string | ||
}{ | ||
{ | ||
name: "println prints colored output", | ||
DisableOutput: false, | ||
Run: func() { | ||
CreatePrintln("foo") | ||
UpdatePrintln("bar") | ||
DeletePrintln("fubaz") | ||
}, | ||
Expected: "\x1b[32mfoo\n\x1b[0m\x1b[33mbar\n\x1b[0m\x1b[31mfubaz\n\x1b[0m", | ||
}, | ||
{ | ||
name: "println doesn't output anything when disabled", | ||
DisableOutput: true, | ||
Run: func() { | ||
CreatePrintln("foo") | ||
UpdatePrintln("bar") | ||
DeletePrintln("fubaz") | ||
}, | ||
Expected: "", | ||
}, | ||
{ | ||
name: "printf prints colored output", | ||
DisableOutput: false, | ||
Run: func() { | ||
CreatePrintf("%s", "foo") | ||
UpdatePrintf("%s", "bar") | ||
DeletePrintf("%s", "fubaz") | ||
}, | ||
Expected: "\x1b[32mfoo\x1b[0m\x1b[33mbar\x1b[0m\x1b[31mfubaz\x1b[0m", | ||
}, | ||
{ | ||
name: "printf doesn't output anything when disabled", | ||
DisableOutput: true, | ||
Run: func() { | ||
CreatePrintln("foo") | ||
UpdatePrintln("bar") | ||
DeletePrintln("fubaz") | ||
}, | ||
Expected: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
DisableOutput = tt.DisableOutput | ||
defer func() { | ||
DisableOutput = false | ||
}() | ||
|
||
output := captureOutput(func() { | ||
tt.Run() | ||
}) | ||
assert.Equal(t, tt.Expected, output) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package crud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// Kind represents Kind of an entity or object. | ||
type Kind string | ||
|
||
// Registry can hold Kinds and their respective CRUD operations. | ||
type Registry struct { | ||
types map[Kind]Actions | ||
} | ||
|
||
func (r *Registry) typesMap() map[Kind]Actions { | ||
if r.types == nil { | ||
r.types = make(map[Kind]Actions) | ||
} | ||
return r.types | ||
} | ||
|
||
// Register a kind with actions. | ||
// An error will be returned if kind was previously registered. | ||
func (r *Registry) Register(kind Kind, a Actions) error { | ||
if kind == "" { | ||
return fmt.Errorf("kind cannot be empty") | ||
} | ||
m := r.typesMap() | ||
if _, ok := m[kind]; ok { | ||
return fmt.Errorf("kind %q already registered", kind) | ||
} | ||
m[kind] = a | ||
return nil | ||
} | ||
|
||
// MustRegister is same as Register but panics on error. | ||
func (r *Registry) MustRegister(kind Kind, a Actions) { | ||
err := r.Register(kind, a) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Get returns actions associated with kind. | ||
// An error will be returned if kind was never registered. | ||
func (r *Registry) Get(kind Kind) (Actions, error) { | ||
if kind == "" { | ||
return nil, fmt.Errorf("kind cannot be empty") | ||
} | ||
m := r.typesMap() | ||
a, ok := m[kind] | ||
if !ok { | ||
return nil, fmt.Errorf("kind %q is not registered", kind) | ||
} | ||
return a, nil | ||
} | ||
|
||
// Create calls the registered create action of kind with args | ||
// and returns the result and error (if any). | ||
func (r *Registry) Create(ctx context.Context, kind Kind, args ...Arg) (Arg, error) { | ||
a, err := r.Get(kind) | ||
if err != nil { | ||
return nil, fmt.Errorf("create failed: %w", err) | ||
} | ||
|
||
res, err := a.Create(ctx, args...) | ||
if err != nil { | ||
return nil, fmt.Errorf("create failed: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
// Update calls the registered update action of kind with args | ||
// and returns the result and error (if any). | ||
func (r *Registry) Update(ctx context.Context, kind Kind, args ...Arg) (Arg, error) { | ||
a, err := r.Get(kind) | ||
if err != nil { | ||
return nil, fmt.Errorf("update failed: %w", err) | ||
} | ||
|
||
res, err := a.Update(ctx, args...) | ||
if err != nil { | ||
return nil, fmt.Errorf("update failed: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
// Delete calls the registered delete action of kind with args | ||
// and returns the result and error (if any). | ||
func (r *Registry) Delete(ctx context.Context, kind Kind, args ...Arg) (Arg, error) { | ||
a, err := r.Get(kind) | ||
if err != nil { | ||
return nil, fmt.Errorf("delete failed: %w", err) | ||
} | ||
|
||
res, err := a.Delete(ctx, args...) | ||
if err != nil { | ||
return nil, fmt.Errorf("delete failed: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
// Do calls an action based on op with args and returns the result and error. | ||
func (r *Registry) Do(ctx context.Context, kind Kind, op Op, args ...Arg) (Arg, error) { | ||
a, err := r.Get(kind) | ||
if err != nil { | ||
return nil, fmt.Errorf("%v failed: %w", op, err) | ||
} | ||
|
||
var res Arg | ||
|
||
switch op.name { | ||
case Create.name: | ||
res, err = a.Create(ctx, args...) | ||
case Update.name: | ||
res, err = a.Update(ctx, args...) | ||
case Delete.name: | ||
res, err = a.Delete(ctx, args...) | ||
default: | ||
return nil, fmt.Errorf("unknown operation: %s", op.name) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} |
Oops, something went wrong.