-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimal support for project.toml
Signed-off-by: Travis <longoria.public@gmail.com>
- Loading branch information
Showing
5 changed files
with
525 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[project] | ||
name = "Sample" | ||
|
||
[[build.buildpacks]] | ||
id = "example/lua" | ||
version = "1.0" | ||
|
||
[[build.env]] | ||
name = "KEY1" | ||
value = "VALUE1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package project | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Buildpack struct { | ||
ID string `toml:"id"` | ||
Version string `toml:"version"` | ||
URI string `toml:"uri"` | ||
} | ||
|
||
type EnvVar struct { | ||
Name string `toml:"name"` | ||
Value string `toml:"value"` | ||
} | ||
|
||
type Build struct { | ||
Include []string `toml:"include"` | ||
Exclude []string `toml:"exclude"` | ||
Buildpacks []Buildpack `toml:"buildpacks"` | ||
Env []EnvVar `toml:"env"` | ||
} | ||
|
||
type License struct { | ||
Type string `toml:"type"` | ||
URI string `toml:"uri"` | ||
} | ||
|
||
type Project struct { | ||
Name string `toml:"name"` | ||
Licenses []License `toml:"licenses"` | ||
} | ||
|
||
type Descriptor struct { | ||
Project Project `toml:"project"` | ||
Build Build `toml:"build"` | ||
Metadata map[string]interface{} `toml:"metadata"` | ||
} | ||
|
||
func ReadProjectDescriptor(pathToFile string) (Descriptor, error) { | ||
if _, err := os.Stat(pathToFile); os.IsNotExist(err) { | ||
return Descriptor{}, err | ||
} | ||
projectTomlContents, err := ioutil.ReadFile(pathToFile) | ||
if err != nil { | ||
fmt.Print(err) | ||
} | ||
|
||
var descriptor Descriptor | ||
_, err = toml.Decode(string(projectTomlContents), &descriptor) | ||
if err != nil { | ||
return Descriptor{}, err | ||
} | ||
|
||
return descriptor, descriptor.validate() | ||
} | ||
|
||
func (p Descriptor) validate() error { | ||
if p.Build.Exclude != nil && p.Build.Include != nil { | ||
return errors.New("project.toml cannot have both include and exclude defined") | ||
} | ||
if len(p.Project.Licenses) > 0 { | ||
for _, license := range p.Project.Licenses { | ||
if license.Type == "" && license.URI == "" { | ||
return errors.New("project.toml must have a type or uri defined for each license") | ||
} | ||
} | ||
} | ||
|
||
for _, bp := range p.Build.Buildpacks { | ||
if bp.ID == "" && bp.URI == "" { | ||
return errors.New("buildpacks must have an id or url defined") | ||
} | ||
if bp.URI != "" && bp.Version != "" { | ||
return errors.New("buildpacks cannot have both uri and version defined") | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.