🧰 ComposerUtils
pkg/composerutils is the basic utility set of Composer Skills, providing file system operations, HTTP downloads, mock interfaces, and test utilities. It is reused by pkg/composer, pkg/installer, and other modules, and can also be used independently.
Submodules
| Submodule | Package Path | Description |
|---|---|---|
| 📁 File System | pkg/composerutils (fs.go) | Write permission check, directory creation, file write |
| 🌐 HTTP Download | pkg/composerutils (http.go) | File download with proxy/timeout |
| 🎭 Mock Interfaces | pkg/composerutils/mock | Command execution, file system, download, runtime mocks |
| 🧪 Test Utilities | pkg/composerutils (test_utils.go) | Temporary directory, file assertions |
Quick Example
go
package main
import (
"fmt"
"github.com/scagogogo/composer-skills/pkg/composerutils"
)
func main() {
dir := "/tmp/my-app"
// Ensure directory exists
if err := composerutils.EnsureDirectoryExists(dir); err != nil {
panic(err)
}
// Check write permission
if err := composerutils.CheckWritePermission(dir); err != nil {
fmt.Printf("No write permission: %v\n", err)
}
// Write file
content := []byte(`{"name": "my/project"}`)
_ = composerutils.CreateFileWithContent(dir+"/composer.json", content, 0644)
}Design Goals
- Thin and reliable: Only wraps most common system operations, avoiding reinventing the wheel.
- Testable: Provides interface-based mocks through
mocksub-package, making unit testing easy for upper modules. - Cross-platform: Based on standard library
os/net/http, naturally cross-platform.
See each sub-document: File System · HTTP Download · Mock Interfaces · Test Utilities.