Core API
The pkg/nuget
package provides the main API interface for the NuGet Config Parser library. This is the primary entry point for most operations.
Overview
The Core API is designed to provide a simple, unified interface for all NuGet configuration operations. It abstracts away the complexity of the underlying components and provides a clean, easy-to-use API for developers.
API Structure
type API struct {
Parser *parser.ConfigParser
Finder *finder.ConfigFinder
Manager *manager.ConfigManager
}
The API struct integrates three core components:
- Parser: Handles configuration file parsing and serialization
- Finder: Locates configuration files across different platforms
- Manager: Manages configuration modifications and operations
Constructor
NewAPI
func NewAPI() *API
Creates a new API instance with default settings.
Returns:
*API
: A new API instance ready for use
Example:
api := nuget.NewAPI()
Parsing Methods
ParseFromFile
func (a *API) ParseFromFile(filePath string) (*types.NuGetConfig, error)
Parses a NuGet configuration file from the specified path.
Parameters:
filePath
(string): Path to the configuration file
Returns:
*types.NuGetConfig
: Parsed configuration objecterror
: Error if parsing fails
Example:
config, err := api.ParseFromFile("/path/to/NuGet.Config")
if err != nil {
log.Fatalf("Failed to parse config: %v", err)
}
ParseFromString
func (a *API) ParseFromString(content string) (*types.NuGetConfig, error)
Parses a NuGet configuration from an XML string.
Parameters:
content
(string): XML content as string
Returns:
*types.NuGetConfig
: Parsed configuration objecterror
: Error if parsing fails
Example:
xmlContent := `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>`
config, err := api.ParseFromString(xmlContent)
ParseFromReader
func (a *API) ParseFromReader(reader io.Reader) (*types.NuGetConfig, error)
Parses a NuGet configuration from an io.Reader.
Parameters:
reader
(io.Reader): Reader containing XML content
Returns:
*types.NuGetConfig
: Parsed configuration objecterror
: Error if parsing fails
Example:
file, err := os.Open("NuGet.Config")
if err != nil {
log.Fatal(err)
}
defer file.Close()
config, err := api.ParseFromReader(file)
ParseFromFileWithPositions
func (a *API) ParseFromFileWithPositions(filePath string) (*parser.ParseResult, error)
Parses a configuration file while tracking element positions for position-aware editing.
Parameters:
filePath
(string): Path to the configuration file
Returns:
*parser.ParseResult
: Parse result with position informationerror
: Error if parsing fails
Example:
parseResult, err := api.ParseFromFileWithPositions("/path/to/NuGet.Config")
if err != nil {
log.Fatalf("Failed to parse with positions: %v", err)
}
editor := api.CreateConfigEditor(parseResult)
Finding Methods
FindConfigFile
func (a *API) FindConfigFile() (string, error)
Finds the first available NuGet configuration file in the system.
Returns:
string
: Path to the found configuration fileerror
: Error if no configuration file is found
Example:
configPath, err := api.FindConfigFile()
if err != nil {
log.Fatalf("No config file found: %v", err)
}
fmt.Printf("Found config: %s\n", configPath)
FindAllConfigFiles
func (a *API) FindAllConfigFiles() []string
Finds all available NuGet configuration files in the system.
Returns:
[]string
: Slice of paths to all found configuration files
Example:
configPaths := api.FindAllConfigFiles()
fmt.Printf("Found %d config files:\n", len(configPaths))
for _, path := range configPaths {
fmt.Printf(" - %s\n", path)
}
FindProjectConfig
func (a *API) FindProjectConfig(startDir string) (string, error)
Finds a project-level configuration file starting from the specified directory.
Parameters:
startDir
(string): Starting directory for the search
Returns:
string
: Path to the found project configuration fileerror
: Error if no project configuration file is found
Example:
projectConfig, err := api.FindProjectConfig("./my-project")
if err != nil {
log.Printf("No project config found: %v", err)
} else {
fmt.Printf("Project config: %s\n", projectConfig)
}
FindAndParseConfig
func (a *API) FindAndParseConfig() (*types.NuGetConfig, string, error)
Finds and parses the first available configuration file.
Returns:
*types.NuGetConfig
: Parsed configuration objectstring
: Path to the configuration file that was parsederror
: Error if no configuration file is found or parsing fails
Example:
config, configPath, err := api.FindAndParseConfig()
if err != nil {
log.Fatalf("Failed to find and parse config: %v", err)
}
fmt.Printf("Loaded config from: %s\n", configPath)
Package Source Management
AddPackageSource
func (a *API) AddPackageSource(config *types.NuGetConfig, key, value, protocolVersion string)
Adds or updates a package source in the configuration.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Unique identifier for the package sourcevalue
(string): URL or path to the package sourceprotocolVersion
(string): Protocol version (optional, can be empty)
Example:
api.AddPackageSource(config, "myFeed", "https://my-nuget-feed.com/v3/index.json", "3")
RemovePackageSource
func (a *API) RemovePackageSource(config *types.NuGetConfig, key string) bool
Removes a package source from the configuration.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Key of the package source to remove
Returns:
bool
: True if the source was found and removed, false otherwise
Example:
removed := api.RemovePackageSource(config, "myFeed")
if removed {
fmt.Println("Package source removed successfully")
}
GetPackageSource
func (a *API) GetPackageSource(config *types.NuGetConfig, key string) *types.PackageSource
Retrieves a specific package source by key.
Parameters:
config
(*types.NuGetConfig): Configuration object to searchkey
(string): Key of the package source to retrieve
Returns:
*types.PackageSource
: Package source if found, nil otherwise
Example:
source := api.GetPackageSource(config, "nuget.org")
if source != nil {
fmt.Printf("Source URL: %s\n", source.Value)
}
GetAllPackageSources
func (a *API) GetAllPackageSources(config *types.NuGetConfig) []types.PackageSource
Retrieves all package sources from the configuration.
Parameters:
config
(*types.NuGetConfig): Configuration object
Returns:
[]types.PackageSource
: Slice of all package sources
Example:
sources := api.GetAllPackageSources(config)
for _, source := range sources {
fmt.Printf("- %s: %s\n", source.Key, source.Value)
}
Package Source Status Management
EnablePackageSource
func (a *API) EnablePackageSource(config *types.NuGetConfig, key string)
Enables a package source by removing it from the disabled sources list.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Key of the package source to enable
DisablePackageSource
func (a *API) DisablePackageSource(config *types.NuGetConfig, key string)
Disables a package source by adding it to the disabled sources list.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Key of the package source to disable
IsPackageSourceDisabled
func (a *API) IsPackageSourceDisabled(config *types.NuGetConfig, key string) bool
Checks if a package source is disabled.
Parameters:
config
(*types.NuGetConfig): Configuration object to checkkey
(string): Key of the package source to check
Returns:
bool
: True if the source is disabled, false otherwise
Example:
if api.IsPackageSourceDisabled(config, "myFeed") {
fmt.Println("myFeed is disabled")
api.EnablePackageSource(config, "myFeed")
}
Credential Management
AddCredential
func (a *API) AddCredential(config *types.NuGetConfig, sourceKey, username, password string)
Adds credentials for a package source.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifysourceKey
(string): Key of the package sourceusername
(string): Username for authenticationpassword
(string): Password for authentication
Example:
api.AddCredential(config, "privateFeed", "myuser", "mypassword")
RemoveCredential
func (a *API) RemoveCredential(config *types.NuGetConfig, sourceKey string) bool
Removes credentials for a package source.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifysourceKey
(string): Key of the package source
Returns:
bool
: True if credentials were found and removed, false otherwise
GetCredential
func (a *API) GetCredential(config *types.NuGetConfig, sourceKey string) *types.SourceCredential
Retrieves credentials for a package source.
Parameters:
config
(*types.NuGetConfig): Configuration object to searchsourceKey
(string): Key of the package source
Returns:
*types.SourceCredential
: Credentials if found, nil otherwise
Configuration Options
AddConfigOption
func (a *API) AddConfigOption(config *types.NuGetConfig, key, value string)
Adds or updates a global configuration option.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Configuration option keyvalue
(string): Configuration option value
Example:
api.AddConfigOption(config, "globalPackagesFolder", "/custom/packages/path")
RemoveConfigOption
func (a *API) RemoveConfigOption(config *types.NuGetConfig, key string) bool
Removes a global configuration option.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Configuration option key to remove
Returns:
bool
: True if the option was found and removed, false otherwise
GetConfigOption
func (a *API) GetConfigOption(config *types.NuGetConfig, key string) string
Retrieves a global configuration option value.
Parameters:
config
(*types.NuGetConfig): Configuration object to searchkey
(string): Configuration option key
Returns:
string
: Configuration option value, empty string if not found
Active Package Source
SetActivePackageSource
func (a *API) SetActivePackageSource(config *types.NuGetConfig, key, value string)
Sets the active package source.
Parameters:
config
(*types.NuGetConfig): Configuration object to modifykey
(string): Key of the active package sourcevalue
(string): URL of the active package source
GetActivePackageSource
func (a *API) GetActivePackageSource(config *types.NuGetConfig) *types.PackageSource
Retrieves the active package source.
Parameters:
config
(*types.NuGetConfig): Configuration object to search
Returns:
*types.PackageSource
: Active package source if set, nil otherwise
Serialization and Persistence
SaveConfig
func (a *API) SaveConfig(config *types.NuGetConfig, filePath string) error
Saves a configuration object to a file.
Parameters:
config
(*types.NuGetConfig): Configuration object to savefilePath
(string): Path where to save the configuration file
Returns:
error
: Error if saving fails
Example:
err := api.SaveConfig(config, "/path/to/NuGet.Config")
if err != nil {
log.Fatalf("Failed to save config: %v", err)
}
SerializeToXML
func (a *API) SerializeToXML(config *types.NuGetConfig) (string, error)
Serializes a configuration object to XML string.
Parameters:
config
(*types.NuGetConfig): Configuration object to serialize
Returns:
string
: XML representation of the configurationerror
: Error if serialization fails
Example:
xmlString, err := api.SerializeToXML(config)
if err != nil {
log.Fatalf("Failed to serialize config: %v", err)
}
fmt.Println(xmlString)
Configuration Creation
CreateDefaultConfig
func (a *API) CreateDefaultConfig() *types.NuGetConfig
Creates a new configuration with default settings.
Returns:
*types.NuGetConfig
: New configuration with default package source
Example:
config := api.CreateDefaultConfig()
// config now contains nuget.org as the default source
InitializeDefaultConfig
func (a *API) InitializeDefaultConfig(filePath string) error
Creates and saves a default configuration to the specified path.
Parameters:
filePath
(string): Path where to create the configuration file
Returns:
error
: Error if creation or saving fails
Example:
err := api.InitializeDefaultConfig("/path/to/new/NuGet.Config")
if err != nil {
log.Fatalf("Failed to initialize config: %v", err)
}
Position-Aware Editing
CreateConfigEditor
func (a *API) CreateConfigEditor(parseResult *parser.ParseResult) *editor.ConfigEditor
Creates a position-aware configuration editor.
Parameters:
parseResult
(*parser.ParseResult): Parse result with position information
Returns:
*editor.ConfigEditor
: Configuration editor instance
Example:
parseResult, err := api.ParseFromFileWithPositions("/path/to/NuGet.Config")
if err != nil {
log.Fatal(err)
}
editor := api.CreateConfigEditor(parseResult)
err = editor.AddPackageSource("newSource", "https://example.com", "3")
if err != nil {
log.Fatal(err)
}
modifiedContent, err := editor.ApplyEdits()
if err != nil {
log.Fatal(err)
}
// Save the modified content
err = os.WriteFile("/path/to/NuGet.Config", modifiedContent, 0644)
Error Handling
All methods that can fail return an error as the last return value. Use the error handling utilities from the pkg/errors
package:
import "github.com/scagogogo/nuget-config-parser/pkg/errors"
config, err := api.ParseFromFile("config.xml")
if err != nil {
if errors.IsNotFoundError(err) {
// Handle file not found
} else if errors.IsParseError(err) {
// Handle parsing error
} else {
// Handle other errors
}
}
Best Practices
- Reuse API instances: Create one API instance and reuse it throughout your application
- Check errors: Always check and handle errors appropriately
- Use position-aware editing: For minimal file changes, use the position-aware editing features
- Validate inputs: Ensure package source keys are unique and URLs are valid
- Handle missing files: Use
FindConfigFile()
or create default configurations when files don't exist