Skip to content

Commit

Permalink
add support for package keys for new cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Oct 4, 2024
1 parent 31fef2c commit 492fa1f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func delRun(cmd *cobra.Command, args []string) error {
PackageID: &delOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
31 changes: 25 additions & 6 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ func draftRun(cmd *cobra.Command, args []string) error {
desc = &d
}

var pkg *string
if p := draftOpts.Package; p != "" {
pkg = &p
}

version, err := semver.NewVersion(draftOpts.Version)
if err != nil {
return fmt.Errorf(`version "%s" is not acceptable (%s)`, draftOpts.Version, italic(strings.ToLower(err.Error())))
Expand All @@ -168,6 +163,31 @@ func draftRun(cmd *cobra.Command, args []string) error {
}
}

var pkg *string
if id := draftOpts.Package; id != "" {
p := &keygenext.Package{ID: id}

// get actual package id e.g. id is key ident
if err := p.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

pkg = &p.ID
}

release := &keygenext.Release{
Name: name,
Description: desc,
Expand All @@ -179,7 +199,6 @@ func draftRun(cmd *cobra.Command, args []string) error {
Constraints: constraints,
Metadata: metadata,
}

if err := release.Create(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
1 change: 1 addition & 0 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func publishRun(cmd *cobra.Command, args []string) error {
PackageID: &publishOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
1 change: 1 addition & 0 deletions cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func tagRun(cmd *cobra.Command, args []string) error {
PackageID: &tagOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
1 change: 1 addition & 0 deletions cmd/untag.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func untagRun(cmd *cobra.Command, args []string) error {
PackageID: &untagOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
2 changes: 1 addition & 1 deletion cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func uploadRun(cmd *cobra.Command, args []string) error {
PackageID: &uploadOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down Expand Up @@ -287,7 +288,6 @@ func uploadRun(cmd *cobra.Command, args []string) error {
ReleaseID: &release.ID,
Metadata: metadata,
}

if err := artifact.Create(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
1 change: 1 addition & 0 deletions cmd/yank.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func yankRun(cmd *cobra.Command, args []string) error {
PackageID: &yankOpts.Package,
}

// get actual release id w/ filters e.g. package
if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
77 changes: 77 additions & 0 deletions internal/keygenext/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package keygenext

import (
"github.com/keygen-sh/jsonapi-go"
"github.com/keygen-sh/keygen-go/v2"
)

type Package struct {
ID string `json:"-"`
Type string `json:"-"`
Name *string `json:"name,omitempty"`
Key *string `json:"key,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
ProductID string `json:"-"`
}

func (p *Package) SetID(id string) error {
p.ID = id
return nil
}

func (p *Package) SetType(t string) error {
p.Type = t
return nil
}

func (p *Package) SetData(to func(target interface{}) error) error {
return to(p)
}

func (p Package) GetID() string {
return p.ID
}

func (p Package) GetType() string {
return "packages"
}

func (p Package) GetData() interface{} {
return p
}

func (p Package) GetRelationships() map[string]interface{} {
relationships := make(map[string]interface{})

if p.ProductID != "" {
relationships["product"] = jsonapi.ResourceObjectIdentifier{
Type: "products",
ID: p.ProductID,
}
}

if len(relationships) == 0 {
return nil
}

return relationships
}

func (p *Package) Get() error {
client := keygen.NewClientWithOptions(
&keygen.ClientOptions{Account: Account, Environment: Environment, Token: Token, PublicKey: PublicKey, UserAgent: UserAgent, APIURL: APIURL},
)

res, err := client.Get("packages/"+p.ID, nil, p)
if err != nil {
if res != nil && len(res.Document.Errors) > 0 {
e := res.Document.Errors[0]

return &Error{Title: e.Title, Detail: e.Detail, Source: e.Source.Pointer, Code: e.Code, Err: err}
}

return err
}

return nil
}

0 comments on commit 492fa1f

Please sign in to comment.