diff --git a/api/docs/docs.go b/api/docs/docs.go index bc7029b..619dc0f 100644 --- a/api/docs/docs.go +++ b/api/docs/docs.go @@ -33,7 +33,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Channel" + "$ref": "#/definitions/model.Channel" } } }, @@ -68,7 +68,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Channel" + "$ref": "#/definitions/model.Channel" } }, "default": { @@ -95,7 +95,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Decoder" + "$ref": "#/definitions/model.Decoder" } } }, @@ -130,7 +130,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Decoder" + "$ref": "#/definitions/model.Decoder" } }, "default": { @@ -157,7 +157,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PipelineListItem" + "$ref": "#/definitions/model.PipelineListItem" } } }, @@ -192,7 +192,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.PipelineListItem" + "$ref": "#/definitions/model.PipelineListItem" } }, "default": { @@ -246,7 +246,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PipelineRunListRow" + "$ref": "#/definitions/model.PipelineRunListRow" } } }, @@ -274,7 +274,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PostHook" + "$ref": "#/definitions/model.PostHook" } } }, @@ -309,7 +309,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.PostHook" + "$ref": "#/definitions/model.PostHook" } }, "default": { @@ -356,7 +356,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Run" + "$ref": "#/definitions/model.Run" } } }, @@ -391,7 +391,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Run" + "$ref": "#/definitions/model.Run" } }, "default": { @@ -438,7 +438,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Task" + "$ref": "#/definitions/model.Task" } } }, @@ -473,7 +473,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Task" + "$ref": "#/definitions/model.Task" } }, "default": { @@ -512,7 +512,7 @@ const docTemplate = `{ } } }, - "store.Channel": { + "model.Channel": { "type": "object", "properties": { "decoder_name": { @@ -523,7 +523,7 @@ const docTemplate = `{ } } }, - "store.Decoder": { + "model.Decoder": { "type": "object", "properties": { "name": { @@ -537,7 +537,7 @@ const docTemplate = `{ } } }, - "store.PipelineListItem": { + "model.PipelineListItem": { "type": "object", "properties": { "channels": { @@ -560,7 +560,7 @@ const docTemplate = `{ } } }, - "store.PipelineRunListRow": { + "model.PipelineRunListRow": { "type": "object", "properties": { "dest_branch": { @@ -597,7 +597,7 @@ const docTemplate = `{ } } }, - "store.PostHook": { + "model.PostHook": { "type": "object", "properties": { "name": { @@ -611,7 +611,7 @@ const docTemplate = `{ } } }, - "store.Run": { + "model.Run": { "type": "object", "properties": { "dest_branch": { @@ -645,7 +645,7 @@ const docTemplate = `{ } } }, - "store.Task": { + "model.Task": { "type": "object", "properties": { "dest_branch": { diff --git a/api/docs/swagger.json b/api/docs/swagger.json index bb9dca7..5f3ca79 100644 --- a/api/docs/swagger.json +++ b/api/docs/swagger.json @@ -22,7 +22,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Channel" + "$ref": "#/definitions/model.Channel" } } }, @@ -57,7 +57,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Channel" + "$ref": "#/definitions/model.Channel" } }, "default": { @@ -84,7 +84,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Decoder" + "$ref": "#/definitions/model.Decoder" } } }, @@ -119,7 +119,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Decoder" + "$ref": "#/definitions/model.Decoder" } }, "default": { @@ -146,7 +146,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PipelineListItem" + "$ref": "#/definitions/model.PipelineListItem" } } }, @@ -181,7 +181,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.PipelineListItem" + "$ref": "#/definitions/model.PipelineListItem" } }, "default": { @@ -235,7 +235,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PipelineRunListRow" + "$ref": "#/definitions/model.PipelineRunListRow" } } }, @@ -263,7 +263,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.PostHook" + "$ref": "#/definitions/model.PostHook" } } }, @@ -298,7 +298,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.PostHook" + "$ref": "#/definitions/model.PostHook" } }, "default": { @@ -345,7 +345,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Run" + "$ref": "#/definitions/model.Run" } } }, @@ -380,7 +380,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Run" + "$ref": "#/definitions/model.Run" } }, "default": { @@ -427,7 +427,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/store.Task" + "$ref": "#/definitions/model.Task" } } }, @@ -462,7 +462,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/store.Task" + "$ref": "#/definitions/model.Task" } }, "default": { @@ -501,7 +501,7 @@ } } }, - "store.Channel": { + "model.Channel": { "type": "object", "properties": { "decoder_name": { @@ -512,7 +512,7 @@ } } }, - "store.Decoder": { + "model.Decoder": { "type": "object", "properties": { "name": { @@ -526,7 +526,7 @@ } } }, - "store.PipelineListItem": { + "model.PipelineListItem": { "type": "object", "properties": { "channels": { @@ -549,7 +549,7 @@ } } }, - "store.PipelineRunListRow": { + "model.PipelineRunListRow": { "type": "object", "properties": { "dest_branch": { @@ -586,7 +586,7 @@ } } }, - "store.PostHook": { + "model.PostHook": { "type": "object", "properties": { "name": { @@ -600,7 +600,7 @@ } } }, - "store.Run": { + "model.Run": { "type": "object", "properties": { "dest_branch": { @@ -634,7 +634,7 @@ } } }, - "store.Task": { + "model.Task": { "type": "object", "properties": { "dest_branch": { diff --git a/api/docs/swagger.yaml b/api/docs/swagger.yaml index 9fa2615..0e181c0 100644 --- a/api/docs/swagger.yaml +++ b/api/docs/swagger.yaml @@ -15,14 +15,14 @@ definitions: repo: type: string type: object - store.Channel: + model.Channel: properties: decoder_name: type: string name: type: string type: object - store.Decoder: + model.Decoder: properties: name: type: string @@ -31,7 +31,7 @@ definitions: type: integer type: array type: object - store.PipelineListItem: + model.PipelineListItem: properties: channels: items: @@ -46,7 +46,7 @@ definitions: repo_uri: $ref: '#/definitions/git.PackageURI' type: object - store.PipelineRunListRow: + model.PipelineRunListRow: properties: dest_branch: type: string @@ -71,7 +71,7 @@ definitions: type: string type: array type: object - store.PostHook: + model.PostHook: properties: name: type: string @@ -80,7 +80,7 @@ definitions: type: integer type: array type: object - store.Run: + model.Run: properties: dest_branch: type: string @@ -103,7 +103,7 @@ definitions: type: string type: array type: object - store.Task: + model.Task: properties: dest_branch: type: string @@ -144,7 +144,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.Channel' + $ref: '#/definitions/model.Channel' type: array default: description: Error @@ -167,7 +167,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.Channel' + $ref: '#/definitions/model.Channel' default: description: Error schema: @@ -184,7 +184,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.Decoder' + $ref: '#/definitions/model.Decoder' type: array default: description: Error @@ -207,7 +207,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.Decoder' + $ref: '#/definitions/model.Decoder' default: description: Error schema: @@ -224,7 +224,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.PipelineListItem' + $ref: '#/definitions/model.PipelineListItem' type: array default: description: Error @@ -247,7 +247,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.PipelineListItem' + $ref: '#/definitions/model.PipelineListItem' default: description: Error schema: @@ -282,7 +282,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.PipelineRunListRow' + $ref: '#/definitions/model.PipelineRunListRow' type: array default: description: Error @@ -300,7 +300,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.PostHook' + $ref: '#/definitions/model.PostHook' type: array default: description: Error @@ -323,7 +323,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.PostHook' + $ref: '#/definitions/model.PostHook' default: description: Error schema: @@ -353,7 +353,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.Run' + $ref: '#/definitions/model.Run' type: array default: description: Error @@ -376,7 +376,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.Run' + $ref: '#/definitions/model.Run' default: description: Error schema: @@ -406,7 +406,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/store.Task' + $ref: '#/definitions/model.Task' type: array default: description: Error @@ -429,7 +429,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/store.Task' + $ref: '#/definitions/model.Task' default: description: Error schema: diff --git a/api/handler.go b/api/handler.go index c0dafec..6bac6ae 100644 --- a/api/handler.go +++ b/api/handler.go @@ -12,12 +12,12 @@ import ( httpSwagger "github.com/swaggo/http-swagger/v2" "github.com/bluebrown/kobold/api/docs" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" ) // @license.name BSD-3-Clause type WebAPI struct { - q *store.Queries + q *model.Queries router *mux.Router } @@ -25,7 +25,7 @@ type WebAPI struct { // on, in order to generate correct swagger docs. It will not register routes on // the basepath, the caller should remove the basepath from the mux before // calling ServeHTTP -func New(basepath string, q *store.Queries) *WebAPI { +func New(basepath string, q *model.Queries) *WebAPI { api := WebAPI{q, mux.NewRouter()} docs.SwaggerInfo.Title = "Kobold API" @@ -105,7 +105,7 @@ func (api *WebAPI) respond(w http.ResponseWriter, r *http.Request, data any, err // @Tags channels // @Produce json // @Param name path string true "channel name" -// @Success 200 {object} store.Channel +// @Success 200 {object} model.Channel // @Response default {object} errorMsg "Error" func (api *WebAPI) GetChannel(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -119,7 +119,7 @@ func (api *WebAPI) GetChannel(w http.ResponseWriter, r *http.Request) { // @Summary get a list of channels // @Tags channels // @Produce json -// @Success 200 {array} store.Channel +// @Success 200 {array} model.Channel // @Response default {object} errorMsg "Error" func (api *WebAPI) GetChannelList(w http.ResponseWriter, r *http.Request) { d, err := api.q.ChannelList(r.Context()) @@ -133,7 +133,7 @@ func (api *WebAPI) GetChannelList(w http.ResponseWriter, r *http.Request) { // @Tags decoders // @Produce json // @Param name path string true "decoder name" -// @Success 200 {object} store.Decoder +// @Success 200 {object} model.Decoder // @Response default {object} errorMsg "Error" func (api *WebAPI) GetDecoder(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -147,7 +147,7 @@ func (api *WebAPI) GetDecoder(w http.ResponseWriter, r *http.Request) { // @Summary get a list of decoders // @Tags decoders // @Produce json -// @Success 200 {array} store.Decoder +// @Success 200 {array} model.Decoder // @Response default {object} errorMsg "Error" func (api *WebAPI) GetDecoderList(w http.ResponseWriter, r *http.Request) { d, err := api.q.DecoderList(r.Context()) @@ -161,7 +161,7 @@ func (api *WebAPI) GetDecoderList(w http.ResponseWriter, r *http.Request) { // @Tags pipelines // @Produce json // @Param name path string true "pipeline name" -// @Success 200 {object} store.PipelineListItem +// @Success 200 {object} model.PipelineListItem // @Response default {object} errorMsg "Error" func (api *WebAPI) GetPipeline(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -175,7 +175,7 @@ func (api *WebAPI) GetPipeline(w http.ResponseWriter, r *http.Request) { // @Summary get a list of pipelines // @Tags pipelines // @Produce json -// @Success 200 {array} store.PipelineListItem +// @Success 200 {array} model.PipelineListItem // @Response default {object} errorMsg "Error" func (api *WebAPI) GetPipelineList(w http.ResponseWriter, r *http.Request) { d, err := api.q.PipelineList(r.Context()) @@ -192,10 +192,10 @@ func (api *WebAPI) GetPipelineList(w http.ResponseWriter, r *http.Request) { // @Param status query string false "run status" // @Param limit query int false "limit" // @Param offset query int false "offset" -// @Success 200 {array} store.PipelineRunListRow +// @Success 200 {array} model.PipelineRunListRow // @Response default {object} errorMsg "Error" func (api *WebAPI) GetPipelineRunList(w http.ResponseWriter, r *http.Request) { - var params store.PipelineRunListParams + var params model.PipelineRunListParams if err := decoder.Decode(¶ms, r.URL.Query()); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -223,7 +223,7 @@ func (api *WebAPI) GetPipelineRunList(w http.ResponseWriter, r *http.Request) { // @Tags posthooks // @Produce json // @Param name path string true "posthook name" -// @Success 200 {object} store.PostHook +// @Success 200 {object} model.PostHook // @Response default {object} errorMsg "Error" func (api *WebAPI) GetPostHook(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -237,7 +237,7 @@ func (api *WebAPI) GetPostHook(w http.ResponseWriter, r *http.Request) { // @Summary get a list of posthooks // @Tags posthooks // @Produce json -// @Success 200 {array} store.PostHook +// @Success 200 {array} model.PostHook // @Response default {object} errorMsg "Error" func (api *WebAPI) GetPostHookList(w http.ResponseWriter, r *http.Request) { d, err := api.q.PostHookList(r.Context()) @@ -251,7 +251,7 @@ func (api *WebAPI) GetPostHookList(w http.ResponseWriter, r *http.Request) { // @Tags tasks // @Produce json // @Param id path string true "task id" -// @Success 200 {object} store.Task +// @Success 200 {object} model.Task // @Response default {object} errorMsg "Error" func (a *WebAPI) GetTask(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -268,10 +268,10 @@ func (a *WebAPI) GetTask(w http.ResponseWriter, r *http.Request) { // @Param status query string false "task status" // @Param limit query int false "limit" // @Param offset query int false "offset" -// @Success 200 {array} store.Task +// @Success 200 {array} model.Task // @Response default {object} errorMsg "Error" func (a *WebAPI) GetTaskList(w http.ResponseWriter, r *http.Request) { - var params store.TaskListParams + var params model.TaskListParams if err := decoder.Decode(¶ms, r.URL.Query()); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -297,7 +297,7 @@ func (a *WebAPI) GetTaskList(w http.ResponseWriter, r *http.Request) { // @Tags runs // @Produce json // @Param id path string true "run fingerprint" -// @Success 200 {object} store.Run +// @Success 200 {object} model.Run // @Response default {object} errorMsg "Error" func (api *WebAPI) GetRun(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] @@ -314,10 +314,10 @@ func (api *WebAPI) GetRun(w http.ResponseWriter, r *http.Request) { // @Param status query string false "run status" // @Param limit query int false "limit" // @Param offset query int false "offset" -// @Success 200 {array} store.Run +// @Success 200 {array} model.Run // @Response default {object} errorMsg "Error" func (api *WebAPI) GetRunList(w http.ResponseWriter, r *http.Request) { - var params store.RunListParams + var params model.RunListParams if err := decoder.Decode(¶ms, r.URL.Query()); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/build/sqlc.yaml b/build/sqlc.yaml index 3407ee7..14ad707 100644 --- a/build/sqlc.yaml +++ b/build/sqlc.yaml @@ -1,12 +1,12 @@ version: "2" sql: - engine: sqlite - queries: ../sql/*.query.sql - schema: ../sql/*.schema.sql + queries: ../store/schema/*.query.sql + schema: ../store/schema/*.schema.sql gen: go: - package: store - out: ../store/ + package: model + out: ../store/model/ emit_json_tags: true emit_empty_slices: true emit_sql_as_comment: true @@ -27,20 +27,20 @@ sql: - column: "*.msgs" go_type: - import: github.com/bluebrown/kobold/dbutil - package: dbutil + import: github.com/bluebrown/kobold/store + package: store type: SliceText - column: "*.warnings" go_type: - import: github.com/bluebrown/kobold/dbutil - package: dbutil + import: github.com/bluebrown/kobold/store + package: store type: SliceText - column: "*.task_ids" go_type: - import: github.com/bluebrown/kobold/dbutil - package: dbutil + import: github.com/bluebrown/kobold/store + package: store type: SliceText - column: "*.fingerprint" @@ -49,6 +49,6 @@ sql: - column: "*.channels" go_type: - import: github.com/bluebrown/kobold/dbutil - package: dbutil + import: github.com/bluebrown/kobold/store + package: store type: JsonArray diff --git a/cmd/cli/main.go b/cmd/cli/main.go index a27530e..53d34bd 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -11,14 +11,14 @@ import ( _ "modernc.org/sqlite" "github.com/bluebrown/kobold/config" - "github.com/bluebrown/kobold/dbutil" - ts "github.com/bluebrown/kobold/sql" + "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/schema" "github.com/bluebrown/kobold/task" ) func init() { - dbutil.MustMakeUUID() - dbutil.MustMakeSha1() + store.MustMakeUUID() + store.MustMakeSha1() } func main() { @@ -60,7 +60,7 @@ func run(ctx context.Context, args []string, env []string, input io.Reader) erro return fmt.Errorf("parse args: %w", err) } - model, err := config.Configure(ctx, *opts, ts.TaskSchema) + model, err := config.Configure(ctx, *opts, schema.TaskSchema) if err != nil { return fmt.Errorf("configure: %w", err) } diff --git a/cmd/server/main.go b/cmd/server/main.go index 1e00d76..e72e3a8 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -10,21 +10,21 @@ import ( "os/signal" "time" + "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/sync/errgroup" _ "modernc.org/sqlite" "github.com/bluebrown/kobold/api" "github.com/bluebrown/kobold/config" - "github.com/bluebrown/kobold/dbutil" - ts "github.com/bluebrown/kobold/sql" + "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/schema" "github.com/bluebrown/kobold/task" "github.com/bluebrown/kobold/webhook" - "github.com/prometheus/client_golang/prometheus/promhttp" ) func init() { - dbutil.MustMakeUUID() - dbutil.MustMakeSha1() + store.MustMakeUUID() + store.MustMakeSha1() } func main() { @@ -64,7 +64,7 @@ func run(ctx context.Context, args []string, env []string) error { return fmt.Errorf("parse args: %w", err) } - model, err := config.Configure(ctx, *opts, ts.TaskSchema) + model, err := config.Configure(ctx, *opts, schema.TaskSchema) if err != nil { return fmt.Errorf("configure: %w", err) } diff --git a/config/builtins.go b/config/builtins.go index 52b78df..3955648 100644 --- a/config/builtins.go +++ b/config/builtins.go @@ -4,13 +4,13 @@ import ( "context" "fmt" - "github.com/bluebrown/kobold/builtin" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/plugin/builtin" + "github.com/bluebrown/kobold/store/model" ) -func ApplyBuiltins(ctx context.Context, q *store.Queries) error { +func ApplyBuiltins(ctx context.Context, q *model.Queries) error { for _, d := range builtin.Decoders() { - if err := q.DecoderPut(ctx, store.DecoderPutParams{ + if err := q.DecoderPut(ctx, model.DecoderPutParams{ Name: d.Name, Script: []byte(d.Script), }); err != nil { @@ -19,7 +19,7 @@ func ApplyBuiltins(ctx context.Context, q *store.Queries) error { } for _, p := range builtin.PostHooks() { - if err := q.PostHookPut(ctx, store.PostHookPutParams{ + if err := q.PostHookPut(ctx, model.PostHookPutParams{ Name: p.Name, Script: []byte(p.Script), }); err != nil { diff --git a/config/config.go b/config/config.go index 6bd39c1..0555c5c 100644 --- a/config/config.go +++ b/config/config.go @@ -6,7 +6,7 @@ import ( "fmt" "github.com/bluebrown/kobold/git" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" "github.com/volatiletech/null/v8" ) @@ -41,9 +41,9 @@ type Config struct { Decoders []Decoder `toml:"decoder"` } -func (cfg *Config) Apply(ctx context.Context, q *store.Queries) error { +func (cfg *Config) Apply(ctx context.Context, q *model.Queries) error { for _, d := range cfg.Decoders { - if err := q.DecoderPut(ctx, store.DecoderPutParams{ + if err := q.DecoderPut(ctx, model.DecoderPutParams{ Name: d.Name, Script: []byte(d.Script), }); err != nil { @@ -52,7 +52,7 @@ func (cfg *Config) Apply(ctx context.Context, q *store.Queries) error { } for _, p := range cfg.PostHooks { - if err := q.PostHookPut(ctx, store.PostHookPutParams{ + if err := q.PostHookPut(ctx, model.PostHookPutParams{ Name: p.Name, Script: []byte(p.Script), }); err != nil { @@ -61,15 +61,15 @@ func (cfg *Config) Apply(ctx context.Context, q *store.Queries) error { } for _, c := range cfg.Channels { - // ch := store.ChannelPutParams{Name: c.Name, DecoderName: dbutil.NullString{String: c.Decoder, Valid: c.Decoder != ""}} - ch := store.ChannelPutParams{Name: c.Name, DecoderName: null.NewString(c.Decoder, c.Decoder != "")} + // ch := model.ChannelPutParams{Name: c.Name, DecoderName: store.NullString{String: c.Decoder, Valid: c.Decoder != ""}} + ch := model.ChannelPutParams{Name: c.Name, DecoderName: null.NewString(c.Decoder, c.Decoder != "")} if err := q.ChannelPut(ctx, ch); err != nil { return fmt.Errorf("create channel %q: %w", c.Name, err) } } for _, p := range cfg.Pipelines { - if err := q.PipelinePut(ctx, store.PipelinePutParams{ + if err := q.PipelinePut(ctx, model.PipelinePutParams{ Name: p.Name, RepoUri: p.RepoURI, DestBranch: null.NewString(p.DestBranch, p.DestBranch != ""), @@ -79,7 +79,7 @@ func (cfg *Config) Apply(ctx context.Context, q *store.Queries) error { } for _, c := range p.Channels { - if err := q.SubscriptionPut(ctx, store.SubscriptionPutParams{ + if err := q.SubscriptionPut(ctx, model.SubscriptionPutParams{ PipelineName: p.Name, ChannelName: c, }); err != nil { diff --git a/config/configure.go b/config/configure.go index c79abc8..64501a6 100644 --- a/config/configure.go +++ b/config/configure.go @@ -7,11 +7,11 @@ import ( "log/slog" "net/url" - ksql "github.com/bluebrown/kobold/sql" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" + ksql "github.com/bluebrown/kobold/store/schema" ) -func Configure(ctx context.Context, opts options, schemas ...[]byte) (*store.Queries, error) { +func Configure(ctx context.Context, opts options, schemas ...[]byte) (*model.Queries, error) { SetLog(opts.w, opts.logfmt, slog.Level(opts.loglvl)) sqliteDSN := "file:" + opts.dbfile + "?" + query(UsePragmas) @@ -32,7 +32,7 @@ func Configure(ctx context.Context, opts options, schemas ...[]byte) (*store.Que } } - model := store.New(db) + model := model.New(db) if err := ApplyBuiltins(ctx, model); err != nil { return nil, fmt.Errorf("apply builtins: %w", err) diff --git a/builtin/embed.go b/plugin/builtin/embed.go similarity index 100% rename from builtin/embed.go rename to plugin/builtin/embed.go diff --git a/builtin/starlark/decoder.distribution@v1.star b/plugin/builtin/starlark/decoder.distribution@v1.star similarity index 100% rename from builtin/starlark/decoder.distribution@v1.star rename to plugin/builtin/starlark/decoder.distribution@v1.star diff --git a/builtin/starlark/decoder.dockerhub@v1.star b/plugin/builtin/starlark/decoder.dockerhub@v1.star similarity index 100% rename from builtin/starlark/decoder.dockerhub@v1.star rename to plugin/builtin/starlark/decoder.dockerhub@v1.star diff --git a/builtin/starlark/decoder.lines@v1.star b/plugin/builtin/starlark/decoder.lines@v1.star similarity index 100% rename from builtin/starlark/decoder.lines@v1.star rename to plugin/builtin/starlark/decoder.lines@v1.star diff --git a/builtin/starlark/posthook.ado-pr@v1.star b/plugin/builtin/starlark/posthook.ado-pr@v1.star similarity index 100% rename from builtin/starlark/posthook.ado-pr@v1.star rename to plugin/builtin/starlark/posthook.ado-pr@v1.star diff --git a/builtin/starlark/posthook.gitea-pr@v1.star b/plugin/builtin/starlark/posthook.gitea-pr@v1.star similarity index 100% rename from builtin/starlark/posthook.gitea-pr@v1.star rename to plugin/builtin/starlark/posthook.gitea-pr@v1.star diff --git a/builtin/starlark/posthook.github-pr@v1.star b/plugin/builtin/starlark/posthook.github-pr@v1.star similarity index 100% rename from builtin/starlark/posthook.github-pr@v1.star rename to plugin/builtin/starlark/posthook.github-pr@v1.star diff --git a/builtin/starlark/posthook.print@v1.star b/plugin/builtin/starlark/posthook.print@v1.star similarity index 100% rename from builtin/starlark/posthook.print@v1.star rename to plugin/builtin/starlark/posthook.print@v1.star diff --git a/plugin/decoder.go b/plugin/decoder.go new file mode 100644 index 0000000..acb2885 --- /dev/null +++ b/plugin/decoder.go @@ -0,0 +1,26 @@ +package plugin + +import ( + "fmt" + + "go.starlark.net/starlark" +) + +type decoder struct { +} + +func NewDecoderRunner() *decoder { + return &decoder{} +} + +func (d *decoder) Decode(name string, script []byte, data []byte) ([]string, error) { + res, err := runMain(defaultThread(name), name, script, d.args(data), nil) + if err != nil { + return nil, fmt.Errorf("run main: %w", err) + } + return asStringSlice(res) +} + +func (d *decoder) args(data []byte) starlark.Tuple { + return starlark.Tuple{starlark.String(data)} +} diff --git a/task/hook.go b/plugin/hook.go similarity index 61% rename from task/hook.go rename to plugin/hook.go index 06ed564..f1cb801 100644 --- a/task/hook.go +++ b/plugin/hook.go @@ -1,4 +1,4 @@ -package task +package plugin import ( "fmt" @@ -7,28 +7,25 @@ import ( "go.starlark.net/starlark" - "github.com/bluebrown/kobold/starutil" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" ) -type StarlarkHookRunner struct { +type PostHookRunner struct { hostEnv *starlark.Dict } -var _ HookRunner = (*StarlarkHookRunner)(nil) - -func NewStarlarkPostHook() *StarlarkHookRunner { - return &StarlarkHookRunner{ - hostEnv: starutil.EnvToStarlarkDict(os.Environ()), +func NewPostHookRunner() *PostHookRunner { + return &PostHookRunner{ + hostEnv: envToStarlarkDict(os.Environ()), } } -func (d *StarlarkHookRunner) Run(group store.TaskGroup, msg string, changes []string, warnings []string) error { +func (d *PostHookRunner) Run(group model.TaskGroup, msg string, changes []string, warnings []string) error { if group.PostHook == nil { return nil } - res, err := starutil.RunMain(starutil.DefaultThread(group.Fingerprint), "post_hook", group.PostHook, d.args(group, msg, changes, warnings), d.hostEnv) + res, err := runMain(defaultThread(group.Fingerprint), "post_hook", group.PostHook, d.args(group, msg, changes, warnings), d.hostEnv) if err != nil { return fmt.Errorf("run main: %w", err) } @@ -40,7 +37,7 @@ func (d *StarlarkHookRunner) Run(group store.TaskGroup, msg string, changes []st return nil } -func (runner *StarlarkHookRunner) args(group store.TaskGroup, msg string, changes []string, warnings []string) starlark.Tuple { +func (runner *PostHookRunner) args(group model.TaskGroup, msg string, changes []string, warnings []string) starlark.Tuple { title, body, ok := strings.Cut(msg, "\n") if !ok { title = msg diff --git a/starutil/starlark.go b/plugin/starlark.go similarity index 84% rename from starutil/starlark.go rename to plugin/starlark.go index e6a0ba5..753c3a1 100644 --- a/starutil/starlark.go +++ b/plugin/starlark.go @@ -1,4 +1,4 @@ -package starutil +package plugin // TODO: dont panic on error @@ -11,7 +11,7 @@ import ( "go.starlark.net/starlark" ) -func RunMain(thread *starlark.Thread, name string, script []byte, args starlark.Tuple, hostEnv *starlark.Dict) (starlark.Value, error) { +func runMain(thread *starlark.Thread, name string, script []byte, args starlark.Tuple, hostEnv *starlark.Dict) (starlark.Value, error) { globals := starlark.StringDict{ "host_env": hostEnv, } @@ -26,7 +26,7 @@ func RunMain(thread *starlark.Thread, name string, script []byte, args starlark. return starlark.Call(thread, m, args, nil) } -func AsStringSlice(v starlark.Value) ([]string, error) { +func asStringSlice(v starlark.Value) ([]string, error) { _, ok := v.(starlark.Iterable) if !ok { return nil, fmt.Errorf("expected iterable, got %s", v.Type()) @@ -45,7 +45,7 @@ func AsStringSlice(v starlark.Value) ([]string, error) { return slice, nil } -func EnvToStarlarkDict(env []string) *starlark.Dict { +func envToStarlarkDict(env []string) *starlark.Dict { d := starlark.NewDict(0) for _, e := range env { key, val, ok := strings.Cut(e, "=") @@ -59,7 +59,7 @@ func EnvToStarlarkDict(env []string) *starlark.Dict { return d } -func DefaultThread(name string) *starlark.Thread { +func defaultThread(name string) *starlark.Thread { return &starlark.Thread{ Name: name, Load: starlib.Loader, diff --git a/starutil/starlark_test.go b/plugin/starlark_test.go similarity index 90% rename from starutil/starlark_test.go rename to plugin/starlark_test.go index 81f28bd..3ff4bf1 100644 --- a/starutil/starlark_test.go +++ b/plugin/starlark_test.go @@ -1,4 +1,4 @@ -package starutil +package plugin import ( "reflect" @@ -24,7 +24,7 @@ func TestRunMain(t *testing.T) { { name: "simple", args: args{ - thread: DefaultThread("test"), + thread: defaultThread("test"), name: "test", script: []byte(`def main(): return 1`), args: starlark.Tuple{}, @@ -34,7 +34,7 @@ func TestRunMain(t *testing.T) { { name: "lookup env", args: args{ - thread: DefaultThread("test"), + thread: defaultThread("test"), name: "test", script: []byte(`def main(): return host_env["FOO"]`), args: starlark.Tuple{}, @@ -51,7 +51,7 @@ func TestRunMain(t *testing.T) { { name: "pass args", args: args{ - thread: DefaultThread("test"), + thread: defaultThread("test"), name: "test", script: []byte(`def main(a, b): return a + b`), args: starlark.Tuple{starlark.MakeInt(1), starlark.MakeInt(2)}, @@ -61,7 +61,7 @@ func TestRunMain(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := RunMain(tt.args.thread, tt.args.name, tt.args.script, tt.args.args, tt.args.hostEnv) + got, err := runMain(tt.args.thread, tt.args.name, tt.args.script, tt.args.args, tt.args.hostEnv) if (err != nil) != tt.wantErr { t.Errorf("RunMain() error = %v, wantErr %v", err, tt.wantErr) return @@ -93,7 +93,7 @@ func TestEnvToStarlarkDict(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := EnvToStarlarkDict(tt.giveEnv); !reflect.DeepEqual(got, tt.wantDict) { + if got := envToStarlarkDict(tt.giveEnv); !reflect.DeepEqual(got, tt.wantDict) { t.Errorf("OsEnvToStarlarkDict() = %v, want %v", got, tt.wantDict) } }) @@ -119,7 +119,7 @@ func TestAsStringSlice(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := AsStringSlice(tt.args.v) + got, err := asStringSlice(tt.args.v) if (err != nil) != tt.wantErr { t.Errorf("AsStringSlice() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/dbutil/func.go b/store/func.go similarity index 98% rename from dbutil/func.go rename to store/func.go index 3f1485a..5715be7 100644 --- a/dbutil/func.go +++ b/store/func.go @@ -1,4 +1,4 @@ -package dbutil +package store import ( "crypto/sha1" diff --git a/dbutil/func_test.go b/store/func_test.go similarity index 98% rename from dbutil/func_test.go rename to store/func_test.go index 4b042db..10a3810 100644 --- a/dbutil/func_test.go +++ b/store/func_test.go @@ -1,4 +1,4 @@ -package dbutil +package store import "testing" diff --git a/store/config.query.sql.go b/store/model/config.query.sql.go similarity index 99% rename from store/config.query.sql.go rename to store/model/config.query.sql.go index 2027031..c1d5b34 100644 --- a/store/config.query.sql.go +++ b/store/model/config.query.sql.go @@ -3,7 +3,7 @@ // sqlc v1.25.0 // source: config.query.sql -package store +package model import ( "context" diff --git a/store/db.go b/store/model/db.go similarity index 97% rename from store/db.go rename to store/model/db.go index 50bf666..2e42192 100644 --- a/store/db.go +++ b/store/model/db.go @@ -2,7 +2,7 @@ // versions: // sqlc v1.25.0 -package store +package model import ( "context" diff --git a/store/model/models.go b/store/model/models.go new file mode 100644 index 0000000..8580c77 --- /dev/null +++ b/store/model/models.go @@ -0,0 +1,80 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.25.0 + +package model + +import ( + git "github.com/bluebrown/kobold/git" + store "github.com/bluebrown/kobold/store" + null "github.com/volatiletech/null/v8" +) + +type Channel struct { + Name string `json:"name"` + DecoderName null.String `json:"decoder_name"` +} + +type Decoder struct { + Name string `json:"name"` + Script []byte `json:"script"` +} + +type Pipeline struct { + Name string `json:"name"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHookName null.String `json:"post_hook_name"` +} + +type PipelineListItem struct { + Name string `json:"name"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHookName null.String `json:"post_hook_name"` + Channels store.JsonArray `json:"channels"` +} + +type PostHook struct { + Name string `json:"name"` + Script []byte `json:"script"` +} + +type Run struct { + Fingerprint string `json:"fingerprint"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHook null.String `json:"post_hook"` + Status string `json:"status"` + Timestamp interface{} `json:"timestamp"` + Warnings store.SliceText `json:"warnings"` + Error interface{} `json:"error"` + Msgs store.SliceText `json:"msgs"` +} + +type Subscription struct { + PipelineName string `json:"pipeline_name"` + ChannelName string `json:"channel_name"` +} + +type Task struct { + ID string `json:"id"` + Msgs store.SliceText `json:"msgs"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHookName null.String `json:"post_hook_name"` + Status string `json:"status"` + Timestamp string `json:"timestamp"` + Warnings store.SliceText `json:"warnings"` + FailureReason null.String `json:"failure_reason"` + TaskGroupFingerprint null.String `json:"task_group_fingerprint"` +} + +type TaskGroup struct { + Fingerprint string `json:"fingerprint"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHook []byte `json:"post_hook"` + TaskIds store.SliceText `json:"task_ids"` + Msgs store.SliceText `json:"msgs"` +} diff --git a/store/read.query.sql.go b/store/model/read.query.sql.go similarity index 95% rename from store/read.query.sql.go rename to store/model/read.query.sql.go index 8139c78..da903b9 100644 --- a/store/read.query.sql.go +++ b/store/model/read.query.sql.go @@ -3,14 +3,14 @@ // sqlc v1.25.0 // source: read.query.sql -package store +package model import ( "context" "strings" - dbutil "github.com/bluebrown/kobold/dbutil" git "github.com/bluebrown/kobold/git" + store "github.com/bluebrown/kobold/store" null "github.com/volatiletech/null/v8" ) @@ -174,16 +174,16 @@ type PipelineRunListParams struct { } type PipelineRunListRow struct { - Name null.String `json:"name"` - Fingerprint string `json:"fingerprint"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHook null.String `json:"post_hook"` - Status string `json:"status"` - Timestamp interface{} `json:"timestamp"` - Warnings dbutil.SliceText `json:"warnings"` - Error interface{} `json:"error"` - Msgs dbutil.SliceText `json:"msgs"` + Name null.String `json:"name"` + Fingerprint string `json:"fingerprint"` + RepoUri git.PackageURI `json:"repo_uri"` + DestBranch null.String `json:"dest_branch"` + PostHook null.String `json:"post_hook"` + Status string `json:"status"` + Timestamp interface{} `json:"timestamp"` + Warnings store.SliceText `json:"warnings"` + Error interface{} `json:"error"` + Msgs store.SliceText `json:"msgs"` } // PipelineRunList diff --git a/store/task.query.sql.go b/store/model/task.query.sql.go similarity index 90% rename from store/task.query.sql.go rename to store/model/task.query.sql.go index b819c0f..dfca215 100644 --- a/store/task.query.sql.go +++ b/store/model/task.query.sql.go @@ -3,13 +3,13 @@ // sqlc v1.25.0 // source: task.query.sql -package store +package model import ( "context" "strings" - dbutil "github.com/bluebrown/kobold/dbutil" + store "github.com/bluebrown/kobold/store" null "github.com/volatiletech/null/v8" ) @@ -77,12 +77,12 @@ returning id ` type TaskGroupsStatusCompSwapParams struct { - TaskGroupFingerprint null.String `json:"task_group_fingerprint"` - Status string `json:"status"` - Warnings dbutil.SliceText `json:"warnings"` - FailureReason null.String `json:"failure_reason"` - ReqStatus string `json:"req_status"` - Ids []string `json:"ids"` + TaskGroupFingerprint null.String `json:"task_group_fingerprint"` + Status string `json:"status"` + Warnings store.SliceText `json:"warnings"` + FailureReason null.String `json:"failure_reason"` + ReqStatus string `json:"req_status"` + Ids []string `json:"ids"` } // set the status of all tasks in a group where the status matches the @@ -154,8 +154,8 @@ returning id ` type TasksAppendParams struct { - Msgs dbutil.SliceText `json:"msgs"` - Name string `json:"name"` + Msgs store.SliceText `json:"msgs"` + Name string `json:"name"` } // TasksAppend diff --git a/store/models.go b/store/models.go deleted file mode 100644 index f00120f..0000000 --- a/store/models.go +++ /dev/null @@ -1,80 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.25.0 - -package store - -import ( - dbutil "github.com/bluebrown/kobold/dbutil" - git "github.com/bluebrown/kobold/git" - null "github.com/volatiletech/null/v8" -) - -type Channel struct { - Name string `json:"name"` - DecoderName null.String `json:"decoder_name"` -} - -type Decoder struct { - Name string `json:"name"` - Script []byte `json:"script"` -} - -type Pipeline struct { - Name string `json:"name"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHookName null.String `json:"post_hook_name"` -} - -type PipelineListItem struct { - Name string `json:"name"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHookName null.String `json:"post_hook_name"` - Channels dbutil.JsonArray `json:"channels"` -} - -type PostHook struct { - Name string `json:"name"` - Script []byte `json:"script"` -} - -type Run struct { - Fingerprint string `json:"fingerprint"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHook null.String `json:"post_hook"` - Status string `json:"status"` - Timestamp interface{} `json:"timestamp"` - Warnings dbutil.SliceText `json:"warnings"` - Error interface{} `json:"error"` - Msgs dbutil.SliceText `json:"msgs"` -} - -type Subscription struct { - PipelineName string `json:"pipeline_name"` - ChannelName string `json:"channel_name"` -} - -type Task struct { - ID string `json:"id"` - Msgs dbutil.SliceText `json:"msgs"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHookName null.String `json:"post_hook_name"` - Status string `json:"status"` - Timestamp string `json:"timestamp"` - Warnings dbutil.SliceText `json:"warnings"` - FailureReason null.String `json:"failure_reason"` - TaskGroupFingerprint null.String `json:"task_group_fingerprint"` -} - -type TaskGroup struct { - Fingerprint string `json:"fingerprint"` - RepoUri git.PackageURI `json:"repo_uri"` - DestBranch null.String `json:"dest_branch"` - PostHook []byte `json:"post_hook"` - TaskIds dbutil.SliceText `json:"task_ids"` - Msgs dbutil.SliceText `json:"msgs"` -} diff --git a/sql/clean.sql b/store/schema/clean.sql similarity index 100% rename from sql/clean.sql rename to store/schema/clean.sql diff --git a/sql/config.query.sql b/store/schema/config.query.sql similarity index 100% rename from sql/config.query.sql rename to store/schema/config.query.sql diff --git a/sql/embed.go b/store/schema/embed.go similarity index 88% rename from sql/embed.go rename to store/schema/embed.go index 9238402..0332fca 100644 --- a/sql/embed.go +++ b/store/schema/embed.go @@ -1,4 +1,4 @@ -package sql +package schema import _ "embed" diff --git a/sql/read.query.sql b/store/schema/read.query.sql similarity index 100% rename from sql/read.query.sql rename to store/schema/read.query.sql diff --git a/sql/task.query.sql b/store/schema/task.query.sql similarity index 100% rename from sql/task.query.sql rename to store/schema/task.query.sql diff --git a/sql/task.schema.sql b/store/schema/task.schema.sql similarity index 100% rename from sql/task.schema.sql rename to store/schema/task.schema.sql diff --git a/dbutil/types.go b/store/types.go similarity index 85% rename from dbutil/types.go rename to store/types.go index b630672..f509ae5 100644 --- a/dbutil/types.go +++ b/store/types.go @@ -1,4 +1,4 @@ -package dbutil +package store import ( "database/sql/driver" @@ -23,7 +23,7 @@ func (s *SliceText) Scan(value interface{}) error { case []byte: str = string(v) default: - return fmt.Errorf("dbutil: cannot convert %T to SliceText", value) + return fmt.Errorf("store: cannot convert %T to SliceText", value) } out := strings.Split(str, SEPERATOR) @@ -54,7 +54,7 @@ func (s *JsonArray) Scan(value interface{}) error { case []byte: b = v default: - return fmt.Errorf("dbutil: cannot convert %T to JsonArray", value) + return fmt.Errorf("store: cannot convert %T to JsonArray", value) } return json.Unmarshal(b, &s) } diff --git a/task/decoder.go b/task/decoder.go deleted file mode 100644 index d9c94bc..0000000 --- a/task/decoder.go +++ /dev/null @@ -1,30 +0,0 @@ -package task - -import ( - "fmt" - - "go.starlark.net/starlark" - - "github.com/bluebrown/kobold/starutil" -) - -type StarlarkDecoder struct { -} - -var _ Decoder = (*StarlarkDecoder)(nil) - -func NewStarlarkDecoder() *StarlarkDecoder { - return &StarlarkDecoder{} -} - -func (d *StarlarkDecoder) Decode(name string, script []byte, data []byte) ([]string, error) { - res, err := starutil.RunMain(starutil.DefaultThread(name), name, script, d.args(data), nil) - if err != nil { - return nil, fmt.Errorf("run main: %w", err) - } - return starutil.AsStringSlice(res) -} - -func (d *StarlarkDecoder) args(data []byte) starlark.Tuple { - return starlark.Tuple{starlark.String(data)} -} diff --git a/task/handler.go b/task/handler.go index 1579430..9a670e8 100644 --- a/task/handler.go +++ b/task/handler.go @@ -8,14 +8,14 @@ import ( "github.com/bluebrown/kobold/git" "github.com/bluebrown/kobold/krm" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" "github.com/prometheus/client_golang/prometheus" "sigs.k8s.io/kustomize/kyaml/kio" ) // the task handler is the final point of execution. after decoding, debouncing // and aggregating the events, this handler is resonbible for the actual work -func KoboldHandler(ctx context.Context, cache string, g store.TaskGroup, runner HookRunner) ([]string, error) { +func KoboldHandler(ctx context.Context, cache string, g model.TaskGroup, runner HookRunner) ([]string, error) { var ( changes []string warnings []string @@ -86,7 +86,7 @@ func KoboldHandler(ctx context.Context, cache string, g store.TaskGroup, runner var _ Handler = KoboldHandler -func PrintHandler(ctx context.Context, hostPath string, g store.TaskGroup, runner HookRunner) ([]string, error) { +func PrintHandler(ctx context.Context, hostPath string, g model.TaskGroup, runner HookRunner) ([]string, error) { b, err := json.MarshalIndent(g, "", " ") if err != nil { return nil, fmt.Errorf("marshal task group: %w", err) @@ -97,7 +97,7 @@ func PrintHandler(ctx context.Context, hostPath string, g store.TaskGroup, runne var _ Handler = PrintHandler -func ThrowHandler(ctx context.Context, hostPath string, g store.TaskGroup, runner HookRunner) ([]string, error) { +func ThrowHandler(ctx context.Context, hostPath string, g model.TaskGroup, runner HookRunner) ([]string, error) { return nil, fmt.Errorf("throw handler error") } diff --git a/task/pool.go b/task/pool.go index d861597..5c9c269 100644 --- a/task/pool.go +++ b/task/pool.go @@ -17,9 +17,10 @@ import ( "github.com/volatiletech/null/v8" "golang.org/x/sync/errgroup" - "github.com/bluebrown/kobold/dbutil" "github.com/bluebrown/kobold/git" + "github.com/bluebrown/kobold/plugin" "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" ) // pool implements a worker pool backed by the storage layer. Tasks are first @@ -33,17 +34,17 @@ import ( // the pool drains remaining task and returns the error type Pool struct { group *errgroup.Group - queries *store.Queries + queries *model.Queries ctx context.Context handler Handler - decoder Decoder + decoder DecoderRunner hookRunner HookRunner cancel context.CancelFunc size int cache *git.RepoCache } -func NewPool(ctx context.Context, size int, queries *store.Queries) *Pool { +func NewPool(ctx context.Context, size int, queries *model.Queries) *Pool { ctx, cancel := context.WithCancel(ctx) eg, ectx := errgroup.WithContext(ctx) eg.SetLimit(size) @@ -55,8 +56,8 @@ func NewPool(ctx context.Context, size int, queries *store.Queries) *Pool { group: eg, queries: queries, handler: nil, - decoder: NewStarlarkDecoder(), - hookRunner: NewStarlarkPostHook(), + decoder: plugin.NewDecoderRunner(), + hookRunner: plugin.NewPostHookRunner(), size: size, cache: cache, } @@ -109,7 +110,7 @@ func (p *Pool) Dispatch() error { ids := []string(g.TaskIds) - swapped, err := p.queries.TaskGroupsStatusCompSwap(p.ctx, store.TaskGroupsStatusCompSwapParams{ + swapped, err := p.queries.TaskGroupsStatusCompSwap(p.ctx, model.TaskGroupsStatusCompSwapParams{ TaskGroupFingerprint: null.NewString(g.Fingerprint, true), Status: string(StatusRunning), ReqStatus: string(StatusPending), @@ -156,13 +157,13 @@ func (p *Pool) Dispatch() error { slog.WarnContext(p.ctx, "cache error", "fingerprint", g.Fingerprint, "error", err) } - swapped, err = p.queries.TaskGroupsStatusCompSwap(p.ctx, store.TaskGroupsStatusCompSwapParams{ + swapped, err = p.queries.TaskGroupsStatusCompSwap(p.ctx, model.TaskGroupsStatusCompSwapParams{ TaskGroupFingerprint: null.NewString(g.Fingerprint, true), Ids: ids, ReqStatus: string(StatusRunning), Status: string(status), FailureReason: null.NewString(reason, reason != ""), - Warnings: dbutil.SliceText(warns), + Warnings: store.SliceText(warns), }) slog.InfoContext(p.ctx, "task group done", "fingerprint", g.Fingerprint, "status", status) @@ -248,8 +249,8 @@ func (p *Pool) Queue(ctx context.Context, channel string, msg []byte) (err error } } - _, err = p.queries.TasksAppend(ctx, store.TasksAppendParams{ - Msgs: dbutil.SliceText(refs), + _, err = p.queries.TasksAppend(ctx, model.TasksAppendParams{ + Msgs: store.SliceText(refs), Name: channel, }) diff --git a/task/scheduler.go b/task/scheduler.go index ec32e56..63849c7 100644 --- a/task/scheduler.go +++ b/task/scheduler.go @@ -7,7 +7,7 @@ import ( "log/slog" "time" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" ) // Scheduler wraps the pool and provides a scheduling interface events can be @@ -20,7 +20,7 @@ type Scheduler struct { pool *Pool } -func NewScheduler(ctx context.Context, q *store.Queries, size int, interval time.Duration) *Scheduler { +func NewScheduler(ctx context.Context, q *model.Queries, size int, interval time.Duration) *Scheduler { return &Scheduler{ // buffer incoming events to prevent blocking the caller, // incase the scheduler is currently blocking on pool.Dispatch() diff --git a/task/task.go b/task/task.go index 3af2b11..f988eca 100644 --- a/task/task.go +++ b/task/task.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/bluebrown/kobold/store" + "github.com/bluebrown/kobold/store/model" ) type Status string @@ -16,17 +16,16 @@ const ( StatusFailure Status = "failure" ) -type Decoder interface { +type DecoderRunner interface { Decode(name string, script []byte, data []byte) ([]string, error) } type HookRunner interface { - Run(group store.TaskGroup, msg string, changes []string, warnings []string) error + Run(group model.TaskGroup, msg string, changes []string, warnings []string) error } -type Handler func(ctx context.Context, hostPath string, g store.TaskGroup, hook HookRunner) ([]string, error) +type Handler func(ctx context.Context, hostPath string, g model.TaskGroup, hook HookRunner) ([]string, error) -// implement the flag.Value interface func (t *Handler) String() string { return fmt.Sprintf("%T", *t) }