From 23a80b01b6bc8c9708c51964cb2865ea2babecbe Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Fri, 19 Jun 2020 17:08:51 +0200 Subject: [PATCH] add build tag to disable metrics --- cmd/portable.go | 4 +- cmd/portable_disabled.go | 4 +- cmd/root.go | 5 +-- config/config.go | 3 +- dataprovider/bolt.go | 3 +- dataprovider/bolt_disabled.go | 4 +- dataprovider/mysql.go | 4 +- dataprovider/mysql_disabled.go | 4 +- dataprovider/pgsql.go | 4 +- dataprovider/pgsql_disabled.go | 4 +- dataprovider/sqlite.go | 3 +- dataprovider/sqlite_disabled.go | 4 +- docker/sftpgo/alpine/Dockerfile | 2 +- docker/sftpgo/debian/Dockerfile | 2 +- docs/build-from-source.md | 13 ++++--- httpd/api_utils.go | 11 +++--- httpd/router.go | 4 +- httpd/web.go | 4 +- metrics/metrics.go | 14 +++++-- metrics/metrics_disabled.go | 67 +++++++++++++++++++++++++++++++++ service/service.go | 4 +- service/service_portable.go | 4 +- utils/utils.go | 5 --- utils/version.go | 51 ------------------------- version/version.go | 56 +++++++++++++++++++++++++++ vfs/gcsfs.go | 4 +- vfs/gcsfs_disabled.go | 4 +- vfs/s3fs.go | 3 +- vfs/s3fs_disabled.go | 4 +- 29 files changed, 189 insertions(+), 109 deletions(-) create mode 100644 metrics/metrics_disabled.go delete mode 100644 utils/version.go create mode 100644 version/version.go diff --git a/cmd/portable.go b/cmd/portable.go index bfc634c1..6e6a0dbb 100644 --- a/cmd/portable.go +++ b/cmd/portable.go @@ -16,7 +16,7 @@ import ( "github.com/drakkan/sftpgo/dataprovider" "github.com/drakkan/sftpgo/service" "github.com/drakkan/sftpgo/sftpd" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -141,7 +141,7 @@ Please take a look at the usage below to customize the serving parameters`, ) func init() { - utils.AddFeature("+portable") + version.AddFeature("+portable") portableCmd.Flags().StringVarP(&directoryToServe, "directory", "d", ".", "Path to the directory to serve. This can be an absolute path or a path relative to the current directory") diff --git a/cmd/portable_disabled.go b/cmd/portable_disabled.go index c374cae3..516cd0d5 100644 --- a/cmd/portable_disabled.go +++ b/cmd/portable_disabled.go @@ -2,8 +2,8 @@ package cmd -import "github.com/drakkan/sftpgo/utils" +import "github.com/drakkan/sftpgo/version" func init() { - utils.AddFeature("-portable") + version.AddFeature("-portable") } diff --git a/cmd/root.go b/cmd/root.go index f1007eae..7ed0efcc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/viper" "github.com/drakkan/sftpgo/config" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) const ( @@ -60,9 +60,8 @@ var ( ) func init() { - version := utils.GetAppVersion() rootCmd.Flags().BoolP("version", "v", false, "") - rootCmd.Version = version.GetVersionAsString() + rootCmd.Version = version.GetAsString() rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}} `) } diff --git a/config/config.go b/config/config.go index 20148ca7..fce5b35d 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/sftpd" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) const ( @@ -31,7 +32,7 @@ const ( var ( globalConf globalConfig - defaultBanner = fmt.Sprintf("SFTPGo_%v", utils.GetAppVersion().Version) + defaultBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version) ) type globalConfig struct { diff --git a/dataprovider/bolt.go b/dataprovider/bolt.go index 483be3ef..8742d1a4 100644 --- a/dataprovider/bolt.go +++ b/dataprovider/bolt.go @@ -14,6 +14,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -57,7 +58,7 @@ type compatUserV2 struct { } func init() { - utils.AddFeature("+bolt") + version.AddFeature("+bolt") } func initializeBoltProvider(basePath string) error { diff --git a/dataprovider/bolt_disabled.go b/dataprovider/bolt_disabled.go index a84260e2..69053794 100644 --- a/dataprovider/bolt_disabled.go +++ b/dataprovider/bolt_disabled.go @@ -5,11 +5,11 @@ package dataprovider import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-bolt") + version.AddFeature("-bolt") } func initializeBoltProvider(basePath string) error { diff --git a/dataprovider/mysql.go b/dataprovider/mysql.go index 80f211ea..134804ab 100644 --- a/dataprovider/mysql.go +++ b/dataprovider/mysql.go @@ -12,7 +12,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/drakkan/sftpgo/logger" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -45,7 +45,7 @@ type MySQLProvider struct { } func init() { - utils.AddFeature("+mysql") + version.AddFeature("+mysql") } func initializeMySQLProvider() error { diff --git a/dataprovider/mysql_disabled.go b/dataprovider/mysql_disabled.go index e004be9f..e122e7d9 100644 --- a/dataprovider/mysql_disabled.go +++ b/dataprovider/mysql_disabled.go @@ -5,11 +5,11 @@ package dataprovider import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-mysql") + version.AddFeature("-mysql") } func initializeMySQLProvider() error { diff --git a/dataprovider/pgsql.go b/dataprovider/pgsql.go index d8c2ba29..49bc76d3 100644 --- a/dataprovider/pgsql.go +++ b/dataprovider/pgsql.go @@ -11,7 +11,7 @@ import ( _ "github.com/lib/pq" "github.com/drakkan/sftpgo/logger" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -44,7 +44,7 @@ type PGSQLProvider struct { } func init() { - utils.AddFeature("+pgsql") + version.AddFeature("+pgsql") } func initializePGSQLProvider() error { diff --git a/dataprovider/pgsql_disabled.go b/dataprovider/pgsql_disabled.go index c9aeee32..983c548f 100644 --- a/dataprovider/pgsql_disabled.go +++ b/dataprovider/pgsql_disabled.go @@ -5,11 +5,11 @@ package dataprovider import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-pgsql") + version.AddFeature("-pgsql") } func initializePGSQLProvider() error { diff --git a/dataprovider/sqlite.go b/dataprovider/sqlite.go index a8d721a0..bd961a00 100644 --- a/dataprovider/sqlite.go +++ b/dataprovider/sqlite.go @@ -13,6 +13,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -69,7 +70,7 @@ type SQLiteProvider struct { } func init() { - utils.AddFeature("+sqlite") + version.AddFeature("+sqlite") } func initializeSQLiteProvider(basePath string) error { diff --git a/dataprovider/sqlite_disabled.go b/dataprovider/sqlite_disabled.go index 430f2a72..636c70cc 100644 --- a/dataprovider/sqlite_disabled.go +++ b/dataprovider/sqlite_disabled.go @@ -5,11 +5,11 @@ package dataprovider import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-sqlite") + version.AddFeature("-sqlite") } func initializeSQLiteProvider(basePath string) error { diff --git a/docker/sftpgo/alpine/Dockerfile b/docker/sftpgo/alpine/Dockerfile index 77a5cdd6..5236f022 100644 --- a/docker/sftpgo/alpine/Dockerfile +++ b/docker/sftpgo/alpine/Dockerfile @@ -7,7 +7,7 @@ ARG TAG ARG FEATURES # Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built. RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi) -RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo +RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo FROM alpine:latest diff --git a/docker/sftpgo/debian/Dockerfile b/docker/sftpgo/debian/Dockerfile index f602991e..1f3718c5 100644 --- a/docker/sftpgo/debian/Dockerfile +++ b/docker/sftpgo/debian/Dockerfile @@ -7,7 +7,7 @@ ARG TAG ARG FEATURES # Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built. RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi) -RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo +RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o sftpgo # now define the run environment FROM debian:latest diff --git a/docs/build-from-source.md b/docs/build-from-source.md index 1558f243..c7b906cc 100644 --- a/docs/build-from-source.md +++ b/docs/build-from-source.md @@ -11,12 +11,13 @@ Make sure [Git](https://git-scm.com/downloads) is installed on your machine and The following build tags are available: - `nogcs`, disable Google Cloud Storage backend, default enabled -- `nos3`, disable S3 Compabible Object Storage backends, , default enabled -- `nobolt`, disable Bolt data provider, , default enabled +- `nos3`, disable S3 Compabible Object Storage backends, default enabled +- `nobolt`, disable Bolt data provider, default enabled - `nomysql`, disable MySQL data provider, default enabled - `nopgsql`, disable PostgreSQL data provider, default enabled - `nosqlite`, disable SQLite data provider, default enabled - `noportable`, disable portable mode, default enabled +- `nometrics`, disable Prometheus metrics, default enabled If no build tag is specified the build will include the default features. @@ -27,18 +28,18 @@ The compiler is a build time only dependency. It is not required at runtime. Version info, such as git commit and build date, can be embedded setting the following string variables at build time: -- `github.com/drakkan/sftpgo/utils.commit` -- `github.com/drakkan/sftpgo/utils.date` +- `github.com/drakkan/sftpgo/version.commit` +- `github.com/drakkan/sftpgo/version.date` For example, you can build using the following command: ```bash -go build -i -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo +go build -i -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o sftpgo ``` You should get a version that includes git commit, build date and available features like this one: ```bash $ ./sftpgo -v -SFTPGo 0.9.6-dev-15298b0-dirty-2020-05-22T21:25:51Z -gcs -s3 +bolt +mysql +pgsql -sqlite +portable +SFTPGo 0.9.6-dev-b30614e-dirty-2020-06-19T11:04:56Z +metrics -gcs -s3 +bolt +mysql +pgsql -sqlite +portable ``` diff --git a/httpd/api_utils.go b/httpd/api_utils.go index 845bc3ec..b063e2dc 100644 --- a/httpd/api_utils.go +++ b/httpd/api_utils.go @@ -22,6 +22,7 @@ import ( "github.com/drakkan/sftpgo/httpclient" "github.com/drakkan/sftpgo/sftpd" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -370,21 +371,21 @@ func StartFolderQuotaScan(folder vfs.BaseVirtualFolder, expectedStatusCode int) } // GetVersion returns version details -func GetVersion(expectedStatusCode int) (utils.VersionInfo, []byte, error) { - var version utils.VersionInfo +func GetVersion(expectedStatusCode int) (version.Info, []byte, error) { + var appVersion version.Info var body []byte resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(versionPath), nil, "") if err != nil { - return version, body, err + return appVersion, body, err } defer resp.Body.Close() err = checkResponse(resp.StatusCode, expectedStatusCode) if err == nil && expectedStatusCode == http.StatusOK { - err = render.DecodeJSON(resp.Body, &version) + err = render.DecodeJSON(resp.Body, &appVersion) } else { body, _ = getResponseBody(resp) } - return version, body, err + return appVersion, body, err } // GetProviderStatus returns provider status diff --git a/httpd/router.go b/httpd/router.go index 068c7058..c3c14238 100644 --- a/httpd/router.go +++ b/httpd/router.go @@ -12,7 +12,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/metrics" "github.com/drakkan/sftpgo/sftpd" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) // GetHTTPRouter returns the configured HTTP handler @@ -55,7 +55,7 @@ func initializeRouter(staticFilesPath string, enableProfiler, enableWebAdmin boo metrics.AddMetricsEndpoint(metricsPath, router) router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) { - render.JSON(w, r, utils.GetAppVersion()) + render.JSON(w, r, version.Get()) }) router.Get(providerStatusPath, func(w http.ResponseWriter, r *http.Request) { diff --git a/httpd/web.go b/httpd/web.go index ed3a7d6b..8633027c 100644 --- a/httpd/web.go +++ b/httpd/web.go @@ -18,6 +18,7 @@ import ( "github.com/drakkan/sftpgo/dataprovider" "github.com/drakkan/sftpgo/sftpd" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" "github.com/drakkan/sftpgo/vfs" ) @@ -143,7 +144,6 @@ func loadTemplates(templatesPath string) { } func getBasePageData(title, currentURL string) basePage { - version := utils.GetAppVersion() return basePage{ Title: title, CurrentURL: currentURL, @@ -160,7 +160,7 @@ func getBasePageData(title, currentURL string) basePage { UsersTitle: pageUsersTitle, ConnectionsTitle: pageConnectionsTitle, FoldersTitle: pageFoldersTitle, - Version: version.GetVersionAsString(), + Version: version.GetAsString(), } } diff --git a/metrics/metrics.go b/metrics/metrics.go index ffcc2f74..c95dacca 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -1,3 +1,5 @@ +// +build !nometrics + // Package metrics provides Prometheus metrics support package metrics @@ -6,6 +8,8 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" + + "github.com/drakkan/sftpgo/version" ) const ( @@ -15,9 +19,8 @@ const ( loginMethodKeyAndKeyboardInt = "publickey+keyboard-interactive" ) -// AddMetricsEndpoint exposes metrics to the specified endpoint -func AddMetricsEndpoint(metricsPath string, handler chi.Router) { - handler.Handle(metricsPath, promhttp.Handler()) +func init() { + version.AddFeature("+metrics") } var ( @@ -393,6 +396,11 @@ var ( }) ) +// AddMetricsEndpoint exposes metrics to the specified endpoint +func AddMetricsEndpoint(metricsPath string, handler chi.Router) { + handler.Handle(metricsPath, promhttp.Handler()) +} + // TransferCompleted updates metrics after an upload or a download func TransferCompleted(bytesSent, bytesReceived int64, transferKind int, err error) { if transferKind == 0 { diff --git a/metrics/metrics_disabled.go b/metrics/metrics_disabled.go new file mode 100644 index 00000000..02e42357 --- /dev/null +++ b/metrics/metrics_disabled.go @@ -0,0 +1,67 @@ +// +build nometrics + +package metrics + +import ( + "github.com/go-chi/chi" + + "github.com/drakkan/sftpgo/version" +) + +func init() { + version.AddFeature("-metrics") +} + +// AddMetricsEndpoint exposes metrics to the specified endpoint +func AddMetricsEndpoint(metricsPath string, handler chi.Router) {} + +// TransferCompleted updates metrics after an upload or a download +func TransferCompleted(bytesSent, bytesReceived int64, transferKind int, err error) {} + +// S3TransferCompleted updates metrics after an S3 upload or a download +func S3TransferCompleted(bytes int64, transferKind int, err error) {} + +// S3ListObjectsCompleted updates metrics after an S3 list objects request terminates +func S3ListObjectsCompleted(err error) {} + +// S3CopyObjectCompleted updates metrics after an S3 copy object request terminates +func S3CopyObjectCompleted(err error) {} + +// S3DeleteObjectCompleted updates metrics after an S3 delete object request terminates +func S3DeleteObjectCompleted(err error) {} + +// S3HeadBucketCompleted updates metrics after an S3 head bucket request terminates +func S3HeadBucketCompleted(err error) {} + +// GCSTransferCompleted updates metrics after a GCS upload or a download +func GCSTransferCompleted(bytes int64, transferKind int, err error) {} + +// GCSListObjectsCompleted updates metrics after a GCS list objects request terminates +func GCSListObjectsCompleted(err error) {} + +// GCSCopyObjectCompleted updates metrics after a GCS copy object request terminates +func GCSCopyObjectCompleted(err error) {} + +// GCSDeleteObjectCompleted updates metrics after a GCS delete object request terminates +func GCSDeleteObjectCompleted(err error) {} + +// GCSHeadBucketCompleted updates metrics after a GCS head bucket request terminates +func GCSHeadBucketCompleted(err error) {} + +// SSHCommandCompleted update metrics after an SSH command terminates +func SSHCommandCompleted(err error) {} + +// UpdateDataProviderAvailability updates the metric for the data provider availability +func UpdateDataProviderAvailability(err error) {} + +// AddLoginAttempt increments the metrics for login attempts +func AddLoginAttempt(authMethod string) {} + +// AddLoginResult increments the metrics for login results +func AddLoginResult(authMethod string, err error) {} + +// HTTPRequestServed increments the metrics for HTTP requests +func HTTPRequestServed(status int) {} + +// UpdateActiveConnectionsSize sets the metric for active connections +func UpdateActiveConnectionsSize(size int) {} diff --git a/service/service.go b/service/service.go index ccc770f4..116af6ff 100644 --- a/service/service.go +++ b/service/service.go @@ -12,6 +12,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/sftpd" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) const ( @@ -54,9 +55,8 @@ func (s *Service) Start() error { logger.DisableLogger() } } - version := utils.GetAppVersion() logger.Info(logSender, "", "starting SFTPGo %v, config dir: %v, config file: %v, log max size: %v log max backups: %v "+ - "log max age: %v log verbose: %v, log compress: %v, profile: %v", version.GetVersionAsString(), s.ConfigDir, s.ConfigFile, + "log max age: %v log verbose: %v, log compress: %v, profile: %v", version.GetAsString(), s.ConfigDir, s.ConfigFile, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogVerbose, s.LogCompress, s.Profiler) // in portable mode we don't read configuration from file if s.PortableMode != 1 { diff --git a/service/service_portable.go b/service/service_portable.go index b05b50f6..681f60e0 100644 --- a/service/service_portable.go +++ b/service/service_portable.go @@ -19,6 +19,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/sftpd" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) // StartPortableMode starts the service in portable mode @@ -67,9 +68,8 @@ func (s *Service) StartPortableMode(sftpdPort int, enabledSSHCommands []string, } var mDNSService *zeroconf.Server if advertiseService { - version := utils.GetAppVersion() meta := []string{ - fmt.Sprintf("version=%v", version.GetVersionAsString()), + fmt.Sprintf("version=%v", version.GetAsString()), } if advertiseCredentials { logger.InfoToConsole("Advertising credentials via multicast DNS") diff --git a/utils/utils.go b/utils/utils.go index bd8bee7b..b899d665 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -62,11 +62,6 @@ func GetTimeFromMsecSinceEpoch(msec int64) time.Time { return time.Unix(0, msec*1000000) } -// GetAppVersion returns VersionInfo struct -func GetAppVersion() VersionInfo { - return versionInfo -} - // GetDurationAsString returns a string representation for a time.Duration func GetDurationAsString(d time.Duration) string { d = d.Round(time.Second) diff --git a/utils/version.go b/utils/version.go deleted file mode 100644 index e475ccb4..00000000 --- a/utils/version.go +++ /dev/null @@ -1,51 +0,0 @@ -package utils - -import "strings" - -const version = "0.9.6-dev" - -var ( - commit = "" - date = "" - versionInfo VersionInfo -) - -// VersionInfo defines version details -type VersionInfo struct { - Version string `json:"version"` - BuildDate string `json:"build_date"` - CommitHash string `json:"commit_hash"` - Features []string `json:"features"` -} - -// GetVersionAsString returns the string representation of the VersionInfo struct -func (v *VersionInfo) GetVersionAsString() string { - var sb strings.Builder - sb.WriteString(v.Version) - if len(v.CommitHash) > 0 { - sb.WriteString("-") - sb.WriteString(v.CommitHash) - } - if len(v.BuildDate) > 0 { - sb.WriteString("-") - sb.WriteString(v.BuildDate) - } - if len(v.Features) > 0 { - sb.WriteString(" ") - sb.WriteString(strings.Join(v.Features, " ")) - } - return sb.String() -} - -// AddFeature adds a feature description -func AddFeature(feature string) { - versionInfo.Features = append(versionInfo.Features, feature) -} - -func init() { - versionInfo = VersionInfo{ - Version: version, - CommitHash: commit, - BuildDate: date, - } -} diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..fb4cd39f --- /dev/null +++ b/version/version.go @@ -0,0 +1,56 @@ +package version + +import "strings" + +const version = "0.9.6-dev" + +var ( + commit = "" + date = "" + info Info +) + +// Info defines version details +type Info struct { + Version string `json:"version"` + BuildDate string `json:"build_date"` + CommitHash string `json:"commit_hash"` + Features []string `json:"features"` +} + +// GetAsString returns the string representation of the version +func GetAsString() string { + var sb strings.Builder + sb.WriteString(info.Version) + if len(info.CommitHash) > 0 { + sb.WriteString("-") + sb.WriteString(info.CommitHash) + } + if len(info.BuildDate) > 0 { + sb.WriteString("-") + sb.WriteString(info.BuildDate) + } + if len(info.Features) > 0 { + sb.WriteString(" ") + sb.WriteString(strings.Join(info.Features, " ")) + } + return sb.String() +} + +func init() { + info = Info{ + Version: version, + CommitHash: commit, + BuildDate: date, + } +} + +// AddFeature adds a feature description +func AddFeature(feature string) { + info.Features = append(info.Features, feature) +} + +// Get returns the Info struct +func Get() Info { + return info +} diff --git a/vfs/gcsfs.go b/vfs/gcsfs.go index fb638b67..4caa9673 100644 --- a/vfs/gcsfs.go +++ b/vfs/gcsfs.go @@ -21,7 +21,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/metrics" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) var ( @@ -41,7 +41,7 @@ type GCSFs struct { } func init() { - utils.AddFeature("+gcs") + version.AddFeature("+gcs") } // NewGCSFs returns an GCSFs object that allows to interact with Google Cloud Storage diff --git a/vfs/gcsfs_disabled.go b/vfs/gcsfs_disabled.go index f1515e2b..e90232e4 100644 --- a/vfs/gcsfs_disabled.go +++ b/vfs/gcsfs_disabled.go @@ -5,11 +5,11 @@ package vfs import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-gcs") + version.AddFeature("-gcs") } // NewGCSFs returns an error, GCS is disabled diff --git a/vfs/s3fs.go b/vfs/s3fs.go index b71263ba..76de994e 100644 --- a/vfs/s3fs.go +++ b/vfs/s3fs.go @@ -22,6 +22,7 @@ import ( "github.com/drakkan/sftpgo/logger" "github.com/drakkan/sftpgo/metrics" "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) // S3Fs is a Fs implementation for Amazon S3 compatible object storage. @@ -35,7 +36,7 @@ type S3Fs struct { } func init() { - utils.AddFeature("+s3") + version.AddFeature("+s3") } // NewS3Fs returns an S3Fs object that allows to interact with an s3 compatible diff --git a/vfs/s3fs_disabled.go b/vfs/s3fs_disabled.go index 2e39d39b..743b0d1f 100644 --- a/vfs/s3fs_disabled.go +++ b/vfs/s3fs_disabled.go @@ -5,11 +5,11 @@ package vfs import ( "errors" - "github.com/drakkan/sftpgo/utils" + "github.com/drakkan/sftpgo/version" ) func init() { - utils.AddFeature("-s3") + version.AddFeature("-s3") } // NewS3Fs returns an error, S3 is disabled