Skip to content

Commit

Permalink
Rename Outputs to Artefacts
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Aug 21, 2024
1 parent 8557473 commit 2e38e1a
Show file tree
Hide file tree
Showing 19 changed files with 754 additions and 748 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

Specter is a development toolkit in Go that allows you to develop configuration file processors based on
HashiCorp Configuration Language (HCL). With Specter, you can define your own Domain-Specific Language (DSL)
using HCL and create a processing pipeline to validate, lint, resolve dependencies, and generate code or output
using HCL and create a processing pipeline to validate, lint, resolve dependencies, and generate code or artifact
files from these DSL configuration files.

## Features

- Develop your own DSL using HCL
- Validate and lint configuration files
- Resolve dependencies between configuration files
- Generate code or output files from configuration files
- Generate code or artifact files from configuration files

## Getting Started

Expand Down
107 changes: 107 additions & 0 deletions artefactregistry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package specter

import (
"encoding/json"
"github.com/morebec/go-errors/errors"
"os"
"sync"
"time"
)

// JSONArtifactRegistry implementation of a ArtifactRegistry that is saved as a JSON file.
type JSONArtifactRegistry struct {
GeneratedAt time.Time `json:"generatedAt"`
ArtifactMap map[string]*JSONArtifactRegistryProcessor `json:"files"`
FilePath string
mu sync.RWMutex // Mutex to protect concurrent access
}

type JSONArtifactRegistryProcessor struct {
Artifacts []string `json:"files"`
}

// NewJSONArtifactRegistry returns a new artifact file registry.
func NewJSONArtifactRegistry(fileName string) *JSONArtifactRegistry {
return &JSONArtifactRegistry{
GeneratedAt: time.Now(),
ArtifactMap: nil,
FilePath: fileName,
}
}

func (r *JSONArtifactRegistry) Load() error {
r.mu.Lock()
defer r.mu.Unlock()

bytes, err := os.ReadFile(r.FilePath)

if err != nil {
if os.IsNotExist(err) {
return nil
}
return errors.WrapWithMessage(err, errors.InternalErrorCode, "failed loading artifact file registry")
}
if err := json.Unmarshal(bytes, r); err != nil {
return errors.WrapWithMessage(err, errors.InternalErrorCode, "failed loading artifact file registry")
}

return nil
}

func (r *JSONArtifactRegistry) Save() error {
r.mu.RLock()
defer r.mu.RUnlock()

if r.ArtifactMap == nil {
return nil
}
// Generate a JSON file containing all artifact files for clean up later on
js, err := json.MarshalIndent(r, "", " ")
if err != nil {
return errors.Wrap(err, "failed generating artifact file registry")
}
if err := os.WriteFile(r.FilePath, js, os.ModePerm); err != nil {
return errors.Wrap(err, "failed generating artifact file registry")
}

return nil
}

func (r *JSONArtifactRegistry) AddArtifact(processorName string, artifactName string) {
r.mu.Lock()
defer r.mu.Unlock()

if _, ok := r.ArtifactMap[processorName]; !ok {
r.ArtifactMap[processorName] = &JSONArtifactRegistryProcessor{}
}
r.ArtifactMap[processorName].Artifacts = append(r.ArtifactMap[processorName].Artifacts, artifactName)
}

func (r *JSONArtifactRegistry) RemoveArtifact(processorName string, artifactName string) {
r.mu.Lock()
defer r.mu.Unlock()

if _, ok := r.ArtifactMap[processorName]; !ok {
return
}

var files []string
for _, file := range r.ArtifactMap[processorName].Artifacts {
if file != artifactName {
files = append(files, file)
}
}

r.ArtifactMap[processorName].Artifacts = files
}

func (r *JSONArtifactRegistry) Artifacts(processorName string) []string {
r.mu.RLock()
defer r.mu.RUnlock()

artifacts, ok := r.ArtifactMap[processorName]
if !ok {
return nil
}
return artifacts.Artifacts
}
Loading

0 comments on commit 2e38e1a

Please sign in to comment.