github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/config/v1/io.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package v1
    20  
    21  import (
    22  	"io/ioutil"
    23  	"path/filepath"
    24  
    25  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/commoncore"
    26  	"github.com/pkg/errors"
    27  	"sigs.k8s.io/yaml"
    28  )
    29  
    30  func ReadCoreFile(path string) (*Core, error) {
    31  	core, err := ioutil.ReadFile(filepath.Clean(path))
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return coreFromBytes(core)
    37  }
    38  
    39  func ReadFrom(from *[]byte) (*Core, error) {
    40  	return coreFromBytes(*from)
    41  }
    42  
    43  func ReadCoreFromBytes(core []byte) (*Core, error) {
    44  	return coreFromBytes(core)
    45  }
    46  
    47  func coreFromBytes(coreBytes []byte) (*Core, error) {
    48  	coreConfig := &Core{}
    49  	err := yaml.Unmarshal(coreBytes, coreConfig)
    50  	if err != nil {
    51  		// Check if peer.gossip.bootstrap needs to be converted
    52  		updatedCore, err := commoncore.ConvertBootstrapToArray(coreBytes)
    53  		if err != nil {
    54  			return nil, errors.Wrap(err, "failed to convert peer.gossip.bootstrap to string array")
    55  		}
    56  		err = yaml.Unmarshal(updatedCore, coreConfig)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  	}
    61  
    62  	return coreConfig, nil
    63  }