mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
94 lines
2.6 KiB
9 years ago
|
/*
|
||
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package registry
|
||
|
|
||
|
import (
|
||
|
"github.com/google/go-github/github"
|
||
|
|
||
|
"fmt"
|
||
|
"log"
|
||
|
)
|
||
|
|
||
9 years ago
|
// GithubRegistry implements the Registry interface that talks to github.
|
||
9 years ago
|
type GithubRegistry struct {
|
||
|
owner string
|
||
|
repository string
|
||
|
client *github.Client
|
||
|
}
|
||
|
|
||
9 years ago
|
// NewGithubRegistry creates a Registry that can be used to talk to github.
|
||
9 years ago
|
func NewGithubRegistry(owner string, repository string) *GithubRegistry {
|
||
9 years ago
|
return &GithubRegistry{
|
||
|
owner: owner,
|
||
|
repository: repository,
|
||
|
client: github.NewClient(nil),
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// List the types from the Registry.
|
||
9 years ago
|
func (g *GithubRegistry) List() ([]Type, error) {
|
||
|
// First list all the types at the top level.
|
||
|
types, err := g.getDirs(TypesDir)
|
||
|
if err != nil {
|
||
|
log.Printf("Failed to list types : %v", err)
|
||
|
return nil, err
|
||
|
}
|
||
|
var retTypes []Type
|
||
|
for _, t := range types {
|
||
|
// Then we need to fetch the versions (directories for this type)
|
||
|
versions, err := g.getDirs(TypesDir + "/" + t)
|
||
|
if err != nil {
|
||
|
log.Printf("Failed to fetch versions for type: %s", t)
|
||
|
return nil, err
|
||
|
}
|
||
|
for _, v := range versions {
|
||
|
retTypes = append(retTypes, Type{Name: t, Version: v})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return retTypes, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// GetURL fetches the download URL for a given Type.
|
||
9 years ago
|
func (g *GithubRegistry) GetURL(t Type) (string, error) {
|
||
|
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, TypesDir+"/"+t.Name+"/"+t.Version, nil)
|
||
|
if err != nil {
|
||
|
log.Printf("Failed to list types : %v", err)
|
||
|
return "", err
|
||
|
}
|
||
|
for _, f := range dc {
|
||
|
if *f.Type == "file" {
|
||
9 years ago
|
if *f.Name == t.Name+".jinja" || *f.Name == t.Name+".py" {
|
||
9 years ago
|
return *f.DownloadURL, nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return "", fmt.Errorf("Can not find type %s:%s", t.Name, t.Version)
|
||
|
}
|
||
|
|
||
|
func (g *GithubRegistry) getDirs(dir string) ([]string, error) {
|
||
9 years ago
|
_, dc, _, err := g.client.Repositories.GetContents(g.owner, g.repository, dir, nil)
|
||
9 years ago
|
if err != nil {
|
||
|
log.Printf("Failed to call ListRefs : %v", err)
|
||
|
return nil, err
|
||
|
}
|
||
|
var dirs []string
|
||
|
for _, entry := range dc {
|
||
|
if *entry.Type == "dir" {
|
||
|
dirs = append(dirs, *entry.Name)
|
||
|
}
|
||
|
}
|
||
|
return dirs, nil
|
||
|
}
|