Skip to content

Commit

Permalink
factor out absolute filepath creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mfleader committed Jan 5, 2024
1 parent d6b5bee commit 3bf9f4f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
36 changes: 13 additions & 23 deletions cmd/arcaflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import (
"flag"
"fmt"
"go.arcalot.io/log/v2"
"go.flow.arcalot.io/engine/loadfile"
"os"
"os/signal"
"path/filepath"

"go.flow.arcalot.io/engine"
"go.flow.arcalot.io/engine/config"
"go.flow.arcalot.io/engine/loadfile"
"gopkg.in/yaml.v3"
"os"
"os/signal"
)

// These variables are filled using ldflags during the build process with Goreleaser.
Expand Down Expand Up @@ -126,28 +124,16 @@ Options:

var err error

absDir, err := filepath.Abs(dir)
if err != nil {
tempLogger.Errorf("Failed to determine absolute path of arcaflow context directory %s (%v)", dir, err)
os.Exit(ExitCodeInvalidData)
}

requiredFiles := map[string]string{
RequiredFileKeyConfig: configFile,
RequiredFileKeyInput: input,
RequiredFileKeyWorkflow: workflowFile,
}
var requiredFilesAbs = map[string]string{}
for key, f := range requiredFiles {
abspath := f
if !filepath.IsAbs(f) {
abspath = filepath.Join(absDir, f)
}
requiredFilesAbs[key] = abspath
}
var requiredFilesAbsSlice = make([]string, len(requiredFiles))
for _, f := range requiredFilesAbs {
requiredFilesAbsSlice = append(requiredFilesAbsSlice, f)

requiredFilesAbsPaths, err := loadfile.ContextAbsFilepaths(dir, requiredFiles)
if err != nil {
tempLogger.Errorf("Failed to determine absolute path of arcaflow context directory %s (%v)", dir, err)
os.Exit(ExitCodeInvalidData)
}

var configData any = map[any]any{}
Expand All @@ -169,6 +155,10 @@ Options:

logger := log.New(cfg.Log).WithLabel("source", "main")

var requiredFilesAbsSlice = make([]string, len(requiredFiles))
for _, f := range requiredFilesAbsPaths {
requiredFilesAbsSlice = append(requiredFilesAbsSlice, f)
}
dirContext, err := loadfile.LoadContext(requiredFilesAbsSlice)
if err != nil {
logger.Errorf("Failed to load configuration file %s (%v)", configFile, err)
Expand All @@ -193,7 +183,7 @@ Options:
}
}

os.Exit(runWorkflow(flow, dirContext, requiredFilesAbs[RequiredFileKeyWorkflow], logger, inputData))
os.Exit(runWorkflow(flow, dirContext, requiredFilesAbsPaths[RequiredFileKeyWorkflow], logger, inputData))
}

func runWorkflow(flow engine.WorkflowEngine, dirContext map[string][]byte, workflowFile string, logger log.Logger, inputData []byte) int {
Expand Down
19 changes: 19 additions & 0 deletions loadfile/loadfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ func LoadContext(neededFilepaths []string) (map[string][]byte, error) {
}
return result, err
}

// ContextAbsFilepaths creates a map of absolute filepaths. If a required
// file is not provided with an absolute path, then it is joined with the
// root directory.
func ContextAbsFilepaths(rootDir string, requiredFiles map[string]string) (map[string]string, error) {
absDir, err := filepath.Abs(rootDir)
if err != nil {
return nil, err
}
requiredFilesAbs := map[string]string{}
for key, f := range requiredFiles {
abspath := f
if !filepath.IsAbs(f) {
abspath = filepath.Join(absDir, f)
}
requiredFilesAbs[key] = abspath
}
return requiredFilesAbs, nil
}

0 comments on commit 3bf9f4f

Please sign in to comment.