Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template api #52

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions momentum-core/artefacts/artefact-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func loadArtefactTreeUntilDepth(path string, parent *Artefact, depth int) (*Arte
if err != nil {
return nil, err
}
defer dir.Close()

dirEntries, err := dir.ReadDir(-1) // reads all entries
for _, entry := range dirEntries {
Expand All @@ -372,6 +373,7 @@ func children(artefact *Artefact) ([]*Artefact, error) {
if err != nil {
return make([]*Artefact, 0), err
}
defer dir.Close()

dirEntries, err := dir.ReadDir(-1) // reads all entries
for _, entry := range dirEntries {
Expand Down
36 changes: 0 additions & 36 deletions momentum-core/artefacts/override.go

This file was deleted.

6 changes: 3 additions & 3 deletions momentum-core/artefacts/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func RegisterArtefactRoutes(engine *gin.Engine) {
engine.GET(config.API_ARTEFACT_BY_ID, GetArtefact)
engine.GET(config.API_APPLICATIONS, GetApplications)
engine.GET(config.API_STAGES, GetStages)
engine.GET(config.API_DEPLOYMENTS, GetDeployments)
engine.GET(config.API_ARTEFACT_APPLICATIONS, GetApplications)
engine.GET(config.API_ARTEFACT_STAGES, GetStages)
engine.GET(config.API_ARTEFACT_DEPLOYMENTS, GetDeployments)
}

// GetArtefact godoc
Expand Down
2 changes: 1 addition & 1 deletion momentum-core/backtracking/backtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func root[T any, P any](n *Node[T, P]) *Node[T, P] {

func reject[T any, P any](search Search[T, P], n *Node[T, P]) bool {

// can be dangerous, because results are ommited possibly ommitted
// can be dangerous, because correct results are possibly ommitted
return search.StopEarly(search.Predicate(), search.Comparable(n))
}

Expand Down
95 changes: 55 additions & 40 deletions momentum-core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,81 +36,93 @@ var LOGGER ILoggerClient
type MomentumConfig struct {
dataDir string
validationTmpDir string
templatesDir string
logDir string

transactionToken *gittransaction.Token

applicationTemplateFolderPath string
stageTemplateFolderPath string
deploymentTemplateFilePath string
deploymentTemplateFolderPath string
}

func (m *MomentumConfig) DataDir() string {
return m.dataDir
}

func (m *MomentumConfig) RepoDir() string {
return filepath.Join(m.dataDir, "repository")
return filepath.Join(m.DataDir(), "repository")
}

func (m *MomentumConfig) ValidationTmpDir() string {
return m.validationTmpDir
}

func (m *MomentumConfig) TemplateDir() string {
return m.templatesDir
}

func (m *MomentumConfig) LogDir() string {
return m.logDir
}

func (m *MomentumConfig) ApplicationTemplateFolderPath() string {
return m.applicationTemplateFolderPath
func TemplateDir(config *MomentumConfig) string {
return filepath.Join(config.RepoDir(), "templates")
}

func (m *MomentumConfig) StageTemplateFolderPath() string {
return m.stageTemplateFolderPath
func ApplicationTemplatesPath(config *MomentumConfig) string {
return filepath.Join(TemplateDir(config), "applications")
}

func (m *MomentumConfig) DeploymentTemplateFolderPath() string {
return m.deploymentTemplateFolderPath
func StageTemplatesPath(config *MomentumConfig) string {
return filepath.Join(TemplateDir(config), "stages")
}

func (m *MomentumConfig) DeploymentTemplateFilePath() string {
return m.deploymentTemplateFilePath
func DeploymentTemplatesPath(config *MomentumConfig) string {
return filepath.Join(TemplateDir(config), "deployments")
}

func (m *MomentumConfig) TransactionToken() *gittransaction.Token {
return m.transactionToken
}

func (m *MomentumConfig) checkMandatoryTemplates() error {
func checkMandatoryTemplates(config *MomentumConfig) error {

errs := make([]error, 0)

templatePath := TemplateDir(config)
if !utils.FileExists(templatePath) {
err := utils.DirCreate(templatePath)
if err != nil {
errs = append(errs, err)
}
}

if !utils.FileExists(m.ApplicationTemplateFolderPath()) {
return errors.New("provide mandatory template for application folder at " + m.TemplateDir())
appTemplatePath := ApplicationTemplatesPath(config)
if !utils.FileExists(appTemplatePath) {
err := utils.DirCreate(appTemplatePath)
if err != nil {
errs = append(errs, err)
}
}

if !utils.FileExists(m.StageTemplateFolderPath()) {
return errors.New("provide mandatory template for stage folder at " + m.TemplateDir())
stageTemplatePath := StageTemplatesPath(config)
if !utils.FileExists(stageTemplatePath) {
err := utils.DirCreate(stageTemplatePath)
if err != nil {
errs = append(errs, err)
}
}

if !utils.FileExists(m.DeploymentTemplateFolderPath()) {
return errors.New("provide mandatory template for deployment folders at " + m.DeploymentTemplateFolderPath())
deploymentTemplatePath := DeploymentTemplatesPath(config)
if !utils.FileExists(deploymentTemplatePath) {
err := utils.DirCreate(deploymentTemplatePath)
if err != nil {
errs = append(errs, err)
}
}

if !utils.FileExists(m.DeploymentTemplateFilePath()) {
return errors.New("provide mandatory template for deployment files at " + m.DeploymentTemplateFilePath())
if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}

func (m *MomentumConfig) initializeRepository() error {
func initializeRepository(config *MomentumConfig) error {

_, err := os.Stat(m.RepoDir())
_, err := os.Stat(config.RepoDir())
if !os.IsNotExist(err) {
LOGGER.LogInfo("will not clone repository because one present", "STARTUP")
return nil
Expand All @@ -121,7 +133,11 @@ func (m *MomentumConfig) initializeRepository() error {
return errors.New("failed initializing momentum because " + MOMENTUM_GIT_REPO_URL + " was not set")
}

cloneRepoTo(repoUrl, "", "", m.RepoDir())
cloneRepoTo(repoUrl, "", "", config.RepoDir())

if !utils.FileExists(filepath.Join(config.RepoDir(), MOMENTUM_ROOT)) {
return errors.New("invalid momentum repository")
}

return nil
}
Expand Down Expand Up @@ -159,7 +175,6 @@ func InitializeMomentumCore() (*MomentumConfig, error) {
momentumDir := utils.BuildPath(usrHome, ".momentum")
dataDir := utils.BuildPath(momentumDir, "data")
validationTmpDir := utils.BuildPath(momentumDir, "validation")
templatesDir := utils.BuildPath(momentumDir, "templates")
logDir := momentumDir

createPathIfNotPresent(dataDir, momentumDir)
Expand All @@ -170,25 +185,25 @@ func InitializeMomentumCore() (*MomentumConfig, error) {
config.logDir = logDir
config.dataDir = dataDir
config.validationTmpDir = validationTmpDir
config.templatesDir = templatesDir
config.applicationTemplateFolderPath = utils.BuildPath(templatesDir, "applications")
config.stageTemplateFolderPath = utils.BuildPath(templatesDir, "stages")
config.deploymentTemplateFolderPath = utils.BuildPath(templatesDir, "deployments", "deploymentName")
config.deploymentTemplateFilePath = utils.BuildPath(templatesDir, "deployments", "deploymentName.yaml")

LOGGER, err = NewLogger(config.LogDir())
if err != nil {
panic("failed initializing logger: " + err.Error())
}

err = config.initializeGitAccessToken()
if err != nil {
return nil, err
}

err = config.checkMandatoryTemplates()
err = initializeRepository(config)
if err != nil {
return nil, err
}

LOGGER, err = NewLogger(config.LogDir())
err = checkMandatoryTemplates(config)
if err != nil {
panic("failed initializing logger: " + err.Error())
return nil, err
}

GLOBAL = config
Expand Down
23 changes: 23 additions & 0 deletions momentum-core/config/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

const API = "/api"
const API_VERSION_BETA = API + "/beta"

const API_FILE_BY_ID = API_VERSION_BETA + "/file/:id"
const API_FILE_ADD = API_VERSION_BETA + "/file"
const API_FILE_UPDATE = API_VERSION_BETA + "/file/:id"
const API_FILE_LINE_OVERWRITTENBY = API_VERSION_BETA + "/file/:id/line/:lineNumber/overwritten-by"

const API_ARTEFACT_BY_ID = API_VERSION_BETA + "/artefact/:id"
const API_ARTEFACT_APPLICATIONS = API_VERSION_BETA + "/applications"
const API_ARTEFACT_STAGES = API_VERSION_BETA + "/stages"
const API_ARTEFACT_DEPLOYMENTS = API_VERSION_BETA + "/deployments"

const API_TEMPLATES_PREFIX = API_VERSION_BETA + "/templates"
const API_TEMPLATES_APPLICATIONS = API_TEMPLATES_PREFIX + "/applications"
const API_TEMPLATES_STAGES = API_TEMPLATES_PREFIX + "/stages"
const API_TEMPLATES_DEPLOYMENTS = API_TEMPLATES_PREFIX + "/deployments"
const API_TEMPLATES_ADD = API_TEMPLATES_PREFIX
const API_TEMPLATES_SPEC_PREFIX = API_TEMPLATES_PREFIX + "/spec"
const API_TEMPLATE_GET_SPEC = API_TEMPLATES_SPEC_PREFIX + "/:templateName"
const API_TEMPLATE_APPLY = API_TEMPLATES_SPEC_PREFIX + "/apply/:anchorArtefactId"
15 changes: 0 additions & 15 deletions momentum-core/config/routes.go

This file was deleted.

Loading
Loading