-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented project manifest file chainkit.yml
- Loading branch information
Showing
1 changed file
with
52 additions
and
11 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
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 | ||
} |