Skip to content

Commit

Permalink
add getDistributions api
Browse files Browse the repository at this point in the history
  • Loading branch information
atotto committed Sep 3, 2021
1 parent 21d6c8b commit 8a11387
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 18 deletions.
58 changes: 49 additions & 9 deletions api/v1/distributions.go

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions api/v1/distributions_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package packagecloud
package packagecloud_test

import "testing"
import (
"context"
"os"
"testing"

packagecloud "github.com/atotto/packagecloud/api/v1"
"github.com/atotto/packagecloud/api/v1/packagecloudtest"
)

func TestDistro(t *testing.T) {
ctx := context.Background()
ctx = packagecloudtest.SetupToken(t, ctx, os.Getenv("PACKAGECLOUD_TOKEN"))

distributions, err := packagecloud.GetDistributions(ctx)
if err != nil {
t.Fatal(err)
}

id, ok := distributions.DebianDistroVersionID("debian", "stretch")
if !ok {
t.Fatal("not found")
Expand Down
10 changes: 7 additions & 3 deletions api/v1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ import (
func PushPackage(ctx context.Context, repos, distro, version string, fpath string) error {
var distroVersionID string
var ok bool

ds, err := GetDistributions(ctx)
if err != nil {
return err
}
switch filepath.Ext(fpath) {
case ".deb":
distroVersionID, ok = distributions.DebianDistroVersionID(distro, version)
distroVersionID, ok = ds.DebianDistroVersionID(distro, version)
case ".whl":
distro = "python"
version = ""
distroVersionID, ok = distributions.PythonDistroVersionID(distro, version)
distroVersionID, ok = ds.PythonDistroVersionID(distro, version)
}
if !ok {
return status.Errorf(codes.InvalidArgument, "unknown distribution: %s/%s", distro, version)
}

var r io.ReadCloser
var err error
if strings.HasPrefix(fpath, "http://") || strings.HasPrefix(fpath, "https://") {
resp, err := http.Get(fpath)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions api/v1/packagecloudtest/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package packagecloudtest

import (
"context"
"testing"

packagecloud "github.com/atotto/packagecloud/api/v1"
)

func SetupToken(tb testing.TB, ctx context.Context, token string) context.Context {
if token == "" {
tb.Fatalf("empty token")
}
return packagecloud.WithPackagecloudToken(ctx, token)
}
13 changes: 9 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,28 @@ var helpDistroCommand = &commandBase{
nil,
func(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
var v interface{}
distributions, err := packagecloud.GetDistributions(ctx)
if err != nil {
log.Println(err)
return subcommands.ExitUsageError
}
switch typ := f.Arg(0); typ {
case "deb", "debian":
if name := f.Arg(1); name != "" {
for _, distros := range packagecloud.GetDistributions().Deb {
for _, distros := range distributions.Deb {
if distros.IndexName == name {
v = distros.Versions
break
}
}
} else {
v = packagecloud.GetDistributions().Deb
v = distributions.Deb
}

case "py", "python":
v = packagecloud.GetDistributions().Py
v = distributions.Py
case "":
v = packagecloud.GetDistributions()
v = distributions
default:
log.Printf("not supported type:%s", typ)
return subcommands.ExitUsageError
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ require (
github.com/mattn/go-zglob v0.0.3
google.golang.org/grpc v1.24.0
)

replace github.com/atotto/packagecloud => ./

0 comments on commit 8a11387

Please sign in to comment.