-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcommand.go
More file actions
95 lines (80 loc) · 2.34 KB
/
command.go
File metadata and controls
95 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tpm
import (
"context"
"fmt"
"strings"
"github.com/9elements/converged-security-suite/v2/pkg/bootflow/lib/format"
"github.com/9elements/converged-security-suite/v2/pkg/bootflow/types"
)
// Command is a serializable command that could be "sent" (applied) to TPM.
type Command interface {
// Apply applies the changes themselves to the given *TPM (not including appending CommandLog).
//
// Do not use this method directly unless you know what are you doing,
// use (*TPM).TPMExecute instead.
Apply(context.Context, *TPM) error
// LogString formats the entry for CommandLog.
LogString() string
}
// Commands is a slice of Command-s
type Commands []Command
// apply implements Command.
func (s Commands) Apply(ctx context.Context, tpm *TPM) error {
for idx, cmd := range s {
if err := cmd.Apply(ctx, tpm); err != nil {
return fmt.Errorf("unable to apply command #%d '%T': %w", idx, cmd, err)
}
}
return nil
}
// LogString implements Command.
func (s Commands) LogString() string {
result := make([]string, 0, len(s))
for _, cmd := range s {
result = append(result, cmd.LogString())
}
return strings.Join(result, ", ")
}
// CauseCoordinates defines the coordinates of the Action in a Flow which caused the Command.
type CauseCoordinates = types.ActionCoordinates
// CauseAction defines the Action which caused the Command.
type CauseAction = types.Action
// CommandLogEntry is a log entry of a Command.
type CommandLogEntry struct {
Command
CauseCoordinates
CauseAction
}
// String implements fmt.Stringer.
func (entry CommandLogEntry) String() string {
return entry.LogString()
}
func newCommandLogEntry(
cmd Command,
causeCoords types.ActionCoordinates,
causeAction types.Action,
) CommandLogEntry {
return CommandLogEntry{
Command: cmd,
CauseCoordinates: causeCoords,
CauseAction: causeAction,
}
}
// CommandLog is a log of Command-s executed by the TPM.
type CommandLog []CommandLogEntry
// Commands returns the list of raw Command-s.
func (s CommandLog) Commands() Commands {
result := make(Commands, 0, len(s))
for _, entry := range s {
result = append(result, entry.Command)
}
return result
}
// String implements fmt.Stringer
func (s CommandLog) String() string {
var result strings.Builder
for idx, e := range s {
fmt.Fprintf(&result, "%d. %s\n", idx, format.NiceString(e))
}
return result.String()
}