2019-02-10 19:00:16 +00:00
|
|
|
// Copyright 2019 Drone IO, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
package yaml
|
|
|
|
|
|
|
|
import (
|
2019-02-10 19:00:16 +00:00
|
|
|
"bytes"
|
2019-01-22 23:44:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-02-10 19:00:16 +00:00
|
|
|
|
2019-05-30 01:21:23 +00:00
|
|
|
"github.com/buildkite/yaml"
|
2019-01-22 23:44:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Resource enums.
|
|
|
|
const (
|
|
|
|
KindCron = "cron"
|
|
|
|
KindPipeline = "pipeline"
|
|
|
|
KindRegistry = "registry"
|
|
|
|
KindSecret = "secret"
|
|
|
|
KindSignature = "signature"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Manifest is a collection of Drone resources.
|
|
|
|
Manifest struct {
|
|
|
|
Resources []Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource represents a Drone resource.
|
|
|
|
Resource interface {
|
|
|
|
// GetVersion returns the resource version.
|
|
|
|
GetVersion() string
|
|
|
|
|
|
|
|
// GetKind returns the resource kind.
|
|
|
|
GetKind() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawResource is a raw encoded resource with the
|
|
|
|
// resource kind and type extracted.
|
|
|
|
RawResource struct {
|
|
|
|
Version string
|
|
|
|
Kind string
|
|
|
|
Type string
|
|
|
|
Data []byte `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
resource struct {
|
|
|
|
Version string
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalJSON implement the json.Unmarshaler.
|
|
|
|
func (m *Manifest) UnmarshalJSON(b []byte) error {
|
|
|
|
messages := []json.RawMessage{}
|
|
|
|
err := json.Unmarshal(b, &messages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, message := range messages {
|
|
|
|
res := new(resource)
|
|
|
|
err := json.Unmarshal(message, res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var obj Resource
|
|
|
|
switch res.Kind {
|
|
|
|
case "cron":
|
|
|
|
obj = new(Cron)
|
|
|
|
case "secret":
|
|
|
|
obj = new(Secret)
|
|
|
|
case "signature":
|
|
|
|
obj = new(Signature)
|
|
|
|
case "registry":
|
|
|
|
obj = new(Registry)
|
|
|
|
default:
|
|
|
|
obj = new(Pipeline)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(message, obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Resources = append(m.Resources, obj)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implement the json.Marshaler.
|
|
|
|
func (m *Manifest) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(m.Resources)
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:26:34 +00:00
|
|
|
// MarshalYAML is not implemented and returns an error. This is
|
|
|
|
// because the Manifest is a representation of multiple yaml
|
|
|
|
// documents, and MarshalYAML would otherwise attempt to marshal
|
|
|
|
// as a single Yaml document. Use the Encode method instead.
|
2019-01-22 23:44:17 +00:00
|
|
|
func (m *Manifest) MarshalYAML() (interface{}, error) {
|
|
|
|
return nil, errors.New("yaml: marshal not implemented")
|
|
|
|
}
|
2019-02-10 19:00:16 +00:00
|
|
|
|
|
|
|
// Encode encodes the manifest in Yaml format.
|
|
|
|
func (m *Manifest) Encode() ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := yaml.NewEncoder(buf)
|
|
|
|
for _, res := range m.Resources {
|
|
|
|
if err := enc.Encode(res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|