github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/protocol/register.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package protocol
    16  
    17  import (
    18  	"embed"
    19  	"fmt"
    20  	"io/fs"
    21  	"path/filepath"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"github.com/erda-project/erda-infra/pkg/strutil"
    26  )
    27  
    28  type file struct {
    29  	isDir    bool
    30  	name     string
    31  	fullPath string
    32  	data     []byte
    33  }
    34  
    35  func newDir(fullPath string) *file {
    36  	return &file{isDir: true, name: filepath.Base(fullPath), fullPath: fullPath, data: nil}
    37  }
    38  func newFile(fullPath string, data []byte) *file {
    39  	return &file{isDir: false, name: filepath.Base(fullPath), fullPath: fullPath, data: data}
    40  }
    41  
    42  // MustRegisterProtocolsFromFS registry protocols from embed fs.
    43  func MustRegisterProtocolsFromFS(rootFS embed.FS) {
    44  	var files []*file
    45  	walkEmbedFS(rootFS, ".", &files)
    46  	// log
    47  	for _, file := range files {
    48  		logrus.Infof("register protocols from fs: fullPath: %s, isDir: %t", file.fullPath, file.isDir)
    49  	}
    50  	// register all protocols
    51  	registerAllProtocolsFromRootFSFiles(files)
    52  }
    53  
    54  func walkEmbedFS(rootFS embed.FS, fullPath string, files *[]*file) {
    55  	entries, err := fs.ReadDir(rootFS, fullPath)
    56  	if err != nil {
    57  		panic(fmt.Errorf("fullPath: %s, err: %v", fullPath, err))
    58  	}
    59  	for _, entry := range entries {
    60  		entryPath := filepath.Join(fullPath, entry.Name())
    61  		if !entry.IsDir() {
    62  			data, err := rootFS.ReadFile(entryPath)
    63  			if err != nil {
    64  				panic(fmt.Errorf("failed to read file, filePath: %s, err: %v", entryPath, err))
    65  			}
    66  			*files = append(*files, newFile(entryPath, data))
    67  			continue
    68  		}
    69  		*files = append(*files, newDir(entryPath))
    70  		walkEmbedFS(rootFS, entryPath, files)
    71  	}
    72  }
    73  
    74  func registerAllProtocolsFromRootFSFiles(files []*file) {
    75  	for _, file := range files {
    76  		if file.isDir {
    77  			continue
    78  		}
    79  		if !strutil.HasSuffixes(file.name, ".yml", ".yaml") {
    80  			continue
    81  		}
    82  		RegisterDefaultProtocols(file.data)
    83  	}
    84  }