octopush/internal/actions/action_creator.go
kjuulh 46cc160764
Examples, rename and fixes
updated roadmap

updated roadmap

Add a basic readme (#15)

Co-authored-by: kjuulh <contact@kjuulh.io>
Reviewed-on: kjuulh/kraken#15

fix readme

Add readme

with stuff

more roadmap items

update formatting

formatting 2

more setup

with semantic

with semantic 2

revert

add releaserc

with releaser

with kraken

Update roadmap

rename

rename

feature/move-command (#18)

Co-authored-by: kjuulh <contact@kjuulh.io>
Reviewed-on: #18

fix/require-two-pushes (#20)

Co-authored-by: kjuulh <contact@kjuulh.io>
Reviewed-on: #20
2022-09-21 22:52:24 +02:00

86 lines
1.9 KiB
Go

package actions
import (
"context"
"fmt"
"os"
"path"
"time"
"git.front.kjuulh.io/kjuulh/octopush/internal/schema"
"git.front.kjuulh.io/kjuulh/octopush/internal/services/providers"
"git.front.kjuulh.io/kjuulh/octopush/internal/services/storage"
"go.uber.org/zap"
)
type (
ActionCreatorOps struct {
RepositoryUrl string
Branch string
Path string
}
ActionCreator struct {
logger *zap.Logger
storage *storage.Service
git *providers.GoGit
}
ActionCreatorDeps interface {
GetStorageService() *storage.Service
GetGitProvider() *providers.GoGit
}
)
func NewActionCreator(logger *zap.Logger, deps ActionCreatorDeps) *ActionCreator {
return &ActionCreator{
logger: logger,
storage: deps.GetStorageService(),
git: deps.GetGitProvider(),
}
}
func (ac *ActionCreator) Prepare(ctx context.Context, ops *ActionCreatorOps) (*Action, error) {
area, err := ac.storage.CreateArea(ctx)
if err != nil {
ac.logger.Error("failed to allocate area", zap.Error(err))
return nil, err
}
cloneCtx, _ := context.WithTimeout(ctx, time.Second*10)
_, err = ac.git.CloneBranch(cloneCtx, area, ops.RepositoryUrl, ops.Branch)
if err != nil {
ac.logger.Error("could not clone repo", zap.Error(err))
return nil, err
}
executorUrl := path.Join(area.Path, ops.Path)
if _, err = os.Stat(executorUrl); os.IsNotExist(err) {
return nil, fmt.Errorf("path is invalid: %s", ops.Path)
}
contents, err := os.ReadFile(path.Join(executorUrl, "octopush.yml"))
if err != nil {
return nil, err
}
octopushSchema, err := schema.Unmarshal(string(contents))
if err != nil {
return nil, err
}
ac.logger.Debug("Action creator done")
return &Action{
Schema: octopushSchema,
SchemaPath: executorUrl,
}, nil
}
func (ac *ActionCreator) Cleanup(ctx context.Context, area *storage.Area) {
ac.logger.Debug("Removing area", zap.String("path", area.Path))
err := ac.storage.RemoveArea(ctx, area)
if err != nil {
panic(err)
}
}