Skip to content

Commit

Permalink
add parse package
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre committed Oct 18, 2023
1 parent 91af703 commit 1d1276c
Show file tree
Hide file tree
Showing 50 changed files with 5,311 additions and 702 deletions.
34 changes: 15 additions & 19 deletions load_mod/load_mod.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package load_mod

import (
"context"
"fmt"
"github.com/turbot/pipe-fittings/parse"
"log"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/hcl/v2"
filehelpers "github.com/turbot/go-kit/files"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/constants"
"github.com/turbot/pipe-fittings/error_helpers"
"github.com/turbot/pipe-fittings/filepaths"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/parse"
"github.com/turbot/pipe-fittings/perr"
"github.com/turbot/pipe-fittings/versionmap"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)

func LoadModWithFileName(modPath string, modFile string, parseCtx *parse.ModParseContext) (mod *modconfig.Mod, errorsAndWarnings *error_helpers.ErrorAndWarnings) {
func LoadModWithFileName(ctx context.Context, modPath string, modFile string, parseCtx *parse.ModParseContext) (mod *modconfig.Mod, errorsAndWarnings *error_helpers.ErrorAndWarnings) {
defer func() {
if r := recover(); r != nil {
errorsAndWarnings = error_helpers.NewErrorsAndWarning(helpers.ToError(r))
}
}()

mod, loadModResult := loadModDefinition(modPath, modFile, parseCtx)
mod, loadModResult := loadModDefinition(ctx, modPath, parseCtx)
if loadModResult.Error != nil {
return nil, loadModResult
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func LoadMod(modPath string, parseCtx *parse.ModParseContext) (mod *modconfig.Mo
}
}()

return LoadModWithFileName(modPath, filepaths.PipesComponentModsFileName, parseCtx)
return LoadModWithFileName(context.Background(), modPath, filepaths.PipesComponentModsFileName, parseCtx)
}

func ModFileExists(modPath, modFile string) bool {
Expand All @@ -91,30 +91,26 @@ func ModFileExists(modPath, modFile string) bool {
return false
}

func loadModDefinition(modPath string, modFile string, parseCtx *parse.ModParseContext) (*modconfig.Mod, *error_helpers.ErrorAndWarnings) {
var mod *modconfig.Mod
errorsAndWarnings := &error_helpers.ErrorAndWarnings{}

if parseCtx.ShouldCreateCreateTransientLocalMod() && !ModFileExists(modPath, modFile) {
mod = modconfig.NewMod("local", modPath, hcl.Range{})
return mod, errorsAndWarnings
}

func loadModDefinition(ctx context.Context, modPath string, parseCtx *parse.ModParseContext) (mod *modconfig.Mod, errorsAndWarnings *error_helpers.ErrorAndWarnings) {
errorsAndWarnings = &error_helpers.ErrorAndWarnings{}
// verify the mod folder exists
modFileFound := ModFileExists(modPath, modFile)
_, err := os.Stat(modPath)
if os.IsNotExist(err) {
return nil, error_helpers.NewErrorsAndWarning(fmt.Errorf("mod folder %s does not exist", modPath))
}

if modFileFound {
if parse.ModfileExists(modPath) {
// load the mod definition to get the dependencies
var res *parse.DecodeResult
mod, res = parse.ParseModDefinitionWithFileName(modPath, modFile, parseCtx.EvalCtx)
mod, res = parse.ParseModDefinition(modPath, parseCtx.EvalCtx)
errorsAndWarnings = error_helpers.DiagsToErrorsAndWarnings("mod load failed", res.Diags)
if res.Diags.HasErrors() {
return nil, errorsAndWarnings
}
} else {
// so there is no mod file - should we create a default?
if !parseCtx.ShouldCreateDefaultMod() {
errorsAndWarnings.Error = perr.BadRequestWithMessage(fmt.Sprintf("mod folder does not contain a mod resource definition '%s'", modPath))
errorsAndWarnings.Error = fmt.Errorf("mod folder %s does not contain a mod resource definition", modPath)
// ShouldCreateDefaultMod flag NOT set - fail
return nil, errorsAndWarnings
}
Expand Down Expand Up @@ -218,7 +214,7 @@ func loadModResources(mod *modconfig.Mod, parseCtx *parse.ModParseContext) (*mod
}

// parse all hcl files (NOTE - this reads the CurrentMod out of ParseContext and adds to it)
mod, errAndWarnings := parse.ParseMod(fileData, pseudoResources, parseCtx)
mod, errAndWarnings := parse.ParseMod(context.Background(), fileData, pseudoResources, parseCtx)

return mod, errAndWarnings
}
Expand Down
134 changes: 64 additions & 70 deletions misc/load_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ package misc
import (
"context"
"fmt"
"github.com/turbot/pipe-fittings/load_mod"
"os"
"path/filepath"

filehelpers "github.com/turbot/go-kit/files"
"github.com/turbot/pipe-fittings/filepaths"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/parse"
"github.com/turbot/pipe-fittings/perr"
)

// ToError formats the supplied value as an error (or just returns it if already an error)
Expand All @@ -31,66 +23,68 @@ func ToError(val interface{}) error {
// We can potentially remove this function, but we have to refactor all our test cases
func LoadPipelines(ctx context.Context, configPath string) (map[string]*modconfig.Pipeline, map[string]*modconfig.Trigger, error) {

var modDir string
var fileName string
var modFileNameToLoad string

// Get information about the path
info, err := os.Stat(configPath)
if err != nil {
if os.IsNotExist(err) {
return map[string]*modconfig.Pipeline{}, map[string]*modconfig.Trigger{}, nil
}
return nil, nil, err
}

// Check if it's a regular file
if info.Mode().IsRegular() {
fileName = filepath.Base(configPath)
modDir = filepath.Dir(configPath)

// TODO: this is a hack (ish) to let the existing automated test to pass
if filepath.Ext(fileName) == ".fp" {
modFileNameToLoad = "ignore.sp"
} else {
modFileNameToLoad = fileName
}
} else if info.IsDir() { // Check if it's a directory

defaultModSp := filepath.Join(configPath, filepaths.PipesComponentModsFileName)

_, err := os.Stat(defaultModSp)
if err == nil {
// default mod.hcl exist
fileName = filepaths.PipesComponentModsFileName
modDir = configPath
} else {
fileName = "*.fp"
modDir = configPath
}
modFileNameToLoad = fileName
} else {
return nil, nil, perr.BadRequestWithMessage("invalid path")
}

parseCtx := parse.NewModParseContext(
ctx,
nil,
modDir,
parse.CreateTransientLocalMod,
&filehelpers.ListOptions{
Flags: filehelpers.Files | filehelpers.Recursive,
Include: []string{"**/" + fileName},
})

mod, errorsAndWarnings := load_mod.LoadModWithFileName(modDir, modFileNameToLoad, parseCtx)

var pipelines map[string]*modconfig.Pipeline
var triggers map[string]*modconfig.Trigger

if mod != nil && mod.ResourceMaps != nil {
pipelines = mod.ResourceMaps.Pipelines
triggers = mod.ResourceMaps.Triggers
}
return pipelines, triggers, errorsAndWarnings.Error
// TODO KAI FIX ME
//var modDir string
//var fileName string
//var modFileNameToLoad string
//
//// Get information about the path
//info, err := os.Stat(configPath)
//if err != nil {
// if os.IsNotExist(err) {
// return map[string]*modconfig.Pipeline{}, map[string]*modconfig.Trigger{}, nil
// }
// return nil, nil, err
//}
//
//// Check if it's a regular file
//if info.Mode().IsRegular() {
// fileName = filepath.Base(configPath)
// modDir = filepath.Dir(configPath)
//
// // TODO: this is a hack (ish) to let the existing automated test to pass
// if filepath.Ext(fileName) == ".fp" {
// modFileNameToLoad = "ignore.sp"
// } else {
// modFileNameToLoad = fileName
// }
//} else if info.IsDir() { // Check if it's a directory
//
// defaultModSp := filepath.Join(configPath, filepaths.PipesComponentModsFileName)
//
// _, err := os.Stat(defaultModSp)
// if err == nil {
// // default mod.hcl exist
// fileName = filepaths.PipesComponentModsFileName
// modDir = configPath
// } else {
// fileName = "*.fp"
// modDir = configPath
// }
// modFileNameToLoad = fileName
//} else {
// return nil, nil, perr.BadRequestWithMessage("invalid path")
//}
//
//parseCtx := parse.NewModParseContext(
// ctx,
// nil,
// modDir,
// parse_v.CreateTransientLocalMod,
// &filehelpers.ListOptions{
// Flags: filehelpers.Files | filehelpers.Recursive,
// Include: []string{"**/" + fileName},
// })
//
//mod, errorsAndWarnings := load_mod.LoadModWithFileName(context.Background(), modDir, modFileNameToLoad, parseCtx)
//
//var pipelines map[string]*modconfig.Pipeline
//var triggers map[string]*modconfig.Trigger
//
//if mod != nil && mod.ResourceMaps != nil {
// pipelines = mod.ResourceMaps.Pipelines
// triggers = mod.ResourceMaps.Triggers
//}
//return pipelines, triggers, errorsAndWarnings.Error
return nil, nil, nil
}
2 changes: 1 addition & 1 deletion misc/workspace_profile_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (l *WorkspaceProfileLoader) get(name string) (*modconfig.WorkspaceProfile,

func (l *WorkspaceProfileLoader) load(runCtx context.Context) (map[string]*modconfig.WorkspaceProfile, error) {
// get all the config files in the directory
return parse.LoadWorkspaceProfiles(runCtx, l.workspaceProfilePath)
return parse.LoadWorkspaceProfiles(l.workspaceProfilePath)
}

/*
Expand Down
Loading

0 comments on commit 1d1276c

Please sign in to comment.