Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include fields for structured docs #76

Merged
merged 2 commits into from
Sep 8, 2016
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
24 changes: 3 additions & 21 deletions lib/spanner/config.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
defmodule Spanner.Config do
alias Spanner.Config.SyntaxValidator
alias Spanner.Config.SemanticValidator
alias Spanner.Config.Upgrader

# Currently we support version n and n-1. Adding new optional fields
# does not require a version bump, but adding new mandatory fields,
# or changing the overall structure of the configuration file does
# require a bump

@current_config_version 3
@current_config_version 4
@old_config_version @current_config_version - 1
@config_extensions [".yaml", ".yml", ".json"]
@config_file "config"
Expand Down Expand Up @@ -61,7 +60,8 @@ defmodule Spanner.Config do
"""
@spec validate(Map.t) ::
{:ok, Map.t} | {:error, List.t, List.t} | {:warning, Map.t, List.t}
def validate(%{"cog_bundle_version" => @current_config_version}=config) do
def validate(%{"cog_bundle_version" => version}=config)
when version >= @old_config_version do
case SyntaxValidator.validate(config) do
:ok ->
config = fixup_rules(config)
Expand All @@ -75,24 +75,6 @@ defmodule Spanner.Config do
{:error, errors, []}
end
end
def validate(%{"cog_bundle_version" => @old_config_version}=config) do
# Upgrader will return an upgraded config and a list of warnings
# or an error
case Upgrader.upgrade(config) do
{:ok, upgraded_config, warnings} ->
# We still need to validate the upgraded config
case validate(upgraded_config) do
{:ok, validated_config} ->
# If everything goes well, we return the validated config
# and a list of warnings.
{:warning, validated_config, warnings}
{:error, errors, _} ->
{:error, errors, warnings}
end
{:error, errors, warnings} ->
{:error, errors, warnings}
end
end
def validate(%{"cog_bundle_version" => version}) do
{:error,
[{"""
Expand Down
7 changes: 4 additions & 3 deletions lib/spanner/config/syntax_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ defmodule Spanner.Config.SyntaxValidator do
@doc """
Accepts a config map and validates syntax.
"""
@spec validate(Map.t, integer()) :: :ok | {:error, [{String.t, String.t}]}
def validate(config, version \\ @current_config_version) do
with {:ok, schema} <- load_schema(version),
@spec validate(Map.t) :: :ok | {:error, [{String.t, String.t}]}
def validate(config) do
with version <- Map.fetch!(config, "cog_bundle_version"),
{:ok, schema} <- load_schema(version),
{:ok, resolved_schema} <- resolve_schema(schema),
:ok <- ExJsonSchema.Validator.validate(resolved_schema, config),
do: :ok
Expand Down
119 changes: 0 additions & 119 deletions lib/spanner/config/upgrader.ex

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
---
"$schema": http://json-schema.org/draft-04/schema#
title: Bundle Config v2
title: Bundle Config v3
description: A config schema for bundles
type: object
required:
- cog_bundle_version
- name
- description
- version
- commands
additionalProperties: false
properties:
cog_bundle_version:
type: number
enum:
- 2
- 4
name:
type: string
description:
type: string
long_descirption:
type: string
author:
type: string
homepage:
type: string
version:
type: string
pattern: ^\d+\.\d+($|\.\d+$)
Expand All @@ -28,15 +36,23 @@ properties:
required:
- image
- tag
optional:
- binds
properties:
image:
type: string
tag:
type: string
binds:
type: array
items:
type: string
templates:
type: object
additionalProperties:
"$ref": "#/definitions/template"
patternProperties:
"^[A-Za-z0-9_]+$":
"$ref": "#/definitions/template"
additionalProperties: false
commands:
type: object
additionalProperties:
Expand All @@ -46,27 +62,32 @@ properties:
definitions:
template:
type: object
additionalProperties: false
properties:
slack:
type: string
hipchat:
body:
type: string
required:
- body
additionalProperties: false
command:
type: object
required:
- executable
- rules
properties:
executable:
type: string
documentation:
description:
type: string
execution:
enum:
- once
- multiple
enforcing:
type: boolean
long_description:
type: string
examples:
type: string
arguments:
type: string
subcommands:
type: object
additionalProperties:
type: string
rules:
type: array
items:
Expand All @@ -86,6 +107,7 @@ definitions:
type: object
required:
- type
additionalProperties: false
properties:
type:
type: string
Expand All @@ -102,4 +124,3 @@ definitions:
type: boolean
short_flag:
type: string

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Spanner.Config.Validator.Test do
defmodule Spanner.Config.V3Validator do
use ExUnit.Case, async: true
alias Spanner.Config

Expand Down Expand Up @@ -26,19 +26,6 @@ defmodule Spanner.Config.Validator.Test do
}
end

defp old_config do
%{"cog_bundle_version" => 2,
"name" => "foo",
"version" => "0.0.1",
"commands" => %{
"date" => %{
"executable" => "/bin/date",
"enforcing" => false
}
}
}
end

defp incomplete_rules_config do
%{"cog_bundle_version" => 3,
"name" => "foo",
Expand Down Expand Up @@ -67,12 +54,12 @@ defmodule Spanner.Config.Validator.Test do
test "wrong cog_bundle_version" do
result = update_in(minimal_config, ["cog_bundle_version"], fn(_) -> 1 end)
|> validate
assert result == {:error, [{"cog_bundle_version 1 is not supported. Please update your bundle config to version 3.", "#/cog_bundle_version"}], []}
assert result == {:error, [{"cog_bundle_version 1 is not supported. Please update your bundle config to version 4.", "#/cog_bundle_version"}], []}
end

test "missing cog_bundle_version" do
result = Map.delete(minimal_config, "cog_bundle_version") |> validate
assert result == {:error, [{"cog_bundle_version not specified. You must specify a valid bundle version. The current version is 3.", "#/cog_bundle_version"}], []}
assert result == {:error, [{"cog_bundle_version not specified. You must specify a valid bundle version. The current version is 4.", "#/cog_bundle_version"}], []}
end

test "incomplete rules" do
Expand Down Expand Up @@ -132,22 +119,6 @@ defmodule Spanner.Config.Validator.Test do
assert response == {:error, [{"Schema does not allow additional properties.", "#/templates/foo/meh"}], []}
end

test "upgrading bundle versions" do
response = validate(old_config)
{:warning, config, warnings} = response

assert %{"cog_bundle_version" => 3,
"name" => "foo",
"version" => "0.0.1",
"commands" => %{
"date" => %{
"executable" => "/bin/date",
"rules" => ["when command is foo:date allow"]}},
} = config
assert [{"Bundle config version 2 has been deprecated. Please update to version 3.", "#/cog_bundle_version"},
{"Non-enforcing commands have been deprecated. Please update your bundle config to version 3.", "#/commands/date/enforcing"}] = warnings
end

# env_vars can be strings, booleans and numbers
Enum.each(["string", true, 4], fn(type) ->
test "env_vars can be a #{type}" do
Expand Down
Loading