-
Notifications
You must be signed in to change notification settings - Fork 28
/
step.go
75 lines (59 loc) · 1.69 KB
/
step.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: Apache-2.0
package step
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type (
// config represents the settings required to create the engine that implements the StepInterface interface.
config struct {
// specifies to skip creating tables and indexes for the Step engine
SkipCreation bool
}
// engine represents the step functionality that implements the StepInterface interface.
engine struct {
// engine configuration settings used in step functions
config *config
ctx context.Context
// gorm.io/gorm database client used in step functions
//
// https://pkg.go.dev/gorm.io/gorm#DB
client *gorm.DB
// sirupsen/logrus logger used in step functions
//
// https://pkg.go.dev/github.com/sirupsen/logrus#Entry
logger *logrus.Entry
}
)
// New creates and returns a Vela service for integrating with steps in the database.
//
//nolint:revive // ignore returning unexported engine
func New(opts ...EngineOpt) (*engine, error) {
// create new Step engine
e := new(engine)
// create new fields
e.client = new(gorm.DB)
e.config = new(config)
e.logger = new(logrus.Entry)
// apply all provided configuration options
for _, opt := range opts {
err := opt(e)
if err != nil {
return nil, err
}
}
// check if we should skip creating step database objects
if e.config.SkipCreation {
e.logger.Warning("skipping creation of steps table in the database")
return e, nil
}
// create the steps table
err := e.CreateStepTable(e.ctx, e.client.Config.Dialector.Name())
if err != nil {
return nil, fmt.Errorf("unable to create %s table: %w", constants.TableStep, err)
}
return e, nil
}