version.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. // Package version defines SFTPGo version details
  15. package version
  16. import "strings"
  17. const (
  18. version = "2.6.6"
  19. appName = "SFTPGo"
  20. )
  21. var (
  22. commit = ""
  23. date = ""
  24. info Info
  25. )
  26. var (
  27. config string
  28. )
  29. // Info defines version details
  30. type Info struct {
  31. Version string `json:"version"`
  32. BuildDate string `json:"build_date"`
  33. CommitHash string `json:"commit_hash"`
  34. Features []string `json:"features"`
  35. }
  36. // GetAsString returns the string representation of the version
  37. func GetAsString() string {
  38. var sb strings.Builder
  39. sb.WriteString(info.Version)
  40. if info.CommitHash != "" {
  41. sb.WriteString("-")
  42. sb.WriteString(info.CommitHash)
  43. }
  44. if info.BuildDate != "" {
  45. sb.WriteString("-")
  46. sb.WriteString(info.BuildDate)
  47. }
  48. if len(info.Features) > 0 {
  49. sb.WriteString(" ")
  50. sb.WriteString(strings.Join(info.Features, " "))
  51. }
  52. return sb.String()
  53. }
  54. func init() {
  55. info = Info{
  56. Version: version,
  57. CommitHash: commit,
  58. BuildDate: date,
  59. }
  60. }
  61. // AddFeature adds a feature description
  62. func AddFeature(feature string) {
  63. info.Features = append(info.Features, feature)
  64. }
  65. // Get returns the Info struct
  66. func Get() Info {
  67. return info
  68. }
  69. // SetConfig sets the version configuration
  70. func SetConfig(val string) {
  71. config = val
  72. }
  73. // GetServerVersion returns the server version according to the configuration
  74. // and the provided parameters.
  75. func GetServerVersion(separator string, addHash bool) string {
  76. var sb strings.Builder
  77. sb.WriteString(appName)
  78. if config != "short" {
  79. sb.WriteString(separator)
  80. sb.WriteString(info.Version)
  81. }
  82. if addHash {
  83. sb.WriteString(separator)
  84. sb.WriteString(info.CommitHash)
  85. }
  86. return sb.String()
  87. }
  88. // GetVersionHash returns the server identification string with the commit hash.
  89. func GetVersionHash() string {
  90. var sb strings.Builder
  91. sb.WriteString(appName)
  92. sb.WriteString("-")
  93. sb.WriteString(info.CommitHash)
  94. return sb.String()
  95. }