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