Skip to content

📁 File System

pkg/composerutils/fs.go provides basic file system utility functions, reused by pkg/composer, pkg/installer, and other modules.

Function List

FunctionSignatureDescription
CheckWritePermission(dir string) errorCheck directory writable (create if not exists)
EnsureDirectoryExists(dir string) errorEnsure directory exists, create recursively if not
CreateFileWithContent(filePath string, content []byte, perm os.FileMode) errorCreate file and write content

CheckWritePermission

Checks if a directory has write permission. If the directory doesn't exist, it will first attempt to create it, then verify write permission by creating a temporary file, and automatically clean up after testing.

go
func CheckWritePermission(dir string) error
ParameterTypeDescription
dirstringDirectory path to check

Possible errors: Directory creation failed, no write permission.

go
if err := composerutils.CheckWritePermission("/usr/local/bin"); err != nil {
    log.Fatalf("Directory has no write permission: %v", err)
}

EnsureDirectoryExists

Ensures specified directory exists, creates recursively if not (permission 0755).

go
func EnsureDirectoryExists(dir string) error
go
installDir := "/opt/composer-skills"
if err := composerutils.EnsureDirectoryExists(installDir); err != nil {
    log.Fatalf("Unable to create installation directory: %v", err)
}

CreateFileWithContent

Creates file at specified path and writes content, automatically ensuring parent directory exists.

go
func CreateFileWithContent(filePath string, content []byte, perm os.FileMode) error
ParameterTypeDescription
filePathstringFile path (including filename)
content[]byteContent to write
permos.FileModeFile permission (e.g., 0644, 0755)
go
script := []byte("#!/bin/sh\necho 'Hello World'")
if err := composerutils.CreateFileWithContent("/usr/local/bin/hello.sh", script, 0755); err != nil {
    log.Fatalf("Unable to create script: %v", err)
}

Relationship with standard library

These three functions are thin wrappers around os.MkdirAll, os.WriteFile, os.Create, adding combined semantics like "ensure parent directory exists" and "write permission detection", avoiding repetitive handling.

Advanced

Released under the MIT License