gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/config.go (about)

     1  package gosec
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  )
    10  
    11  const (
    12  	// Globals are applicable to all rules and used for general
    13  	// configuration settings for gosec.
    14  	Globals = "global"
    15  )
    16  
    17  // Config is used to provide configuration and customization to each of the rules.
    18  type Config map[string]interface{}
    19  
    20  // NewConfig initializes a new configuration instance. The configuration data then
    21  // needs to be loaded via c.ReadFrom(strings.NewReader("config data"))
    22  // or from a *os.File.
    23  func NewConfig() Config {
    24  	cfg := make(Config)
    25  	cfg[Globals] = make(map[string]string)
    26  	return cfg
    27  }
    28  
    29  // ReadFrom implements the io.ReaderFrom interface. This
    30  // should be used with io.Reader to load configuration from
    31  //file or from string etc.
    32  func (c Config) ReadFrom(r io.Reader) (int64, error) {
    33  	data, err := ioutil.ReadAll(r)
    34  	if err != nil {
    35  		return int64(len(data)), err
    36  	}
    37  	if err = json.Unmarshal(data, &c); err != nil {
    38  		return int64(len(data)), err
    39  	}
    40  	return int64(len(data)), nil
    41  }
    42  
    43  // WriteTo implements the io.WriteTo interface. This should
    44  // be used to save or print out the configuration information.
    45  func (c Config) WriteTo(w io.Writer) (int64, error) {
    46  	data, err := json.Marshal(c)
    47  	if err != nil {
    48  		return int64(len(data)), err
    49  	}
    50  	return io.Copy(w, bytes.NewReader(data))
    51  }
    52  
    53  // Get returns the configuration section for the supplied key
    54  func (c Config) Get(section string) (interface{}, error) {
    55  	settings, found := c[section]
    56  	if !found {
    57  		return nil, fmt.Errorf("Section %s not in configuration", section)
    58  	}
    59  	return settings, nil
    60  }
    61  
    62  // Set section in the configuration to specified value
    63  func (c Config) Set(section string, value interface{}) {
    64  	c[section] = value
    65  }
    66  
    67  // GetGlobal returns value associated with global configuration option
    68  func (c Config) GetGlobal(option string) (string, error) {
    69  	if globals, ok := c[Globals]; ok {
    70  		if settings, ok := globals.(map[string]string); ok {
    71  			if value, ok := settings[option]; ok {
    72  				return value, nil
    73  			}
    74  			return "", fmt.Errorf("global setting for %s not found", option)
    75  		}
    76  	}
    77  	return "", fmt.Errorf("no global config options found")
    78  
    79  }
    80  
    81  // SetGlobal associates a value with a global configuration ooption
    82  func (c Config) SetGlobal(option, value string) {
    83  	if globals, ok := c[Globals]; ok {
    84  		if settings, ok := globals.(map[string]string); ok {
    85  			settings[option] = value
    86  		}
    87  	}
    88  }