Merge pull request #16 from drone/feature/dron-101-card-2
read & upload card data to drone serverpull/17/head
commit
86c6a79bd4
@ -0,0 +1,70 @@
|
||||
package extractor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
prefix = []byte("\u001B]1338;")
|
||||
suffix = []byte("\u001B]0m")
|
||||
re = regexp.MustCompilePOSIX("\u001B]1338;((.*?)\u001B]0m)")
|
||||
disableCards = os.Getenv("DRONE_FLAG_ENABLE_CARDS") == "false"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
base io.Writer
|
||||
file []byte
|
||||
chunked bool
|
||||
}
|
||||
|
||||
func New(w io.Writer) *Writer {
|
||||
return &Writer{w, nil, false}
|
||||
}
|
||||
|
||||
func (e *Writer) Write(p []byte) (n int, err error) {
|
||||
if disableCards {
|
||||
return e.base.Write(p)
|
||||
}
|
||||
if bytes.HasPrefix(p, prefix) == false && e.chunked == false {
|
||||
return e.base.Write(p)
|
||||
}
|
||||
n = len(p)
|
||||
|
||||
// if the data does not include the ansi suffix,
|
||||
// it exceeds the size of the buffer and is chunked.
|
||||
e.chunked = !bytes.Contains(p, suffix)
|
||||
|
||||
// trim the ansi prefix and suffix from the data,
|
||||
// and also trim any spacing or newlines that could
|
||||
// cause confusion.
|
||||
p = bytes.TrimSpace(p)
|
||||
p = bytes.TrimPrefix(p, prefix)
|
||||
p = bytes.TrimSuffix(p, suffix)
|
||||
|
||||
e.file = append(e.file, p...)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (e *Writer) File() ([]byte, bool) {
|
||||
if len(e.file) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
data, err := base64.StdEncoding.DecodeString(string(e.file))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if isJSON(data) {
|
||||
return data, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func isJSON(data []byte) bool {
|
||||
var js json.RawMessage
|
||||
return json.Unmarshal(data, &js) == nil
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Uploader interface {
|
||||
UploadCard(context.Context, []byte, *State, string) error
|
||||
}
|
||||
|
||||
func NopUploader() Uploader {
|
||||
return new(nopUploader)
|
||||
}
|
||||
|
||||
type nopUploader struct{}
|
||||
|
||||
func (*nopUploader) UploadCard(context.Context, []byte, *State, string) error { return nil }
|
@ -0,0 +1,48 @@
|
||||
package uploader
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/drone/drone-go/drone"
|
||||
"github.com/drone/runner-go/client"
|
||||
"github.com/drone/runner-go/internal"
|
||||
"github.com/drone/runner-go/pipeline"
|
||||
)
|
||||
|
||||
var _ pipeline.Uploader = (*Upload)(nil)
|
||||
|
||||
type Upload struct {
|
||||
client client.Client
|
||||
}
|
||||
|
||||
func New(client client.Client) *Upload {
|
||||
return &Upload{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Upload) UploadCard(ctx context.Context, bytes []byte, state *pipeline.State, stepName string) error {
|
||||
src := state.Find(stepName)
|
||||
card := drone.CardInput{}
|
||||
err := json.Unmarshal(bytes, &card)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.client.UploadCard(ctx, src.ID, &card)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// update step schema
|
||||
state.Lock()
|
||||
src.Schema = card.Schema
|
||||
cpy := internal.CloneStep(src)
|
||||
state.Unlock()
|
||||
err = s.client.UpdateStep(ctx, cpy)
|
||||
if err == nil {
|
||||
state.Lock()
|
||||
internal.MergeStep(cpy, src)
|
||||
state.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1 @@
|
||||
package uploader
|
@ -0,0 +1 @@
|
||||
package pipeline
|
Loading…
Reference in New Issue