diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..41c2212 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/version-go/ldflags + +go 1.13 diff --git a/version.go b/version.go new file mode 100644 index 0000000..1a751f5 --- /dev/null +++ b/version.go @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020 Alex + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @author Alex + * @copyright Copyright (c) 2020 Alex + * @since 03.10.2020 + * + */ + +package ldflags + +type buildInfo struct { + version string + hash string + time string +} + +var ( + buildVersion = "unknown" + buildHash = "unknown" + buildTime = "unknown" + info *buildInfo +) + +func init() { + info = New() +} + +// New works as a singleton +func New() *buildInfo { + if info == nil { + info = &buildInfo{ + buildVersion, buildHash, buildTime, + } + } + return info +} + +// Version returns build version if provided, "unknown" otherwise +func Version() string { + return info.version +} + +// Build returns build hash if provided, "unknown" otherwise +func Build() string { + return info.hash +} + +// Time returns build time if provided, "unknown" otherwise +func Time() string { + return info.time +}