|
| 1 | +//go:build p4 |
| 2 | + |
| 3 | +package nodes |
| 4 | + |
| 5 | +import ( |
| 6 | + _ "embed" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/actionforge/actrun-cli/core" |
| 11 | + ni "github.com/actionforge/actrun-cli/node_interfaces" |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +var p4PrintDefinition string |
| 16 | + |
| 17 | +type P4PrintNode struct { |
| 18 | + core.NodeBaseComponent |
| 19 | + core.Executions |
| 20 | + core.Inputs |
| 21 | + core.Outputs |
| 22 | +} |
| 23 | + |
| 24 | +func (n *P4PrintNode) ExecuteImpl(c *core.ExecutionState, inputId core.InputId, prevError error) error { |
| 25 | + depotPath, err := core.InputValueById[string](c, n, ni.Core_p4_print_v1_Input_depot_path) |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + outputPath, err := core.InputValueById[string](c, n, ni.Core_p4_print_v1_Input_output_path) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + creds, _ := core.InputValueById[core.Credentials](c, n, ni.Core_p4_print_v1_Input_credentials) |
| 36 | + |
| 37 | + fields := buildP4Fields(c, creds) |
| 38 | + |
| 39 | + p4, err := connectP4(c, fields) |
| 40 | + if err != nil { |
| 41 | + return n.Execute(ni.Core_p4_print_v1_Output_exec_err, c, err) |
| 42 | + } |
| 43 | + defer func() { |
| 44 | + p4.Disconnect() |
| 45 | + p4.Close() |
| 46 | + }() |
| 47 | + |
| 48 | + // Ensure parent directory exists |
| 49 | + if dir := filepath.Dir(outputPath); dir != "" { |
| 50 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 51 | + mkdirErr := core.CreateErr(c, err, "failed to create output directory '%s'", dir) |
| 52 | + return n.Execute(ni.Core_p4_print_v1_Output_exec_err, c, mkdirErr) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // p4 print -q -o <output> <depot_path> |
| 57 | + _, runErr := p4.Run("print", "-q", "-o", outputPath, depotPath) |
| 58 | + if runErr != nil { |
| 59 | + printErr := core.CreateErr(c, runErr, "p4 print failed for %s", depotPath) |
| 60 | + return n.Execute(ni.Core_p4_print_v1_Output_exec_err, c, printErr) |
| 61 | + } |
| 62 | + |
| 63 | + return n.Execute(ni.Core_p4_print_v1_Output_exec_success, c, nil) |
| 64 | +} |
| 65 | + |
| 66 | +func init() { |
| 67 | + err := core.RegisterNodeFactory(p4PrintDefinition, func(ctx any, parent core.NodeBaseInterface, parentId string, nodeDef map[string]any, validate bool, opts core.RunOpts) (core.NodeBaseInterface, []error) { |
| 68 | + return &P4PrintNode{}, nil |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | +} |
0 commit comments