Documentation
¶
Overview ¶
Package version provides version management and license handling for Go applications.
Overview ¶
This package offers a comprehensive solution for managing application version information, including build metadata, release versions, license information, and Go version constraints. It supports multiple open-source licenses and provides formatted output for version and license information.
Features ¶
- Version information management (release, build, date, author)
- Multiple license support (MIT, Apache, GPL, LGPL, AGPL, Mozilla, Creative Commons, etc.)
- Go version constraint validation
- Automatic package path extraction via reflection
- Thread-safe operations
- Formatted output for headers, info, and license text
Basic Usage ¶
Creating a version instance:
import "github.com/nabbar/golib/version"
type MyStruct struct{}
v := version.NewVersion(
version.License_MIT, // License type
"MyApp", // Package name
"My Application Description", // Description
"2024-01-15T10:30:00Z", // Build date (RFC3339)
"abc123def", // Build hash
"v1.2.3", // Release version
"John Doe", // Author
"MYAPP", // Prefix for environment variables
MyStruct{}, // Empty struct for reflection
0, // Number of parent packages to traverse
)
Retrieving Version Information ¶
// Get formatted header fmt.Println(v.GetHeader()) // Get detailed info fmt.Println(v.GetInfo()) // Get individual fields release := v.GetRelease() build := v.GetBuild() date := v.GetDate() author := v.GetAuthor()
License Management ¶
The package supports multiple license types:
- MIT License
- Apache License v2.0
- GNU GPL v3
- GNU Affero GPL v3
- GNU Lesser GPL v3
- Mozilla Public License v2.0
- Unlicense
- Creative Commons Zero v1.0
- Creative Commons Attribution v4.0
- Creative Commons Attribution-ShareAlike v4.0
- SIL Open Font License v1.1
Retrieving license information:
// Get license name name := v.GetLicenseName() // Get full license text legal := v.GetLicenseLegal() // Get license boilerplate (for file headers) boiler := v.GetLicenseBoiler() // Get complete license (boilerplate + legal text) full := v.GetLicenseFull() // Support for multiple licenses combined := v.GetLicenseLegal( version.License_Apache_v2, version.License_MIT, )
Go Version Constraints ¶
Validate that the application is running with a compatible Go version:
// Require Go >= 1.18
if err := v.CheckGo("1.18", ">="); err != nil {
log.Fatal(err)
}
// Require exact Go version
if err := v.CheckGo("1.21.0", "=="); err != nil {
log.Fatal(err)
}
// Pessimistic constraint (compatible with minor versions)
if err := v.CheckGo("1.20", "~>"); err != nil {
log.Fatal(err)
}
Supported constraint operators:
- "==" : Exact version match
- "!=" : Not equal
- ">" : Greater than
- ">=" : Greater than or equal
- "<" : Less than
- "<=" : Less than or equal
- "~>" : Pessimistic constraint (allows patch-level changes)
Package Path Extraction ¶
The package uses reflection to automatically extract the package path. The numSubPackage parameter controls how many parent directories to traverse:
// numSubPackage = 0: github.com/myorg/myapp/cmd // numSubPackage = 1: github.com/myorg/myapp // numSubPackage = 2: github.com/myorg
Error Handling ¶
The package uses the github.com/nabbar/golib/errors package for error handling. Error codes:
- ErrorParamEmpty: Required parameter is empty
- ErrorGoVersionInit: Failed to initialize Go version constraint
- ErrorGoVersionRuntime: Failed to extract runtime Go version
- ErrorGoVersionConstraint: Go version constraint not satisfied
Thread Safety ¶
All methods are safe for concurrent use. The Version interface is read-only after creation, making it inherently thread-safe.
Testing ¶
The package includes comprehensive tests with high code coverage (>93%). Run tests with:
make test # Run all tests make test-race # Run with race detector make test-coverage # Generate coverage report
Dependencies ¶
- github.com/hashicorp/go-version: Version constraint parsing
- github.com/nabbar/golib/errors: Error handling
See Also ¶
- github.com/nabbar/golib/errors: Error handling package
- github.com/hashicorp/go-version: Version constraint library
License ¶
This package is released under the MIT License. See the LICENSE file for details.
Index ¶
Constants ¶
const ( // ErrorParamEmpty indicates that a required parameter is empty or missing. ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgVersion // ErrorGoVersionInit indicates a failure to initialize the Go version constraint parser. // This typically occurs when the constraint string is malformed. ErrorGoVersionInit // ErrorGoVersionRuntime indicates a failure to extract the current Go runtime version. // This is rare and usually indicates a problem with the Go installation. ErrorGoVersionRuntime // ErrorGoVersionConstraint indicates that the current Go version does not satisfy // the required version constraint. ErrorGoVersionConstraint )
Error codes for the version package. These codes are used with the github.com/nabbar/golib/errors package.
const ( // License_MIT represents the MIT License - a permissive license. License_MIT license = iota // License_GNU_GPL_v3 represents the GNU General Public License v3.0 - a strong copyleft license. License_GNU_GPL_v3 // License_GNU_Affero_GPL_v3 represents the GNU Affero General Public License v3.0 - GPL with network use clause. License_GNU_Affero_GPL_v3 // License_GNU_Lesser_GPL_v3 represents the GNU Lesser General Public License v3.0 - weak copyleft for libraries. License_GNU_Lesser_GPL_v3 // License_Mozilla_PL_v2 represents the Mozilla Public License v2.0 - weak copyleft, file-level. License_Mozilla_PL_v2 // License_Apache_v2 represents the Apache License v2.0 - permissive with patent grant. License_Apache_v2 // License_Unlicense represents the Unlicense - public domain dedication. License_Unlicense // License_Creative_Common_Zero_v1 represents CC0 1.0 Universal - public domain dedication. License_Creative_Common_Zero_v1 // License_Creative_Common_Attribution_v4_int represents CC BY 4.0 - requires attribution. License_Creative_Common_Attribution_v4_int License_Creative_Common_Attribution_Share_Alike_v4_int // License_SIL_Open_Font_1_1 represents the SIL Open Font License v1.1 - for fonts. License_SIL_Open_Font_1_1 )
Supported open-source license types. These constants can be passed to NewVersion to specify the application's license.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Version ¶
type Version interface {
// CheckGo validates that the current Go runtime version satisfies the given constraint.
// RequireGoVersion is the version to check against (e.g., "1.18", "1.21.0").
// RequireGoContraint is the operator (e.g., ">=", "==", "~>").
// Returns an error if the constraint is not satisfied or if parsing fails.
CheckGo(RequireGoVersion, RequireGoContraint string) liberr.Error
// GetAppId returns a unique application identifier including version, OS, and architecture.
GetAppId() string
// GetAuthor returns the author information with source package path.
GetAuthor() string
// GetBuild returns the build identifier (typically a git commit hash).
GetBuild() string
// GetDate returns the build date as a formatted string.
GetDate() string
// GetTime returns the build date as a time.Time object.
GetTime() time.Time
// GetDescription returns the application description.
GetDescription() string
// GetHeader returns a formatted header string with package, release, and build info.
GetHeader() string
// GetInfo returns detailed version information including release, build, and date.
GetInfo() string
// GetPackage returns the package name.
GetPackage() string
// GetRootPackagePath returns the root package path extracted via reflection.
GetRootPackagePath() string
// GetPrefix returns the uppercase prefix for environment variables.
GetPrefix() string
// GetRelease returns the release version string.
GetRelease() string
// GetLicenseName returns the name of the primary license.
GetLicenseName() string
// GetLicenseLegal returns the full legal text of the license(s).
// Additional licenses can be provided to combine multiple license texts.
GetLicenseLegal(addMoreLicence ...license) string
// GetLicenseFull returns the complete license including boilerplate and legal text.
// Additional licenses can be provided to combine multiple licenses.
GetLicenseFull(addMoreLicence ...license) string
// GetLicenseBoiler returns the license boilerplate suitable for file headers.
// Additional licenses can be provided to combine multiple boilerplates.
GetLicenseBoiler(addMoreLicence ...license) string
// PrintInfo prints version information to stderr.
PrintInfo()
// PrintLicense prints license boilerplate to stderr.
// Additional licenses can be provided to print multiple licenses.
PrintLicense(addlicence ...license)
}
Version is the main interface for accessing version and license information. All methods are safe for concurrent use.
func NewVersion ¶
func NewVersion(License license, Package, Description, Date, Build, Release, Author, Prefix string, emptyInterface interface{}, numSubPackage int) Version
NewVersion creates a new Version instance with the provided information.
Parameters:
- License: The primary license type (e.g., License_MIT, License_Apache_v2)
- Package: The package/application name (use "" or "noname" to auto-detect from reflection)
- Description: A description of the application
- Date: Build date in RFC3339 format (e.g., "2024-01-15T10:30:00Z")
- Build: Build identifier, typically a git commit hash
- Release: Release version string (e.g., "v1.2.3")
- Author: Author name or organization
- Prefix: Prefix for environment variables (will be converted to uppercase)
- emptyInterface: An empty struct instance used for reflection to extract package path
- numSubPackage: Number of parent directories to traverse from the reflected package path (0 = current package, 1 = parent, 2 = grandparent, etc.)
Returns a Version interface for accessing version and license information.
Example:
type MyApp struct{}
v := NewVersion(
License_MIT,
"MyApp",
"My Application",
"2024-01-15T10:30:00Z",
"abc123",
"v1.0.0",
"John Doe",
"MYAPP",
MyApp{},
0,
)