Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions cmd/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ func NewCmdDefinition() *cobra.Command {
}

definitionPublishCmd := &cobra.Command{
Use: "publish",
Use: "publish [definition file]",
Short: "Publish an artifact definition to Massdriver",
Long: helpdocs.MustRender("definition/publish"),
Args: cobra.ExactArgs(1),
RunE: runDefinitionPublish,
}
definitionPublishCmd.Flags().StringP("file", "f", "", "File containing artifact definition schema (use - for stdin)")
_ = definitionPublishCmd.MarkFlagRequired("file")

definitionDeleteCmd := &cobra.Command{
Use: "delete [definition]",
Expand Down Expand Up @@ -117,23 +116,9 @@ func runDefinitionGet(cmd *cobra.Command, args []string) error {
func runDefinitionPublish(cmd *cobra.Command, args []string) error {
ctx := context.Background()

defPath, err := cmd.Flags().GetString("file")
if err != nil {
return err
}
defFile := args[0]
cmd.SilenceUsage = true

var defFile *os.File
if defPath == "-" {
defFile = os.Stdin
} else {
defFile, err = os.Open(defPath)
if err != nil {
fmt.Println(err)
}
defer defFile.Close()
}

mdClient, mdClientErr := client.New()
if mdClientErr != nil {
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
Expand Down
6 changes: 3 additions & 3 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"path/filepath"

"github.com/massdriver-cloud/mass/docs/helpdocs"
"github.com/massdriver-cloud/mass/pkg/bundle"
"github.com/massdriver-cloud/mass/pkg/definition"
"github.com/massdriver-cloud/mass/pkg/jsonschema"
"github.com/massdriver-cloud/mass/pkg/prettylogs"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
Expand All @@ -28,7 +28,7 @@
RunE: runSchemaDereference,
}
schemaDereferenceCmd.Flags().StringP("file", "f", "", "Path to JSON document")
schemaDereferenceCmd.MarkFlagRequired("file")

Check failure on line 31 in cmd/schema.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `schemaDereferenceCmd.MarkFlagRequired` is not checked (errcheck)

schemaValidateCmd := &cobra.Command{
Use: "validate",
Expand All @@ -38,8 +38,8 @@
}
schemaValidateCmd.Flags().StringP("document", "d", "document.json", "Path to JSON document")
schemaValidateCmd.Flags().StringP("schema", "s", "./schema.json", "Path to JSON Schema")
schemaValidateCmd.MarkFlagRequired("document")

Check failure on line 41 in cmd/schema.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `schemaValidateCmd.MarkFlagRequired` is not checked (errcheck)
schemaValidateCmd.MarkFlagRequired("schema")

Check failure on line 42 in cmd/schema.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `schemaValidateCmd.MarkFlagRequired` is not checked (errcheck)

schemaCmd.AddCommand(schemaDereferenceCmd)
schemaCmd.AddCommand(schemaValidateCmd)
Expand Down Expand Up @@ -68,11 +68,11 @@
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
}

derefOpts := bundle.DereferenceOptions{
derefOpts := definition.DereferenceOptions{
Client: mdClient,
Cwd: basePath,
}
dereferencedSchema, derefErr := bundle.DereferenceSchema(rawSchema, derefOpts)
dereferencedSchema, derefErr := definition.DereferenceSchema(rawSchema, derefOpts)
if derefErr != nil {
return fmt.Errorf("failed to dereference schema: %w", derefErr)
}
Expand Down
21 changes: 8 additions & 13 deletions docs/generated/mass_definition_publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,33 @@ Publish an artifact definition to Massdriver

# Publish Artifact Definition

Publishes a new or updated artifact definition to Massdriver.
Publishes a new or updated artifact definition to Massdriver. Supports JSON or YAML formats.

## Usage

```bash
mass definition publish --file <definition-file>
mass definition publish <definition-file>
```

## Examples

```bash
# Publish a definition from a file
mass definition publish --file my-definition.json
# Publish a definition from a JSON file
mass definition publish my-definition.json

# Publish a definition from stdin
cat my-definition.json | mass definition publish --file -
# Publish a definition from a YAML file
mass definition publish my-definition.yaml
```

## Options

- `--file`: Path to the definition file (use - for stdin)


```
mass definition publish [flags]
mass definition publish [definition file] [flags]
```

### Options

```
-f, --file string File containing artifact definition schema (use - for stdin)
-h, --help help for publish
-h, --help help for publish
```

### SEE ALSO
Expand Down
16 changes: 6 additions & 10 deletions docs/helpdocs/definition/publish.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
# Publish Artifact Definition

Publishes a new or updated artifact definition to Massdriver.
Publishes a new or updated artifact definition to Massdriver. Supports JSON or YAML formats.

## Usage

```bash
mass definition publish --file <definition-file>
mass definition publish <definition-file>
```

## Examples

```bash
# Publish a definition from a file
mass definition publish --file my-definition.json
# Publish a definition from a JSON file
mass definition publish my-definition.json

# Publish a definition from stdin
cat my-definition.json | mass definition publish --file -
# Publish a definition from a YAML file
mass definition publish my-definition.yaml
```

## Options

- `--file`: Path to the definition file (use - for stdin)
209 changes: 1 addition & 208 deletions pkg/bundle/dereference.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package bundle

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"reflect"
"regexp"

"github.com/massdriver-cloud/mass/pkg/definition"

Expand All @@ -37,7 +29,7 @@ func (b *Bundle) DereferenceSchemas(path string, mdClient *client.Client) error
}
}

dereferencedSchema, err := DereferenceSchema(*task.schema, DereferenceOptions{Client: mdClient, Cwd: cwd})
dereferencedSchema, err := definition.DereferenceSchema(*task.schema, definition.DereferenceOptions{Client: mdClient, Cwd: cwd})

if err != nil {
return err
Expand All @@ -53,202 +45,3 @@ func (b *Bundle) DereferenceSchemas(path string, mdClient *client.Client) error

return nil
}

type DereferenceOptions struct {
Client *client.Client
Cwd string
}

// relativeFilePathPattern only accepts relative file path prefixes "./" and "../"
var relativeFilePathPattern = regexp.MustCompile(`^(\.\/|\.\.\/)`)
var massdriverDefinitionPattern = regexp.MustCompile(`^[a-zA-Z0-9-]+(\/[a-zA-Z0-9-]+)?$`)
var httpPattern = regexp.MustCompile(`^(http|https)://`)
var fragmentPattern = regexp.MustCompile(`^#`)

func DereferenceSchema(anyVal any, opts DereferenceOptions) (any, error) {
val := getValue(anyVal)

switch val.Kind() { //nolint:exhaustive
case reflect.Slice, reflect.Array:
return dereferenceList(val, opts)
case reflect.Map:
schemaInterface := val.Interface()
schema, ok := schemaInterface.(map[string]any)
if !ok {
return nil, fmt.Errorf("schema is not an object")
}
hydratedSchema := map[string]any{}

// if this part of the schema has a $ref that is a local file, read it and make it
// the map that we hydrate into. This causes any keys in the ref'ing object to override anything in the ref'd object
// which adheres to the JSON Schema spec.
if schemaRefInterface, refOk := schema["$ref"]; refOk {
schemaRefValue, refStringOk := schemaRefInterface.(string)
if !refStringOk {
return nil, fmt.Errorf("$ref is not a string")
}

var err error
if relativeFilePathPattern.MatchString(schemaRefValue) { //nolint:gocritic
// this is a relative file ref
// build up the path from where the dir current schema was read
hydratedSchema, err = dereferenceFilePathRef(hydratedSchema, schema, schemaRefValue, opts)
} else if httpPattern.MatchString(schemaRefValue) {
// HTTP ref. Pull the schema down via HTTP GET and hydrate
hydratedSchema, err = dereferenceHTTPRef(hydratedSchema, schema, schemaRefValue, opts)
} else if massdriverDefinitionPattern.MatchString(schemaRefValue) {
// this must be a published schema, so fetch from massdriver
hydratedSchema, err = dereferenceMassdriverRef(hydratedSchema, schema, schemaRefValue, opts)
} else if fragmentPattern.MatchString(schemaRefValue) {
// this is a fragment, so we do nothing and leave the schema as is
// since fragments are not dereferenced in the same way as full schemas
} else {
return nil, fmt.Errorf("unable to resolve ref: %s", schemaRefValue)
}
if err != nil {
return hydratedSchema, err
}
}
return dereferenceMap(hydratedSchema, schema, opts)
default:
return anyVal, nil
}
}

func dereferenceMap(hydratedSchema map[string]any, schema map[string]any, opts DereferenceOptions) (map[string]any, error) {
for key, value := range schema {
var valueInterface = value
hydratedValue, err := DereferenceSchema(valueInterface, opts)
if err != nil {
return hydratedSchema, err
}
hydratedSchema[key] = hydratedValue
}
return hydratedSchema, nil
}

func dereferenceList(val reflect.Value, opts DereferenceOptions) ([]any, error) {
hydratedList := make([]any, 0)
for i := 0; i < val.Len(); i++ {
hydratedVal, err := DereferenceSchema(val.Index(i).Interface(), opts)
if err != nil {
return hydratedList, err
}
hydratedList = append(hydratedList, hydratedVal)
}
return hydratedList, nil
}

func dereferenceMassdriverRef(hydratedSchema map[string]any, schema map[string]any, schemaRefValue string, opts DereferenceOptions) (map[string]any, error) {
referencedSchema, err := definition.GetAsMap(context.Background(), opts.Client, schemaRefValue)
if err != nil {
return hydratedSchema, err
}

if nestedSchema, exists := referencedSchema["schema"]; exists {
var ok bool
referencedSchema, ok = nestedSchema.(map[string]any)
if !ok {
return hydratedSchema, fmt.Errorf("schema is not a map")
}
}

hydratedSchema, err = replaceRef(schema, referencedSchema, opts)
if err != nil {
return hydratedSchema, err
}
return hydratedSchema, nil
}

func dereferenceHTTPRef(hydratedSchema map[string]any, schema map[string]any, schemaRefValue string, opts DereferenceOptions) (map[string]any, error) {
ctx := context.Background()
var referencedSchema map[string]any

client := http.Client{}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, schemaRefValue, nil)
if err != nil {
return hydratedSchema, err
}
resp, doErr := client.Do(request)
if doErr != nil {
return hydratedSchema, doErr
}
if resp.StatusCode != http.StatusOK {
return hydratedSchema, errors.New("received non-200 response getting ref " + resp.Status + " " + schemaRefValue)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return hydratedSchema, err
}
err = json.Unmarshal(body, &referencedSchema)
if err != nil {
return hydratedSchema, err
}

hydratedSchema, err = replaceRef(schema, referencedSchema, opts)
return hydratedSchema, err
}

func dereferenceFilePathRef(hydratedSchema map[string]any, schema map[string]any, schemaRefValue string, opts DereferenceOptions) (map[string]any, error) {
var referencedSchema map[string]any
var schemaRefDir string
schemaRefAbsPath, err := filepath.Abs(filepath.Join(opts.Cwd, schemaRefValue))

if err != nil {
return hydratedSchema, err
}

schemaRefDir = filepath.Dir(schemaRefAbsPath)
referencedSchema, readErr := readJSONFile(schemaRefAbsPath)

if readErr != nil {
return hydratedSchema, readErr
}

var replaceErr error
opts.Cwd = schemaRefDir
hydratedSchema, replaceErr = replaceRef(schema, referencedSchema, opts)
if replaceErr != nil {
return hydratedSchema, replaceErr
}
return hydratedSchema, nil
}

func getValue(anyVal any) reflect.Value {
val := reflect.ValueOf(anyVal)

if val.Kind() == reflect.Ptr {
val = val.Elem()
}

return val
}

func readJSONFile(filepath string) (map[string]any, error) {
var result map[string]any
data, err := os.ReadFile(filepath)

if err != nil {
return result, err
}
err = json.Unmarshal(data, &result)

return result, err
}

func replaceRef(base map[string]any, referenced map[string]any, opts DereferenceOptions) (map[string]any, error) {
hydratedSchema := map[string]any{}
delete(base, "$ref")

for k, v := range referenced {
hydratedValue, err := DereferenceSchema(v, opts)
if err != nil {
return hydratedSchema, err
}
hydratedSchema[k] = hydratedValue
}
return hydratedSchema, nil
}
Loading
Loading