-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_di_test.go
60 lines (50 loc) · 1.52 KB
/
example_di_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This example demonstrates a pattern for injecting dependencies into your command handlers.
package xflags
import (
"context"
"database/sql"
"fmt"
"os"
)
// DBClient is our main command with two subcommands.
var DBClient = NewCommand("db-client", "query a database").
Subcommands(
GetCommand,
DeleteCommand,
)
// GetCommand is a subcommand with dependencies injected into the handler by Wrap.
var GetCommand = NewCommand("get", "Get DB resources").
HandleFunc(Wrap(Get))
// DeleteCommand is a subcommand with dependecnies injected into the handler by Wrap.
var DeleteCommand = NewCommand("delete", "Delete DB resources").
HandleFunc(Wrap(Delete))
// Wrap returns a HandlerFunc that initialises common dependencies for command handlers and then
// injects them into fn.
func Wrap(fn func(ctx context.Context, db *sql.DB) error) HandlerFunc {
return func(args []string) (exitCode int) {
// build a context
ctx := context.Background()
// build a database connection
var db *sql.DB = nil
// call the handler with all dependencies
if err := fn(ctx, db); err != nil {
fmt.Fprint(os.Stderr, err)
return 1
}
return 0
}
}
// Get is a custom handler for GetCommand
func Get(ctx context.Context, db *sql.DB) error {
fmt.Println("Issued a get query")
return nil
}
// Delete is a custom handler for DeleteCommand
func Delete(ctx context.Context, db *sql.DB) error {
fmt.Println("Issued a delete query")
return nil
}
func Example_dependencyInjection() {
RunWithArgs(DBClient, "get")
// Output: Issued a get query
}