-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblock.go
More file actions
executable file
·86 lines (75 loc) · 2.24 KB
/
Copy pathblock.go
File metadata and controls
executable file
·86 lines (75 loc) · 2.24 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package rubik
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
)
// Block is an interface that can be implemented to provide
// extended functionalities to rubik server
// Think of it as a plugin which can be attached to the
// rubik server and can be accessible throughout the
// lifecycle of rubik server.
//
// A Block can also be thought of as a dependency injected
// plugin and can be accessed in your controllers by
// calling rubik.GetBlock('BLOCK_NAME').
// Blocks requires you to implement a method called
// OnAttach. This method is called during rubik server
// bootstrapper is run and requires you to return an error
// if any complexity arises in for your block to function
type Block interface {
OnAttach(*App) error
}
// Plugin is executed plugins when RUBIK_ENV = ext.
// Blocks which requires access to server but does need the
// server to run. To run your extention block use
// `okrubik run --plugin`
type Plugin interface {
OnPlug(*App) error
Name() string
RunID() string
}
// App is a sandboxed object used by the external blocks of code
// to access some risk-free part of your rubik server
// For example:
// App do not have full access to your project config but it has
// the ability to decode the config that it needs for
// only this block of code to work
type App struct {
RouteTree
app rubik
blockName string
CurrentURL string
Project string
Args string
}
// Decode decodes the internal rubik server config into the struct
// that you provide. It returns error if the config is not
// un-marshalable OR if there is no config initialized by the given
// name parameter
func (sb *App) Decode(name string, target interface{}) error {
// check for target is pointer or not
val := sb.app.intermConfig.Get(name)
msg := fmt.Sprintf("AppDecodeError: block =[ %s ]= requires you to specify "+
"%s object inside your config/.toml file", sb.blockName, name)
if val == nil {
return errors.New(msg)
}
b, err := json.Marshal(val)
if err != nil {
return err
}
err = json.Unmarshal(b, target)
if err != nil {
return err
}
return nil
}
// Config get config by name
func (sb *App) Config(name string) interface{} {
if strings.Contains(name, ".") {
return nil
}
return sb.app.intermConfig.Get(name)
}