From 55cf60501d7e42d9a225eebb4aaa1993f12fe132 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 3 Oct 2020 17:43:46 +0200 Subject: [PATCH] All initial funcs plus module info. --- go.mod | 3 +++ version.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 go.mod create mode 100644 version.go 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 +}