Skip to content

Commit

Permalink
chore: fixing various bugs and testing input by artifact set
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Jan 6, 2025
1 parent 962ef14 commit 3100dc6
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 162 deletions.
53 changes: 21 additions & 32 deletions api/ocm/plugin/ppi/cmds/upload/put/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package put
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/mandelsoft/goutils/errors"
"github.com/spf13/cobra"
Expand All @@ -15,7 +13,6 @@ import (
"ocm.software/ocm/api/ocm/plugin/ppi"
"ocm.software/ocm/api/ocm/plugin/ppi/cmds/common"
"ocm.software/ocm/api/utils/cobrautils/flag"
"ocm.software/ocm/api/utils/iotools"
"ocm.software/ocm/api/utils/runtime"
)

Expand Down Expand Up @@ -82,47 +79,39 @@ func (o *Options) Complete(args []string) error {
return nil
}

func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error {
spec, err := p.DecodeUploadTargetSpecification(opts.Specification)
if err != nil {
return fmt.Errorf("target specification: %w", err)
func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) (err error) {
var spec ppi.UploadTargetSpec
if spec, err = p.DecodeUploadTargetSpecification(opts.Specification); err != nil {
return fmt.Errorf("error decoding upload target specification: %w", err)
}

u := p.GetUploader(opts.Name)
if u == nil {
return errors.ErrNotFound(descriptor.KIND_UPLOADER, fmt.Sprintf("%s:%s", opts.ArtifactType, opts.MediaType))
}

reader := io.Reader(os.Stdin)
// if we are not size aware, buffer the file to avoid OOM.
if opts.Digest == "" {
tmp, err := os.CreateTemp("", "ocm-upload-")
if err != nil {
return fmt.Errorf("failed to create temporary file to buffer access data: %w", err)
}
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
digestReader := iotools.NewDefaultDigestReader(reader)
if _, err := io.Copy(tmp, digestReader); err != nil {
return fmt.Errorf("failed to read from stdin: %w", err)
}
reader = tmp
opts.Digest = digestReader.Digest().String()
}

h, err := u.Upload(p, opts.ArtifactType, opts.MediaType, opts.Hint, opts.Digest, spec, opts.Credentials, reader)
if err != nil {
reader := cmd.InOrStdin()

var provider ppi.AccessSpecProvider
if provider, err = u.Upload(
cmd.Context(),
p,
opts.ArtifactType,
opts.MediaType,
opts.Hint,
opts.Digest,
spec,
opts.Credentials,
reader,
); err != nil {
return fmt.Errorf("upload failed: %w", err)
}

acc := h()
acc := provider()

data, err := json.Marshal(acc)
if err == nil {
var data []byte
if data, err = json.Marshal(acc); err == nil {
cmd.Printf("%s\n", string(data))
}

return err
}
3 changes: 2 additions & 1 deletion api/ocm/plugin/ppi/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ppi

import (
"context"
"encoding/json"
"io"

Expand Down Expand Up @@ -113,7 +114,7 @@ type Uploader interface {
Description() string

ValidateSpecification(p Plugin, spec UploadTargetSpec) (info *UploadTargetSpecInfo, err error)
Upload(p Plugin, arttype, mediatype, hint, digest string, spec UploadTargetSpec, creds credentials.Credentials, reader io.Reader) (AccessSpecProvider, error)
Upload(ctx context.Context, p Plugin, arttype, mediatype, hint, digest string, spec UploadTargetSpec, creds credentials.Credentials, reader io.Reader) (AccessSpecProvider, error)
}

type UploadTargetSpec = runtime.TypedObject
Expand Down
3 changes: 2 additions & 1 deletion cmds/demoplugin/uploaders/demo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uploaders

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (a *Uploader) ValidateSpecification(_ ppi.Plugin, spec ppi.UploadTargetSpec
return &info, nil
}

func (a *Uploader) Upload(p ppi.Plugin, arttype, mediatype, hint, digest string, spec ppi.UploadTargetSpec, creds credentials.Credentials, reader io.Reader) (ppi.AccessSpecProvider, error) {
func (a *Uploader) Upload(_ context.Context, p ppi.Plugin, arttype, mediatype, hint, digest string, spec ppi.UploadTargetSpec, creds credentials.Credentials, reader io.Reader) (ppi.AccessSpecProvider, error) {
var file *os.File
var err error

Expand Down
21 changes: 21 additions & 0 deletions cmds/jfrogplugin/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"encoding/json"
"fmt"
)

type Config struct {
}

// GetConfig returns the config from the raw json message.
// any return is required for the plugin interface.
func GetConfig(raw json.RawMessage) (interface{}, error) {
var cfg Config

if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("could not get config: %w", err)
}
return &cfg, nil

}
61 changes: 6 additions & 55 deletions cmds/jfrogplugin/main.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,21 @@
package main

import (
"encoding/json"
"fmt"
"os"
"strconv"

"ocm.software/ocm/api/config"
"ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/blobhandler"
"ocm.software/ocm/api/ocm/plugin"
"ocm.software/ocm/api/ocm/plugin/ppi"
"ocm.software/ocm/api/ocm/plugin/ppi/cmds"
"ocm.software/ocm/api/version"
"ocm.software/ocm/cmds/jfrogplugin/uploaders/helm"
jfrogppi "ocm.software/ocm/cmds/jfrogplugin/ppi"
)

const NAME = "jfrog"

func main() {
p := ppi.NewPlugin(NAME, version.Get().String())

p.SetShort(NAME + " plugin")
p.SetLong(`ALPHA GRADE plugin providing custom functions related to interacting with JFrog Repositories (e.g. Artifactory).
This plugin is solely for interacting with JFrog Servers and cannot be used for generic repository types.
Thus, you should only consider this plugin if
- You need to use a JFrog specific API
- You cannot use any of the generic (non-jfrog) implementations.
Examples:
You can configure the JFrog plugin as an Uploader in an ocm config file with:
- type: ` + fmt.Sprintf("%s.ocm.%s", plugin.KIND_UPLOADER, config.OCM_CONFIG_TYPE_SUFFIX) + `
registrations:
- name: ` + fmt.Sprintf("%s/%s/%s", plugin.KIND_PLUGIN, NAME, helm.NAME) + `
artifactType: ` + artifacttypes.HELM_CHART + `
priority: 200 # must be > ` + strconv.Itoa(blobhandler.DEFAULT_BLOBHANDLER_PRIO) + ` to be used over the default handler
config:
type: ` + fmt.Sprintf("%s/%s", helm.NAME, helm.VERSION) + `
# this is only a sample JFrog Server URL, do NOT append /artifactory
url: int.repositories.ocm.software
repository: ocm-helm-test
`)
p.SetConfigParser(GetConfig)

u := helm.New()
if err := p.RegisterUploader(artifacttypes.HELM_CHART, "", u); err != nil {
panic(err)
}
err := cmds.NewPluginCommand(p).Execute(os.Args[1:])
plugin, err := jfrogppi.Plugin()
if err != nil {
fmt.Fprintf(os.Stderr, "error while running plugin: %v\n", err)
fmt.Fprintf(os.Stderr, "error while creating plugin: %v\n", err)
os.Exit(1)
}
}

type Config struct {
}

func GetConfig(raw json.RawMessage) (interface{}, error) {
var cfg Config

if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("could not get config: %w", err)
if err := cmds.NewPluginCommand(plugin).Execute(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "error while running plugin: %v\n", err)
os.Exit(1)
}
return &cfg, nil
}
Loading

0 comments on commit 3100dc6

Please sign in to comment.