1package interfaces
2
3import (
4 "context"
5 "io"
6 "net/http"
7 "os"
8 "os/exec"
9
10 "github.com/bnema/archup/internal/system"
11)
12
13// FileSystem abstracts file system operations for testing
14type FileSystem interface {
15 Stat(name string) (os.FileInfo, error)
16 IsNotExist(err error) bool
17 Open(name string) (io.ReadCloser, error)
18 ReadFile(filename string) ([]byte, error)
19 WriteFile(filename string, data []byte, perm os.FileMode) error
20 Create(name string) (io.WriteCloser, error)
21 MkdirAll(path string, perm os.FileMode) error
22 RemoveAll(path string) error
23 Chmod(name string, mode os.FileMode) error
24}
25
26// CommandExecutor abstracts command execution
27type CommandExecutor interface {
28 Execute(name string, args ...string) ([]byte, error)
29}
30
31// SystemExecutor abstracts system package functions
32type SystemExecutor interface {
33 RunSimple(command string, args ...string) system.CommandResult
34 DetectCPUInfo() (*system.CPUInfo, error)
35}
36
37// ChrootSession represents a persistent chroot session that can execute
38// multiple commands while maintaining state (e.g., /tmp directory contents).
39// Sessions must be explicitly closed when done to clean up resources.
40type ChrootSession interface {
41 // Exec executes a command in the chroot session
42 Exec(command string) error
43
44 // ExecWithOutput executes a command and returns its output
45 ExecWithOutput(command string) (string, error)
46
47 // ExecWithContext executes a command with context support for cancellation
48 ExecWithContext(ctx context.Context, command string) error
49
50 // ExecWithOutputAndContext executes a command with context and returns output
51 ExecWithOutputAndContext(ctx context.Context, command string) (string, error)
52
53 // Close terminates the session and cleans up resources
54 Close() error
55}
56
57// ChrootExecutor abstracts chroot operations
58type ChrootExecutor interface {
59 ChrootExec(logPath, mountPoint, command string, args ...string) error
60 ChrootExecWithOutput(logPath, mountPoint, command string, args ...string) (string, error)
61 ChrootExecWithOutputAndContext(ctx context.Context, logPath, mountPoint, command string, args ...string) (string, error)
62 ChrootSystemctl(logPath, mountPoint, action, service string) error
63 ChrootExecWithStdin(logPath, mountPoint, command, stdin string) error
64 ChrootExecWithContext(ctx context.Context, logPath, mountPoint, command string) error
65 ChrootPacman(logPath, mountPoint, operation string, packages ...string) error
66 DownloadAndInstallPackages(logPath, chrootPath string, urls ...string) error
67
68 // BeginSession starts a persistent chroot session
69 BeginSession(logPath, mountPoint string) (ChrootSession, error)
70
71 // BeginSessionWithContext starts a persistent chroot session with context support
72 BeginSessionWithContext(ctx context.Context, logPath, mountPoint string) (ChrootSession, error)
73}
74
75// HTTPClient abstracts HTTP operations
76type HTTPClient interface {
77 Get(url string) (*http.Response, error)
78}
79
80// ConfigSaver abstracts config save operation
81type ConfigSaver interface {
82 Save() error
83}
84
85// DefaultFileSystem is the concrete implementation using OS functions
86type DefaultFileSystem struct{}
87
88func (d *DefaultFileSystem) Stat(name string) (os.FileInfo, error) {
89 return os.Stat(name)
90}
91
92func (d *DefaultFileSystem) IsNotExist(err error) bool {
93 return os.IsNotExist(err)
94}
95
96func (d *DefaultFileSystem) Open(name string) (io.ReadCloser, error) {
97 return os.Open(name)
98}
99
100func (d *DefaultFileSystem) ReadFile(filename string) ([]byte, error) {
101 return os.ReadFile(filename)
102}
103
104func (d *DefaultFileSystem) WriteFile(filename string, data []byte, perm os.FileMode) error {
105 return os.WriteFile(filename, data, perm)
106}
107
108func (d *DefaultFileSystem) Create(name string) (io.WriteCloser, error) {
109 return os.Create(name)
110}
111
112func (d *DefaultFileSystem) MkdirAll(path string, perm os.FileMode) error {
113 return os.MkdirAll(path, perm)
114}
115
116func (d *DefaultFileSystem) RemoveAll(path string) error {
117 return os.RemoveAll(path)
118}
119
120func (d *DefaultFileSystem) Chmod(name string, mode os.FileMode) error {
121 return os.Chmod(name, mode)
122}
123
124// DefaultCommandExecutor is the concrete implementation using os/exec
125type DefaultCommandExecutor struct{}
126
127func (d *DefaultCommandExecutor) Execute(name string, args ...string) ([]byte, error) {
128 return exec.Command(name, args...).Output()
129}
130
131// DefaultHTTPClient is the concrete implementation using net/http
132type DefaultHTTPClient struct{}
133
134func (d *DefaultHTTPClient) Get(url string) (*http.Response, error) {
135 return http.Get(url)
136}
137
138// DefaultSystemExecutor is the concrete implementation using system functions
139type DefaultSystemExecutor struct{}
140
141func (d *DefaultSystemExecutor) RunSimple(command string, args ...string) system.CommandResult {
142 return system.RunSimple(command, args...)
143}
144
145func (d *DefaultSystemExecutor) DetectCPUInfo() (*system.CPUInfo, error) {
146 return system.DetectCPUInfo()
147}
148
149// DefaultChrootExecutor is the concrete implementation using chroot functions
150type DefaultChrootExecutor struct{}
151
152func (d *DefaultChrootExecutor) ChrootExec(logPath, mountPoint, command string, args ...string) error {
153 return system.ChrootExec(logPath, mountPoint, command, args...)
154}
155
156func (d *DefaultChrootExecutor) ChrootExecWithOutput(logPath, mountPoint, command string, args ...string) (string, error) {
157 return system.ChrootExecWithOutput(logPath, mountPoint, command, args...)
158}
159
160func (d *DefaultChrootExecutor) ChrootExecWithOutputAndContext(ctx context.Context, logPath, mountPoint, command string, args ...string) (string, error) {
161 return system.ChrootExecWithOutputAndContext(ctx, logPath, mountPoint, command, args...)
162}
163
164func (d *DefaultChrootExecutor) ChrootSystemctl(logPath, mountPoint, action, service string) error {
165 return system.ChrootSystemctl(logPath, mountPoint, action, service)
166}
167
168func (d *DefaultChrootExecutor) ChrootExecWithStdin(logPath, mountPoint, command, stdin string) error {
169 return system.ChrootExecWithStdin(logPath, mountPoint, command, stdin)
170}
171
172func (d *DefaultChrootExecutor) ChrootExecWithContext(ctx context.Context, logPath, mountPoint, command string) error {
173 return system.ChrootExecWithContext(ctx, logPath, mountPoint, command)
174}
175
176func (d *DefaultChrootExecutor) ChrootPacman(logPath, mountPoint, operation string, packages ...string) error {
177 return system.ChrootPacman(logPath, mountPoint, operation, packages...)
178}
179
180func (d *DefaultChrootExecutor) DownloadAndInstallPackages(logPath, chrootPath string, urls ...string) error {
181 return system.DownloadAndInstallPackages(logPath, chrootPath, urls...)
182}
183
184func (d *DefaultChrootExecutor) BeginSession(logPath, mountPoint string) (ChrootSession, error) {
185 return system.BeginSession(logPath, mountPoint)
186}
187
188func (d *DefaultChrootExecutor) BeginSessionWithContext(ctx context.Context, logPath, mountPoint string) (ChrootSession, error) {
189 return system.BeginSessionWithContext(ctx, logPath, mountPoint)
190}