github.com/oam-dev/kubevela@v1.9.11/pkg/addon/reader_memory.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package addon
    18  
    19  import (
    20  	"path/filepath"
    21  	"strings"
    22  
    23  	"helm.sh/helm/v3/pkg/chart/loader"
    24  )
    25  
    26  // MemoryReader is async reader for memory data
    27  type MemoryReader struct {
    28  	Name     string
    29  	Files    []*loader.BufferedFile
    30  	fileData map[string]string
    31  }
    32  
    33  // ListAddonMeta list all metadata of helm repo registry
    34  func (l *MemoryReader) ListAddonMeta() (map[string]SourceMeta, error) {
    35  	metas := SourceMeta{Name: l.Name}
    36  	for _, f := range l.Files {
    37  		metas.Items = append(metas.Items, OSSItem{tp: "file", name: f.Name})
    38  		if l.fileData == nil {
    39  			l.fileData = make(map[string]string)
    40  		}
    41  		l.fileData[f.Name] = string(f.Data)
    42  	}
    43  	return map[string]SourceMeta{l.Name: metas}, nil
    44  }
    45  
    46  // ReadFile ready file from memory
    47  func (l *MemoryReader) ReadFile(path string) (string, error) {
    48  	if file, ok := l.fileData[path]; ok {
    49  		return file, nil
    50  	}
    51  	return l.fileData[strings.TrimPrefix(path, l.Name+"/")], nil
    52  }
    53  
    54  // RelativePath calculate the relative path of one file
    55  func (l *MemoryReader) RelativePath(item Item) string {
    56  	if strings.HasPrefix(item.GetName(), l.Name) {
    57  		return item.GetName()
    58  	}
    59  	return filepath.Join(l.Name, item.GetName())
    60  }