Skip to content

Commit

Permalink
Implemented project manifest file chainkit.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
samalba committed Nov 30, 2018
1 parent eee9c6a commit 1ea4fed
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,69 @@
package project

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

type projectBinaries struct {
CLI string
Daemon string
}

// Project represents a project
type Project struct {
Name string
RootDir string
Name string
RootDir string `yaml:"-"`
Binaries *projectBinaries
}

// Create will create a new project in the given directory.
func Create(dir, name string) (*Project, error) {
// 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{
Name: name,
Name: name,
Binaries: &projectBinaries{
CLI: name + "cli",
Daemon: name + "d",
},
RootDir: path.Join(dir, name),
}, nil
}
}

// Save serializes the project data on disk
func (p *Project) Save() error {
ybuf, err := yaml.Marshal(p)
if err != nil {
return err
}
fp, err := os.Create(path.Join(p.RootDir, ChainkitManifest))
if err != nil {
return err
}
if _, err = fp.Write(ybuf); err != nil {
return err
}
return nil
}

// Load will load a project from a given directory
func Load(dir string) (*Project, error) {
return &Project{
Name: filepath.Base(dir),
RootDir: dir,
}, nil
errMsg := fmt.Sprintf("Cannot read manifest \"%s\"", ChainkitManifest)
data, err := ioutil.ReadFile(path.Join(dir, ChainkitManifest))
if err != nil {
return nil, errors.Wrap(err, errMsg)
}
p := &Project{}
if err = yaml.Unmarshal(data, p); err != nil {
return nil, errors.Wrap(err, errMsg)
}
p.RootDir = dir
return p, nil
}

0 comments on commit 1ea4fed

Please sign in to comment.