github.com/openshift-online/ocm-sdk-go@v0.1.473/configuration/doc.go (about)

     1  /*
     2  Copyright (c) 2020 Red Hat, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8    http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package configuration provides a mechanism to load configuration from JSON or YAML files. The
    18  // typical use will be to create a configuration object and then load one or more configuration
    19  // sources:
    20  //
    21  //	// Load the configuration from a file:
    22  //	cfg, err := configuration.New().
    23  //		Load("myconfig.yaml").
    24  //		Build()
    25  //	if err != nil {
    26  //		...
    27  //	}
    28  //
    29  // Once the configuration is loaded it can be copied into an object containing the same tags
    30  // used by the YAML library:
    31  //
    32  //	// Copy the configuration into our object:
    33  //	type MyConfig struct {
    34  //		MyKey string `yaml:"mykey"`
    35  //		YouKey int `yaml:"yourkey"`
    36  //	}
    37  //	var myCfg MyConfig
    38  //	err = cfg.Populate(&myCfg)
    39  //	if err != nil {
    40  //		...
    41  //	}
    42  //
    43  // The advantage of using this configuration instead of using plain YAML is that configuration
    44  // sources can use the tags to reference environment variables, files and scripts. For example:
    45  //
    46  //	mykey: !variable MYVARIABLE
    47  //	yourkey: !file /my/file.txt
    48  //
    49  // The following tags are supported:
    50  //
    51  //	!file /my/file.txt - Is replaced by the content of the file `/my/file.txt`.
    52  //	!script myscript - Is replaced by the result of executing the `myscript` script.
    53  //	!trim mytext - Is replaced by the result of trimming white space from `mytext`.
    54  //	!variable MYVARIABLE - Is replaced by the content of the environment variable `MYVARIABLE`.
    55  //	!yaml mytext - Is replaced by the result of parsing `mytext` as YAML.
    56  //
    57  // Tag names can be abbreviated. For example these are all valid tags:
    58  //
    59  //	!f /my/file.txt - Replaced by the content of the `/my.file.txt` file.
    60  //	!s myscript - Replaced by the result of execution the `myscript` script.
    61  //	!v MYVARIABLE - Replaced by the content of the environment variablel `MYVARIABLE`.
    62  //
    63  // By default the tags replace the value of node they are applied to with a string. This will not
    64  // work for fields that are declared of other types in the configuration struct. In those cases it
    65  // is possible to add a suffix to the tag to indicate the type of the replacmenet. For example:
    66  //
    67  //	# A configuration with an integer loaded from an environment variable and a boolean
    68  //	# loaded from a file:
    69  //	myid: !variable/integer MYID
    70  //	myenabled: !file/boolean /my/enabled.txt
    71  //
    72  // This can be used with the following Go code:
    73  //
    74  //	type MyConfig struct {
    75  //		MyId      int  `yaml:"myid"`
    76  //		MyEnabled bool `yaml:"myenabled"`
    77  //	}
    78  //	var myCfg MyConfig
    79  //	err = cfg.Populate(&myCfg)
    80  //	if err != nil {
    81  //		...
    82  //	}
    83  //
    84  // Tags can be chained. For example to read a value from a file and trim white space from
    85  // the content:
    86  //
    87  //	mypassword: !file/trim mypassword.txt
    88  //
    89  // When multiple sources are configured (calling the Load method multiple times) they will all
    90  // be merged, and sources loaded later sources will override sources loaded earlier.
    91  package configuration