Skip to content

Commit

Permalink
project: add Image to the configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
  • Loading branch information
aluzzardi committed Nov 30, 2018
1 parent c44e1de commit 550e24c
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ type Project struct {
Name string
RootDir string `yaml:"-"`
Binaries *projectBinaries
Image string
}

// ChainkitManifest defines the name of the manifest file
const ChainkitManifest = "chainkit.yml"

// New will create a new project in the given directory.
func New(dir, name string) *Project {
return &Project{
p := &Project{
Name: name,
Binaries: &projectBinaries{
CLI: name + "cli",
Daemon: name + "d",
},
RootDir: path.Join(dir, name),
}
p.SetDefaults()
return p
}

// Save serializes the project data on disk
Expand All @@ -53,9 +56,36 @@ func (p *Project) Save() error {
return nil
}

// Validate runs sanity checks against the project
func (p *Project) Validate() error {
errorOut := func(field string) error {
return fmt.Errorf("missing required field %q", field)
}

switch {
case p.Name == "":
return errorOut("name")
case p.Binaries == nil:
return errorOut("binaries")
case p.Binaries.CLI == "":
return errorOut("binaries.cli")
case p.Binaries.Daemon == "":
return errorOut("binaries.daemon")
}

return nil
}

func (p *Project) SetDefaults() {
switch {
case p.Image == "":
p.Image = fmt.Sprintf("chainkit-%s", p.Name)
}
}

// Load will load a project from a given directory
func Load(dir string) (*Project, error) {
errMsg := fmt.Sprintf("Cannot read manifest \"%s\"", ChainkitManifest)
errMsg := fmt.Sprintf("Cannot read manifest %q", ChainkitManifest)
data, err := ioutil.ReadFile(path.Join(dir, ChainkitManifest))
if err != nil {
return nil, errors.Wrap(err, errMsg)
Expand All @@ -65,5 +95,12 @@ func Load(dir string) (*Project, error) {
return nil, errors.Wrap(err, errMsg)
}
p.RootDir = dir

if err := p.Validate(); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("%s validation", ChainkitManifest))
}

p.SetDefaults()

return p, nil
}

0 comments on commit 550e24c

Please sign in to comment.