operations

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package operations provides CRE workflow operations that execute side effects via the CRE CLI.

Index

Constants

View Source
const (
	// CREDeployTargetName is the workflow.yaml / project.yaml target key used for this layout.
	CREDeployTargetName = "cld-deploy"
)

Variables

View Source
var CREWorkflowDeployOp = fwops.NewOperation(
	"cre-workflow-deploy",
	semver.MustParse("1.0.0"),
	"Deploys a CRE workflow via the CRE CLI subprocess",
	func(b fwops.Bundle, deps CREDeployDeps, input CREWorkflowDeployInput) (CREWorkflowDeployOutput, error) {
		ctx := b.GetContext()
		if deps.CLI == nil {
			return CREWorkflowDeployOutput{}, errors.New("cre CLIRunner is nil")
		}

		workDir, err := os.MkdirTemp("", "cre-workflow-artifacts-*")
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("mkdir temp workflow artifacts: %w", err)
		}
		defer func() { _ = os.RemoveAll(workDir) }()

		resolver, err := creartifacts.NewArtifactsResolver(workDir)
		if err != nil {
			return CREWorkflowDeployOutput{}, err
		}

		binaryPath, err := resolver.ResolveBinary(ctx, input.Binary)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("resolve workflow binary: %w", err)
		}

		configPath, err := resolver.ResolveConfig(ctx, input.Config)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("resolve workflow config: %w", err)
		}

		bundleDir := filepath.Join(workDir, creBundleSubdir)
		if err = os.MkdirAll(bundleDir, 0o700); err != nil {
			return CREWorkflowDeployOutput{}, err
		}

		projectSrc, err := resolver.ResolveConfig(ctx, input.Project)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("resolve project.yaml: %w", err)
		}
		projectDest := filepath.Join(workDir, "project.yaml")
		if err = copyFile(projectSrc, projectDest); err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("copy project.yaml: %w", err)
		}

		target := input.resolveTargetName()
		workflowCfg := crecli.WorkflowConfig{
			target: {
				UserWorkflow: crecli.UserWorkflow{
					DeploymentRegistry: input.DeploymentRegistry,
					WorkflowName:       input.WorkflowName,
				},
				WorkflowArtifacts: crecli.WorkflowArtifacts{
					WorkflowPath: ".",
					ConfigPath:   filepath.Base(configPath),
					SecretsPath:  "",
				},
			},
		}
		workflowYAMLPath, err := crecli.WriteWorkflowYAML(bundleDir, workflowCfg)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("write workflow.yaml: %w", err)
		}

		ctxCfg, err := crecli.BuildContextConfig(input.DonFamily, input.Context, deps.CRECfg, deps.CLI.ContextRegistries())
		if err != nil {
			return CREWorkflowDeployOutput{}, err
		}
		contextPath, err := crecli.WriteContextYAML(workDir, ctxCfg)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("write context.yaml: %w", err)
		}
		logResolvedFile(b.Logger, "workflow.yaml", workflowYAMLPath, prettyYAML)
		logResolvedFile(b.Logger, "project.yaml", projectDest, prettyYAML)
		logResolvedFile(b.Logger, "context.yaml", contextPath, prettyYAML)
		logResolvedFile(b.Logger, "config.json", configPath, prettyJSON)

		envPath, err := crecli.WriteCREEnvFile(workDir, contextPath, deps.CRECfg, input.DonFamily)
		if err != nil {
			return CREWorkflowDeployOutput{}, fmt.Errorf("write CRE .env file: %w", err)
		}

		args := BuildWorkflowDeployArgs(target, workDir, envPath, binaryPath, configPath, input.ExtraCREArgs)
		b.Logger.Infow("Running CRE workflow deploy", "args", args)

		var runEnv map[string]string
		if crecli.IsOnChainRegistry(input.DeploymentRegistry, crecli.FlatRegistries(ctxCfg)) {
			runEnv = map[string]string{
				"CRE_ETH_PRIVATE_KEY": deps.EVMDeployerKey,
			}
		}
		res, runErr := deps.CLI.Run(ctx, runEnv, args...)
		if runErr != nil {
			var exitErr *fcre.ExitError
			if errors.As(runErr, &exitErr) {
				return CREWorkflowDeployOutput{
					ExitCode: exitErr.ExitCode,
					Stdout:   string(exitErr.Stdout),
					Stderr:   string(exitErr.Stderr),
				}, fmt.Errorf("cre workflow deploy: %w", runErr)
			}

			return CREWorkflowDeployOutput{}, fmt.Errorf("cre workflow deploy: %w", runErr)
		}
		if res == nil {
			return CREWorkflowDeployOutput{}, errors.New("cre workflow deploy: CLI returned nil result without error")
		}

		b.Logger.Infow("CRE workflow deploy finished",
			"exitCode", res.ExitCode,
			"stdout", string(res.Stdout),
			"stderr", string(res.Stderr),
		)

		return CREWorkflowDeployOutput{
			ExitCode: res.ExitCode,
			Stdout:   string(res.Stdout),
			Stderr:   string(res.Stderr),
		}, nil
	},
)

CREWorkflowDeployOp deploys a workflow via the CRE CLI (single side effect: CLI invocation).

Functions

func BuildWorkflowDeployArgs

func BuildWorkflowDeployArgs(targetName, workDir, envPath, binaryPath, configPath string, extra []string) []string

BuildWorkflowDeployArgs constructs the CRE CLI argument list for `cre workflow deploy`.

Types

type CREDeployDeps

type CREDeployDeps struct {
	CLI    fcre.CLIRunner
	CRECfg cfgenv.CREConfig
	// EVMDeployerKey is the raw hex EVM private key from Onchain.EVM.DeployerKey.
	// Injected into the child process environment as CRE_ETH_PRIVATE_KEY only for on-chain registries.
	EVMDeployerKey string
}

CREDeployDeps holds non-serializable dependencies for the workflow deploy operation.

type CREWorkflowDeployInput

type CREWorkflowDeployInput struct {
	creartifacts.WorkflowBundle `yaml:",inline"`
	// Project is the path to CRE CLI project.yaml (RPCs, don-family, etc.).
	// Resolved the same way as Binary and Config: local path or GitHub ref.
	Project creartifacts.ConfigSource `json:"project" yaml:"project"`
	// Optional - Context overrides CRE_* process env defaults for the generated context.yaml.
	Context crecli.ContextOverrides `json:"context" yaml:"context"`
	// Optional - ExtraCREArgs are appended after built-in workflow deploy arguments (e.g. org/tenant flags).
	ExtraCREArgs []string `json:"extraCreArgs,omitempty" yaml:"extraCreArgs,omitempty"`
	// Optional - TargetName is the CRE CLI target key that must match a top-level key
	// in project.yaml. Defaults to CREDeployTargetName ("cld-deploy") when empty.
	TargetName string `json:"targetName,omitempty" yaml:"targetName,omitempty"`
}

CREWorkflowDeployInput is the resolved input for a CRE workflow deploy. Binary, config, and project are resolved via creartifacts.ArtifactsResolver. DeploymentRegistry is inherited from creartifacts.WorkflowBundle.

type CREWorkflowDeployOutput

type CREWorkflowDeployOutput struct {
	ExitCode int    `json:"exitCode"`
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
}

CREWorkflowDeployOutput is the serializable result of a CRE CLI deploy invocation.

Jump to

Keyboard shortcuts

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