Manager
The Manager
is the central component of the Go Pip SDK, implementing the PipManager
interface and providing all core functionality for Python package management.
Constructor Functions
NewManager
func NewManager(config *Config) *Manager
Creates a new pip manager instance with the specified configuration.
Parameters:
config
(*Config): Configuration for the manager. Ifnil
, default configuration is used.
Returns:
*Manager
: A new manager instance.
Example:
// Create with default configuration
manager := pip.NewManager(nil)
// Create with custom configuration
config := &pip.Config{
Timeout: 60 * time.Second,
Retries: 5,
LogLevel: "DEBUG",
}
manager := pip.NewManager(config)
NewManagerWithContext
func NewManagerWithContext(ctx context.Context, config *Config) *Manager
Creates a new pip manager instance with a context for cancellation and timeouts.
Parameters:
ctx
(context.Context): Context for operation cancellation and timeouts.config
(*Config): Configuration for the manager. Ifnil
, default configuration is used.
Returns:
*Manager
: A new manager instance with the specified context.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
manager := pip.NewManagerWithContext(ctx, nil)
Configuration Methods
SetConfig
func (m *Manager) SetConfig(config *Config)
Updates the manager configuration.
Parameters:
config
(*Config): New configuration to apply.
GetConfig
func (m *Manager) GetConfig() *Config
Returns the current manager configuration.
Returns:
*Config
: Current configuration.
SetLogger
func (m *Manager) SetLogger(logger *log.Logger)
Sets a custom standard library logger.
Parameters:
logger
(*log.Logger): Standard library logger instance.
SetCustomLogger
func (m *Manager) SetCustomLogger(logger *Logger)
Sets a custom SDK logger with advanced features.
Parameters:
logger
(*Logger): SDK logger instance with structured logging.
SetContext
func (m *Manager) SetContext(ctx context.Context)
Updates the context for operations.
Parameters:
ctx
(context.Context): New context for operations.
System Operations
IsInstalled
func (m *Manager) IsInstalled() (bool, error)
Checks if pip is installed and accessible.
Returns:
bool
:true
if pip is installed and accessible,false
otherwise.error
: Error if the check fails.
Example:
installed, err := manager.IsInstalled()
if err != nil {
return fmt.Errorf("failed to check pip installation: %w", err)
}
if !installed {
fmt.Println("Pip is not installed")
}
Install
func (m *Manager) Install() error
Installs pip on the system using the most appropriate method for the current platform.
Returns:
error
: Error if installation fails.
Installation Methods:
- Windows: Uses
ensurepip
module or downloadsget-pip.py
- macOS: Uses
ensurepip
, Homebrew, or MacPorts - Linux: Uses system package manager (apt, yum, dnf, etc.) or
ensurepip
Example:
if err := manager.Install(); err != nil {
return fmt.Errorf("failed to install pip: %w", err)
}
fmt.Println("Pip installed successfully")
GetVersion
func (m *Manager) GetVersion() (string, error)
Gets the installed pip version.
Returns:
string
: Pip version string (e.g., "23.2.1").error
: Error if version retrieval fails.
Example:
version, err := manager.GetVersion()
if err != nil {
return fmt.Errorf("failed to get pip version: %w", err)
}
fmt.Printf("Pip version: %s\n", version)
Utility Functions
DefaultConfig
func DefaultConfig() *Config
Returns a default configuration with sensible defaults.
Returns:
*Config
: Default configuration.
Default Values:
&Config{
Timeout: 30 * time.Second,
Retries: 3,
LogLevel: "INFO",
CacheDir: "",
Environment: make(map[string]string),
ExtraOptions: make(map[string]string),
}
GetOSType
func GetOSType() OSType
Detects the current operating system type.
Returns:
OSType
: Operating system type (OSWindows
,OSMacOS
,OSLinux
, orOSUnknown
).
Example:
osType := pip.GetOSType()
switch osType {
case pip.OSWindows:
fmt.Println("Running on Windows")
case pip.OSMacOS:
fmt.Println("Running on macOS")
case pip.OSLinux:
fmt.Println("Running on Linux")
default:
fmt.Println("Unknown operating system")
}
Thread Safety
The Manager
type is safe for concurrent use. All methods can be called simultaneously from multiple goroutines.
Error Handling
All methods return structured errors that can be type-checked:
if err := manager.Install(); err != nil {
if pip.IsErrorType(err, pip.ErrorTypePipNotInstalled) {
// Handle pip not installed error
} else if pip.IsErrorType(err, pip.ErrorTypePermissionDenied) {
// Handle permission error
}
return err
}
Best Practices
Always check if pip is installed before performing operations:
goif installed, err := manager.IsInstalled(); err != nil || !installed { if err := manager.Install(); err != nil { return err } }
Use contexts for long-running operations:
goctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() manager := pip.NewManagerWithContext(ctx, nil)
Configure appropriate timeouts and retries:
goconfig := &pip.Config{ Timeout: 120 * time.Second, Retries: 5, }