1//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -destination=mocks/mock_ports.go -package=mocks . FileSystem,File,CommandExecutor,ChrootExecutor,ScriptExecutor,HTTPClient,Response,Logger,InstallationRepository
2
3package ports
4
5import (
6 "context"
7 "os"
8)
9
10// FileSystem is the port for file system operations
11// Implementations must support reading, writing, and file metadata operations
12type FileSystem interface {
13 // Stat returns file info for the given path
14 Stat(name string) (os.FileInfo, error)
15
16 // ReadFile reads entire file content
17 ReadFile(name string) ([]byte, error)
18
19 // WriteFile writes data to a file with the given permissions
20 WriteFile(name string, data []byte, perm os.FileMode) error
21
22 // Create creates or truncates a file
23 Create(name string) (File, error)
24
25 // Chmod changes file permissions
26 Chmod(name string, perm os.FileMode) error
27
28 // MkdirAll creates directory path with parents
29 MkdirAll(path string, perm os.FileMode) error
30
31 // RemoveAll removes a file or directory recursively
32 RemoveAll(path string) error
33
34 // Exists checks if a file/directory exists
35 Exists(path string) (bool, error)
36}
37
38// File is a file handle for reading/writing
39type File interface {
40 // Read reads up to len(b) bytes from the file
41 Read(b []byte) (n int, err error)
42
43 // Write writes len(b) bytes to the file
44 Write(b []byte) (n int, err error)
45
46 // Close closes the file
47 Close() error
48}
49
50// CommandExecutor is the port for executing system commands
51type CommandExecutor interface {
52 // Execute runs a command and returns output
53 Execute(ctx context.Context, command string, args ...string) ([]byte, error)
54
55 // ExecuteWithStdin runs a command with stdin content and returns output
56 ExecuteWithStdin(ctx context.Context, stdin string, command string, args ...string) ([]byte, error)
57
58 // ExecuteWithEnv runs a command with custom environment variables
59 ExecuteWithEnv(ctx context.Context, env map[string]string, command string, args ...string) ([]byte, error)
60}
61
62// ChrootExecutor is the port for executing commands in a chroot environment
63type ChrootExecutor interface {
64 // ExecuteInChroot runs a command inside a chroot
65 ExecuteInChroot(ctx context.Context, chrootPath string, command string, args ...string) ([]byte, error)
66
67 // ExecuteInChrootWithStdin runs a command inside a chroot with stdin
68 ExecuteInChrootWithStdin(ctx context.Context, chrootPath string, stdin string, command string, args ...string) error
69
70 // ChrootSystemctl runs systemctl commands in chroot
71 ChrootSystemctl(ctx context.Context, logPath string, chrootPath string, args ...string) error
72}
73
74// ScriptExecutor is the port for executing shell scripts
75type ScriptExecutor interface {
76 // ExecuteScript runs a shell script with environment variables
77 ExecuteScript(ctx context.Context, scriptPath string, env map[string]string) error
78}
79
80// HTTPClient is the port for HTTP operations
81type HTTPClient interface {
82 // Get performs an HTTP GET request
83 Get(url string) (Response, error)
84
85 // Post performs an HTTP POST request
86 Post(url string, contentType string, body []byte) (Response, error)
87}
88
89// Response represents an HTTP response
90type Response interface {
91 // StatusCode returns the HTTP status code
92 StatusCode() int
93
94 // Body returns the response body
95 Body() []byte
96
97 // Close closes the response
98 Close() error
99}
100
101// Logger is the port for logging
102type Logger interface {
103 // Info logs an informational message
104 Info(msg string, keysAndValues ...any)
105
106 // Warn logs a warning message
107 Warn(msg string, keysAndValues ...any)
108
109 // Error logs an error message
110 Error(msg string, keysAndValues ...any)
111
112 // Debug logs a debug message
113 Debug(msg string, keysAndValues ...any)
114
115 // LogPath returns the path to the log file
116 LogPath() string
117}
118
119// InstallationRepository is the port for installation persistence
120type InstallationRepository interface {
121 // Save persists the installation state
122 Save(ctx context.Context, installationID string, state string) error
123
124 // Load retrieves the installation state
125 Load(ctx context.Context, installationID string) (string, error)
126
127 // Exists checks if an installation exists
128 Exists(ctx context.Context, installationID string) (bool, error)
129}