mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-08-31 16:39:20 +00:00
art: Expose to sdk upgrade/list events
This commit is contained in:
committed by
Itxaka
parent
fdad13bd10
commit
212cf5132a
67
pkg/github/releases.go
Normal file
67
pkg/github/releases.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-github/v40/github"
|
||||
"golang.org/x/oauth2"
|
||||
"gopkg.in/yaml.v1"
|
||||
)
|
||||
|
||||
func newHTTPClient(ctx context.Context, token string) *http.Client {
|
||||
if token == "" {
|
||||
return http.DefaultClient
|
||||
}
|
||||
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
|
||||
return oauth2.NewClient(ctx, src)
|
||||
}
|
||||
|
||||
func FindReleases(ctx context.Context, token, slug string) ([]string, error) {
|
||||
hc := newHTTPClient(ctx, token)
|
||||
cli := github.NewClient(hc)
|
||||
|
||||
repo := strings.Split(slug, "/")
|
||||
if len(repo) != 2 || repo[0] == "" || repo[1] == "" {
|
||||
return nil, fmt.Errorf("Invalid slug format. It should be 'owner/name': %s", slug)
|
||||
}
|
||||
|
||||
rels, res, err := cli.Repositories.ListReleases(ctx, repo[0], repo[1], nil)
|
||||
if err != nil {
|
||||
log.Println("API returned an error response:", err)
|
||||
if res != nil && res.StatusCode == 404 {
|
||||
// 404 means repository not found or release not found. It's not an error here.
|
||||
err = nil
|
||||
log.Println("API returned 404. Repository or release not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
versions := []string{}
|
||||
for _, rel := range rels {
|
||||
if strings.HasPrefix(*rel.Name, "v") {
|
||||
versions = append(versions, *rel.Name)
|
||||
}
|
||||
}
|
||||
return versions, nil
|
||||
}
|
||||
|
||||
func ListOutput(rels []string, output string) []string {
|
||||
switch strings.ToLower(output) {
|
||||
case "yaml":
|
||||
d, _ := yaml.Marshal(rels)
|
||||
return []string{string(d)}
|
||||
case "json":
|
||||
d, _ := json.Marshal(rels)
|
||||
return []string{string(d)}
|
||||
default:
|
||||
for _, r := range rels {
|
||||
fmt.Println(r)
|
||||
}
|
||||
return rels
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user