alter

package
v0.0.0-...-372bc1c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuildNewAlterDir = func(templateMap map[string]string) error {

	templateMap[globals.KEY_STORE_DIR_PATH] = filepath.Join(templateMap[globals.KEY_ALTER_PATH], globals.STORE_DIRNAME)
	templateMap[globals.KEY_CONTROL_JSON_PATH] = filepath.Join(templateMap[globals.KEY_ALTER_PATH], globals.CONTROL_JSON_FILE)
	templateMap[globals.KEY_CODE_BLOCK_ROOT_PATH] = filepath.Join(templateMap[globals.KEY_RECIPE_PATH], globals.CODE_DIRNAME, templateMap[globals.KEY_CODE_BLOCK_NAME])

	alterRelPath := templateMap[globals.KEY_ALTER_REL_PATH]
	alterRelPath = strings.TrimPrefix(alterRelPath, "/")
	alterRelPathParts := strings.Split(alterRelPath, "/")
	codeBlockPath := templateMap[globals.KEY_CODE_BLOCK_ROOT_PATH]
	for _, part := range alterRelPathParts {
		codeBlockPath = filepath.Join(codeBlockPath, part)
	}
	templateMap[globals.KEY_CODE_BLOCK_PATH] = codeBlockPath

	err := domain.BuildStore(templateMap)
	if err != nil {
		return err
	}

	switch templateMap[globals.KEY_ALTER_SUB_COMMAND] {
	case globals.TRANSFORM:
		err = transform.BuildSubcommand(templateMap)
		if err != nil {
			return err
		}
	case globals.RECAST:
		err = recast.BuildSubcommand(templateMap)
		if err != nil {
			return err
		}
	case globals.PICK:
		err = pick.BuildSubcommand(templateMap)
		if err != nil {
			return err
		}
	default:
		msg := "FAILED: unknown alter sub-command"
		logger.Log.Error(msg)
		return errors.New(msg)
	}

	return nil
}
View Source
var BuildNewPhaseFile = func(templateMap map[string]string) (string, error) {

	phaseName := templateMap[globals.KEY_PHASE_NAME]
	phasesPath := templateMap[globals.KEY_PHASES_PATH]

	phaseFilepath := filepath.Join(phasesPath, phaseName+globals.JSON_EXT)
	scaffold := globals.ScaffoldInfoTListT{

		{
			Filepath: filepath.Join(phaseFilepath),
			Content: `
{
  "codeblock": "{{code-block-name}}",
  "requires": [
	"{{depends-on-phase}}"
  ],
  "ops_pipeline": [
	{
	  "alter": {
		"locator": [
		  "{{full-alter-rel-path}}"
		]
	  }
	}
  ]
}
		`,
		},
	}

	err := common.CreateFiles(scaffold)
	if err != nil {
		return phaseFilepath, err
	}

	err = common.ReplaceUsingTemplateMap(templateMap, phasesPath)
	if err != nil {
		return phaseFilepath, err
	}
	return phaseFilepath, err
}
View Source
var CreatePhaseFile = func(templateMap map[string]string) error {
	err := BuildNewAlterDir(templateMap)
	if err != nil {
		return err
	}

	phaseFilepath, err := BuildNewPhaseFile(templateMap)
	if err != nil {
		return err
	}

	err = common.ReplaceUsingTemplateMap(templateMap, phaseFilepath)
	if err != nil {
		return err
	}

	return nil
}
View Source
var UpdatePhaseFile = func(templateMap map[string]string) error {

	var getPhaseData = func(templateMap map[string]string) (map[string]interface{}, error) {
		phaseName := templateMap[globals.KEY_PHASE_NAME]
		phasesPath := templateMap[globals.KEY_PHASES_PATH]
		fullPhasePath := filepath.Join(phasesPath, phaseName+globals.JSON_EXT)

		if !common.IsFile(fullPhasePath) {
			err := errors.New("phase file does not exist")
			return nil, err
		}

		if !common.IsFile(fullPhasePath) {
			err := errors.New("phase file does not exist")
			return nil, err
		}

		result, err := common.ReadJsonFile(fullPhasePath)
		if err != nil {
			msg := fmt.Sprintf("phase file could not be read: %s", err.Error())
			err = errors.New(msg)
			return nil, err
		}
		return result, err
	}

	var getAlterJson = func(templateMap map[string]string) map[string]interface{} {

		fullAlterPath := templateMap[globals.KEY_FULL_ALTER_REL_PATH]

		fullAlterPathWithQuotes := globals.QUOTE + fullAlterPath + globals.QUOTE

		jsonStr := `	{
	  "alter": {
		"locator": [` +
			fullAlterPathWithQuotes + `
		]
	  }
	}`

		var jsonFrame map[string]interface{}

		json.Unmarshal([]byte(jsonStr), &jsonFrame)
		return jsonFrame
	}

	// check if the alter command locator is already present
	var checkForDuplicateAlter = func(opsPipeline []interface{}, alterCommand interface{}) error {
		for _, alterCommandInPipeline := range opsPipeline {
			pathFromPipeline := alterCommandInPipeline.(map[string]interface{})["alter"].(map[string]interface{})["locator"].([]interface{})[0].(string)
			pathFromAlterCommand := alterCommand.(map[string]interface{})["alter"].(map[string]interface{})["locator"].([]interface{})[0].(string)
			if pathFromPipeline == pathFromAlterCommand {
				return errors.New("alter command already present")
			}
		}
		return nil
	}

	var addNewAlterToPhase = func(templateMap map[string]string) (map[string]interface{}, error) {
		phaseContent, err := getPhaseData(templateMap)
		if err != nil {
			return nil, err
		}

		alterCommand := getAlterJson(templateMap)
		logger.Log.Debug("alter command to add: ", alterCommand)

		opsPipeline := phaseContent["ops_pipeline"].([]interface{})
		err = checkForDuplicateAlter(opsPipeline, alterCommand)
		if err != nil {
			return nil, err
		}

		appendedPipeline := append(opsPipeline, alterCommand)
		phaseContent["ops_pipeline"] = appendedPipeline
		return phaseContent, nil
	}

	var writePhaseFile = func(phaseContent map[string]interface{}) error {
		jsonifiedPhase, _ := json.MarshalIndent(phaseContent, "", "  ")
		logger.Log.Debug(string(jsonifiedPhase))

		phaseName := templateMap[globals.KEY_PHASE_NAME]
		phasesPath := templateMap[globals.KEY_PHASES_PATH]
		fullPhasePath := filepath.Join(phasesPath, phaseName+globals.JSON_EXT)

		err := os.WriteFile(fullPhasePath, jsonifiedPhase, fs.ModeAppend.Perm())
		if err != nil {
			msg := fmt.Sprintf("phase file could not be written to: %s", fullPhasePath)
			err = errors.New(msg)
			return err
		}
		return nil
	}

	err := BuildNewAlterDir(templateMap)
	if err != nil {
		return err
	}

	phaseContent, err := addNewAlterToPhase(templateMap)
	if err != nil {
		return err
	}

	err = writePhaseFile(phaseContent)
	if err != nil {
		return err
	}

	logger.Log.Info("SUCCESS: add alter - with updated phase file")
	return nil
}

ensure that the alter location is not already present add the alter location to the phase file write the modified phase file generate the alter directory at the right location

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL