github.com/oam-dev/kubevela@v1.9.11/pkg/addon/reader_gitee.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  	"fmt"
    21  	"net/http"
    22  	"net/url"
    23  	"path"
    24  	"strings"
    25  
    26  	"github.com/pkg/errors"
    27  
    28  	"github.com/oam-dev/kubevela/pkg/utils"
    29  )
    30  
    31  var _ AsyncReader = &giteeReader{}
    32  
    33  // giteeHelper helps get addon's file by git
    34  type giteeHelper struct {
    35  	Client *Client
    36  	Meta   *utils.Content
    37  }
    38  
    39  // Client manages communication with the Gitee API
    40  type Client struct {
    41  	Client  *http.Client
    42  	BaseURL *url.URL
    43  }
    44  
    45  type giteeReader struct {
    46  	h *giteeHelper
    47  }
    48  
    49  // NewGiteeClient returns a new Gitee API client
    50  func NewGiteeClient(httpClient *http.Client, baseURL *url.URL) *Client {
    51  	if httpClient == nil {
    52  		httpClient = &http.Client{}
    53  	}
    54  	if baseURL == nil {
    55  		baseURL, _ = baseURL.Parse(DefaultGiteeURL)
    56  	}
    57  	return &Client{httpClient, baseURL}
    58  }
    59  
    60  // ListAddonMeta relative path to repoURL/basePath
    61  func (g *giteeReader) ListAddonMeta() (map[string]SourceMeta, error) {
    62  	subItems := make(map[string]SourceMeta)
    63  	_, items, err := g.h.readRepo("")
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	for _, item := range items {
    68  		// single addon
    69  		if item.GetType() != DirType {
    70  			continue
    71  		}
    72  		addonName := path.Base(item.GetPath())
    73  		addonMeta, err := g.listAddonMeta(g.RelativePath(item))
    74  		if err != nil {
    75  			return nil, errors.Wrapf(err, "fail to get addon meta of %s", addonName)
    76  		}
    77  		subItems[addonName] = SourceMeta{Name: addonName, Items: addonMeta}
    78  	}
    79  	return subItems, nil
    80  }
    81  
    82  func (g *giteeReader) listAddonMeta(dirPath string) ([]Item, error) {
    83  	_, items, err := g.h.readRepo(dirPath)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	res := make([]Item, 0)
    88  	for _, item := range items {
    89  		switch item.GetType() {
    90  		case FileType:
    91  			res = append(res, item)
    92  		case DirType:
    93  			subItems, err := g.listAddonMeta(g.RelativePath(item))
    94  			if err != nil {
    95  				return nil, err
    96  			}
    97  			res = append(res, subItems...)
    98  		}
    99  	}
   100  	return res, nil
   101  }
   102  
   103  // ReadFile read file content from gitee
   104  func (g *giteeReader) ReadFile(relativePath string) (content string, err error) {
   105  	file, _, err := g.h.readRepo(relativePath)
   106  	if err != nil {
   107  		return
   108  	}
   109  	if file == nil {
   110  		return "", fmt.Errorf("path %s is not a file", relativePath)
   111  	}
   112  	return file.GetContent()
   113  }
   114  
   115  func (g *giteeReader) RelativePath(item Item) string {
   116  	absPath := strings.Split(item.GetPath(), "/")
   117  	if g.h.Meta.GiteeContent.Path == "" {
   118  		return path.Join(absPath...)
   119  	}
   120  	base := strings.Split(g.h.Meta.GiteeContent.Path, "/")
   121  	return path.Join(absPath[len(base):]...)
   122  }