comments
This commit is contained in:
parent
da4e072d56
commit
f78f432cd7
@ -9,16 +9,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The Config model contain the general informations about a project
|
||||||
type Config struct {
|
type Config struct {
|
||||||
file string
|
file string
|
||||||
Version string `yaml:"version,omitempty"`
|
Version string `yaml:"version,omitempty"`
|
||||||
Projects []Project
|
Projects []Project
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default value
|
// The New method puts the cli params in the struct
|
||||||
func New(params *cli.Context) *Config {
|
func New(params *cli.Context) *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
file: app_file,
|
file: appFile,
|
||||||
Version: "1.0",
|
Version: "1.0",
|
||||||
Projects: []Project{
|
Projects: []Project{
|
||||||
{
|
{
|
||||||
@ -29,26 +30,26 @@ func New(params *cli.Context) *Config {
|
|||||||
Build: params.Bool("build"),
|
Build: params.Bool("build"),
|
||||||
Bin: params.Bool("bin"),
|
Bin: params.Bool("bin"),
|
||||||
Watcher: Watcher{
|
Watcher: Watcher{
|
||||||
Paths: watcher_paths,
|
Paths: watcherPaths,
|
||||||
Ignore: watcher_ignores,
|
Ignore: watcherIgnores,
|
||||||
Exts: watcher_exts,
|
Exts: watcherExts,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for duplicates
|
// Duplicates check projects with same name or same combinations of main/path
|
||||||
func Duplicates(value Project, arr []Project) bool {
|
func Duplicates(value Project, arr []Project) bool {
|
||||||
for _, val := range arr {
|
for _, val := range arr {
|
||||||
if value.Main == val.Main || value.Name == val.Name {
|
if value.Main == val.Main && value.Path == val.Path || value.Name == val.Name {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove duplicate projects
|
// Clean duplicate projects
|
||||||
func (h *Config) Clean() {
|
func (h *Config) Clean() {
|
||||||
arr := h.Projects
|
arr := h.Projects
|
||||||
for key, val := range arr {
|
for key, val := range arr {
|
||||||
@ -59,9 +60,10 @@ func (h *Config) Clean() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check, Read and remove duplicates from the config file
|
// Read, Check and remove duplicates from the config file
|
||||||
func (h *Config) Read() error {
|
func (h *Config) Read() error {
|
||||||
if file, err := ioutil.ReadFile(h.file); err == nil {
|
file, err := ioutil.ReadFile(h.file);
|
||||||
|
if err == nil {
|
||||||
if len(h.Projects) > 0 {
|
if len(h.Projects) > 0 {
|
||||||
err = yaml.Unmarshal(file, h)
|
err = yaml.Unmarshal(file, h)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -70,12 +72,11 @@ func (h *Config) Read() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return errors.New("There are no projects")
|
return errors.New("There are no projects")
|
||||||
} else {
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// write and marshal
|
// write and marshal yaml
|
||||||
func (h *Config) Write() error {
|
func (h *Config) Write() error {
|
||||||
y, err := yaml.Marshal(h)
|
y, err := yaml.Marshal(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -87,20 +88,21 @@ func (h *Config) Write() error {
|
|||||||
// Create config yaml file
|
// Create config yaml file
|
||||||
func (h *Config) Create(params *cli.Context) error {
|
func (h *Config) Create(params *cli.Context) error {
|
||||||
if h.Read() != nil {
|
if h.Read() != nil {
|
||||||
if err := h.Write(); err != nil {
|
err := h.Write();
|
||||||
|
if err != nil {
|
||||||
os.Remove(h.file)
|
os.Remove(h.file)
|
||||||
return err
|
|
||||||
} else {
|
} else {
|
||||||
Success("The config file was successfully created")
|
Success("The config file was successfully created")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return errors.New("The config file already exists, check for realize.config.yaml")
|
return errors.New("The config file already exists, check for realize.config.yaml")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add another project
|
// Add another project
|
||||||
func (h *Config) Add(params *cli.Context) error {
|
func (h *Config) Add(params *cli.Context) error {
|
||||||
if err := h.Read(); err == nil {
|
err := h.Read();
|
||||||
|
if err == nil {
|
||||||
new := Project{
|
new := Project{
|
||||||
Name: params.String("name"),
|
Name: params.String("name"),
|
||||||
Main: params.String("main"),
|
Main: params.String("main"),
|
||||||
@ -108,9 +110,9 @@ func (h *Config) Add(params *cli.Context) error {
|
|||||||
Run: params.Bool("run"),
|
Run: params.Bool("run"),
|
||||||
Build: params.Bool("build"),
|
Build: params.Bool("build"),
|
||||||
Watcher: Watcher{
|
Watcher: Watcher{
|
||||||
Paths: watcher_paths,
|
Paths: watcherPaths,
|
||||||
Exts: watcher_exts,
|
Exts: watcherExts,
|
||||||
Ignore: watcher_ignores,
|
Ignore: watcherIgnores,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if Duplicates(new, h.Projects) {
|
if Duplicates(new, h.Projects) {
|
||||||
@ -121,15 +123,14 @@ func (h *Config) Add(params *cli.Context) error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
Success("Your project was successfully added")
|
Success("Your project was successfully added")
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove a project in list
|
// Remove a project in list
|
||||||
func (h *Config) Remove(params *cli.Context) error {
|
func (h *Config) Remove(params *cli.Context) error {
|
||||||
if err := h.Read(); err == nil {
|
err := h.Read();
|
||||||
|
if err == nil {
|
||||||
for key, val := range h.Projects {
|
for key, val := range h.Projects {
|
||||||
if params.String("name") == val.Name {
|
if params.String("name") == val.Name {
|
||||||
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
|
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
|
||||||
@ -141,14 +142,14 @@ func (h *Config) Remove(params *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors.New("No project found")
|
return errors.New("No project found")
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// List of projects
|
// List of projects
|
||||||
func (h *Config) List() error {
|
func (h *Config) List() error {
|
||||||
if err := h.Read(); err == nil {
|
err := h.Read();
|
||||||
|
if err == nil {
|
||||||
for _, val := range h.Projects {
|
for _, val := range h.Projects {
|
||||||
fmt.Println(green("|"), green(val.Name))
|
fmt.Println(green("|"), green(val.Name))
|
||||||
fmt.Println(greenl("|"), "\t", green("Main File:"), red(val.Main))
|
fmt.Println(greenl("|"), "\t", green("Main File:"), red(val.Main))
|
||||||
@ -165,7 +166,6 @@ func (h *Config) List() error {
|
|||||||
fmt.Println(greenl("|"), "\t\t", green("Watch preview:"), red(val.Watcher.Preview))
|
fmt.Println(greenl("|"), "\t\t", green("Watch preview:"), red(val.Watcher.Preview))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else {
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user