...

Source file src/github.com/prometheus/common/config/config.go

Documentation: github.com/prometheus/common/config

     1  // Copyright 2016 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // This package no longer handles safe yaml parsing. In order to
    15  // ensure correct yaml unmarshalling, use "yaml.UnmarshalStrict()".
    16  
    17  package config
    18  
    19  import (
    20  	"encoding/json"
    21  	"net/http"
    22  	"path/filepath"
    23  )
    24  
    25  const secretToken = "<secret>"
    26  
    27  // Secret special type for storing secrets.
    28  type Secret string
    29  
    30  // MarshalSecretValue if set to true will expose Secret type
    31  // through the marshal interfaces. Useful for outside projects
    32  // that load and marshal the Prometheus config.
    33  var MarshalSecretValue bool = false
    34  
    35  // MarshalYAML implements the yaml.Marshaler interface for Secrets.
    36  func (s Secret) MarshalYAML() (interface{}, error) {
    37  	if MarshalSecretValue {
    38  		return string(s), nil
    39  	}
    40  	if s != "" {
    41  		return secretToken, nil
    42  	}
    43  	return nil, nil
    44  }
    45  
    46  // UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets.
    47  func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
    48  	type plain Secret
    49  	return unmarshal((*plain)(s))
    50  }
    51  
    52  // MarshalJSON implements the json.Marshaler interface for Secret.
    53  func (s Secret) MarshalJSON() ([]byte, error) {
    54  	if MarshalSecretValue {
    55  		return json.Marshal(string(s))
    56  	}
    57  	if len(s) == 0 {
    58  		return json.Marshal("")
    59  	}
    60  	return json.Marshal(secretToken)
    61  }
    62  
    63  type Header map[string][]Secret
    64  
    65  func (h *Header) HTTPHeader() http.Header {
    66  	if h == nil || *h == nil {
    67  		return nil
    68  	}
    69  
    70  	header := make(http.Header)
    71  
    72  	for name, values := range *h {
    73  		var s []string
    74  		if values != nil {
    75  			s = make([]string, 0, len(values))
    76  			for _, value := range values {
    77  				s = append(s, string(value))
    78  			}
    79  		}
    80  		header[name] = s
    81  	}
    82  
    83  	return header
    84  }
    85  
    86  // DirectorySetter is a config type that contains file paths that may
    87  // be relative to the file containing the config.
    88  type DirectorySetter interface {
    89  	// SetDirectory joins any relative file paths with dir.
    90  	// Any paths that are empty or absolute remain unchanged.
    91  	SetDirectory(dir string)
    92  }
    93  
    94  // JoinDir joins dir and path if path is relative.
    95  // If path is empty or absolute, it is returned unchanged.
    96  func JoinDir(dir, path string) string {
    97  	if path == "" || filepath.IsAbs(path) {
    98  		return path
    99  	}
   100  	return filepath.Join(dir, path)
   101  }
   102  

View as plain text