You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.3 KiB

  1. [![](https://img.shields.io/github/tag/magiconair/properties.svg?style=flat-square&label=release)](https://github.com/magiconair/properties/releases)
  2. [![Travis CI Status](https://img.shields.io/travis/magiconair/properties.svg?branch=master&style=flat-square&label=travis)](https://travis-ci.org/magiconair/properties)
  3. [![CircleCI Status](https://img.shields.io/circleci/project/github/magiconair/properties.svg?label=circle+ci&style=flat-square)](https://circleci.com/gh/magiconair/properties)
  4. [![License](https://img.shields.io/badge/License-BSD%202--Clause-orange.svg?style=flat-square)](https://raw.githubusercontent.com/magiconair/properties/master/LICENSE)
  5. [![GoDoc](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](http://godoc.org/github.com/magiconair/properties)
  6. # Overview
  7. #### Please run `git pull --tags` to update the tags. See [below](#updated-git-tags) why.
  8. properties is a Go library for reading and writing properties files.
  9. It supports reading from multiple files or URLs and Spring style recursive
  10. property expansion of expressions like `${key}` to their corresponding value.
  11. Value expressions can refer to other keys like in `${key}` or to environment
  12. variables like in `${USER}`. Filenames can also contain environment variables
  13. like in `/home/${USER}/myapp.properties`.
  14. Properties can be decoded into structs, maps, arrays and values through
  15. struct tags.
  16. Comments and the order of keys are preserved. Comments can be modified
  17. and can be written to the output.
  18. The properties library supports both ISO-8859-1 and UTF-8 encoded data.
  19. Starting from version 1.3.0 the behavior of the MustXXX() functions is
  20. configurable by providing a custom `ErrorHandler` function. The default has
  21. changed from `panic` to `log.Fatal` but this is configurable and custom
  22. error handling functions can be provided. See the package documentation for
  23. details.
  24. Read the full documentation on [![GoDoc](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](http://godoc.org/github.com/magiconair/properties)
  25. ## Getting Started
  26. ```go
  27. import (
  28. "flag"
  29. "github.com/magiconair/properties"
  30. )
  31. func main() {
  32. // init from a file
  33. p := properties.MustLoadFile("${HOME}/config.properties", properties.UTF8)
  34. // or multiple files
  35. p = properties.MustLoadFiles([]string{
  36. "${HOME}/config.properties",
  37. "${HOME}/config-${USER}.properties",
  38. }, properties.UTF8, true)
  39. // or from a map
  40. p = properties.LoadMap(map[string]string{"key": "value", "abc": "def"})
  41. // or from a string
  42. p = properties.MustLoadString("key=value\nabc=def")
  43. // or from a URL
  44. p = properties.MustLoadURL("http://host/path")
  45. // or from multiple URLs
  46. p = properties.MustLoadURL([]string{
  47. "http://host/config",
  48. "http://host/config-${USER}",
  49. }, true)
  50. // or from flags
  51. p.MustFlag(flag.CommandLine)
  52. // get values through getters
  53. host := p.MustGetString("host")
  54. port := p.GetInt("port", 8080)
  55. // or through Decode
  56. type Config struct {
  57. Host string `properties:"host"`
  58. Port int `properties:"port,default=9000"`
  59. Accept []string `properties:"accept,default=image/png;image;gif"`
  60. Timeout time.Duration `properties:"timeout,default=5s"`
  61. }
  62. var cfg Config
  63. if err := p.Decode(&cfg); err != nil {
  64. log.Fatal(err)
  65. }
  66. }
  67. ```
  68. ## Installation and Upgrade
  69. ```
  70. $ go get -u github.com/magiconair/properties
  71. ```
  72. ## License
  73. 2 clause BSD license. See [LICENSE](https://github.com/magiconair/properties/blob/master/LICENSE) file for details.
  74. ## ToDo
  75. * Dump contents with passwords and secrets obscured
  76. ## Updated Git tags
  77. #### 13 Feb 2018
  78. I realized that all of the git tags I had pushed before v1.7.5 were lightweight tags
  79. and I've only recently learned that this doesn't play well with `git describe` 😞
  80. I have replaced all lightweight tags with signed tags using this script which should
  81. retain the commit date, name and email address. Please run `git pull --tags` to update them.
  82. Worst case you have to reclone the repo.
  83. ```shell
  84. #!/bin/bash
  85. tag=$1
  86. echo "Updating $tag"
  87. date=$(git show ${tag}^0 --format=%aD | head -1)
  88. email=$(git show ${tag}^0 --format=%aE | head -1)
  89. name=$(git show ${tag}^0 --format=%aN | head -1)
  90. GIT_COMMITTER_DATE="$date" GIT_COMMITTER_NAME="$name" GIT_COMMITTER_EMAIL="$email" git tag -s -f ${tag} ${tag}^0 -m ${tag}
  91. ```
  92. I apologize for the inconvenience.
  93. Frank