...

Source file src/github.com/go-kivik/kivik/v4/config.go

Documentation: github.com/go-kivik/kivik/v4

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package kivik
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/go-kivik/kivik/v4/driver"
    19  )
    20  
    21  // Config represents all the config sections.
    22  type Config map[string]ConfigSection
    23  
    24  // ConfigSection represents all key/value pairs for a section of configuration.
    25  type ConfigSection map[string]string
    26  
    27  // Config returns the entire [server config], for the specified node.
    28  //
    29  // [server config]: http://docs.couchdb.org/en/stable/api/server/configuration.html#get--_node-node-name-_config
    30  func (c *Client) Config(ctx context.Context, node string) (Config, error) {
    31  	endQuery, err := c.startQuery()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	defer endQuery()
    36  	if configer, ok := c.driverClient.(driver.Configer); ok {
    37  		driverCf, err := configer.Config(ctx, node)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  		cf := Config{}
    42  		for k, v := range driverCf {
    43  			cf[k] = ConfigSection(v)
    44  		}
    45  		return cf, nil
    46  	}
    47  	return nil, errConfigNotImplemented
    48  }
    49  
    50  // ConfigSection returns the requested server [config section] for the specified node.
    51  //
    52  // [section]: http://docs.couchdb.org/en/stable/api/server/configuration.html#node-node-name-config-section
    53  func (c *Client) ConfigSection(ctx context.Context, node, section string) (ConfigSection, error) {
    54  	endQuery, err := c.startQuery()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	defer endQuery()
    59  	if configer, ok := c.driverClient.(driver.Configer); ok {
    60  		sec, err := configer.ConfigSection(ctx, node, section)
    61  		return ConfigSection(sec), err
    62  	}
    63  	return nil, errConfigNotImplemented
    64  }
    65  
    66  // ConfigValue returns a single [config value] for the specified node.
    67  //
    68  // [config value]: http://docs.couchdb.org/en/stable/api/server/configuration.html#get--_node-node-name-_config-section-key
    69  func (c *Client) ConfigValue(ctx context.Context, node, section, key string) (string, error) {
    70  	endQuery, err := c.startQuery()
    71  	if err != nil {
    72  		return "", err
    73  	}
    74  	defer endQuery()
    75  	if configer, ok := c.driverClient.(driver.Configer); ok {
    76  		return configer.ConfigValue(ctx, node, section, key)
    77  	}
    78  	return "", errConfigNotImplemented
    79  }
    80  
    81  // SetConfigValue sets the server's [config value] on the specified node, creating
    82  // the key if it doesn't exist. It returns the old value.
    83  //
    84  // [config value]: http://docs.couchdb.org/en/stable/api/server/configuration.html#put--_node-node-name-_config-section-key
    85  func (c *Client) SetConfigValue(ctx context.Context, node, section, key, value string) (string, error) {
    86  	endQuery, err := c.startQuery()
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  	defer endQuery()
    91  	if configer, ok := c.driverClient.(driver.Configer); ok {
    92  		return configer.SetConfigValue(ctx, node, section, key, value)
    93  	}
    94  	return "", errConfigNotImplemented
    95  }
    96  
    97  // DeleteConfigKey deletes the [configuration key] and associated value from the
    98  // specified node. It returns the old value.
    99  //
   100  // [configuration key]: http://docs.couchdb.org/en/stable/api/server/configuration.html#delete--_node-node-name-_config-section-key
   101  func (c *Client) DeleteConfigKey(ctx context.Context, node, section, key string) (string, error) {
   102  	endQuery, err := c.startQuery()
   103  	if err != nil {
   104  		return "", err
   105  	}
   106  	defer endQuery()
   107  	if configer, ok := c.driverClient.(driver.Configer); ok {
   108  		return configer.DeleteConfigKey(ctx, node, section, key)
   109  	}
   110  	return "", errConfigNotImplemented
   111  }
   112  

View as plain text