Command projects list

This commit is contained in:
alessio 2016-07-26 19:04:13 +02:00
parent 9a93840beb
commit c2da6af758
5 changed files with 102 additions and 22 deletions

View File

@ -6,8 +6,9 @@ Run, build and watch file changes with custom paths
### To do
- [x] Command start - default config gile
- [x] Command add - new project on the config file
- [ ] Command remove - remove project from the config file
- [x] Command remove - remove project from the config file
- [ ] Command watch - watch changes
- [x] Command list - print projects list
- [ ] Live reload for web project
- [x] Remove duplicate projects

12
glide.lock generated
View File

@ -1,6 +1,16 @@
hash: ef1aaff11d9be4abdfb2f2152fe8bcb49e255fe27796ab61b938144a0098f883
updated: 2016-07-25T00:16:27.960277787+02:00
updated: 2016-07-26T18:14:33.237501635+02:00
imports:
- name: github.com/fatih/color
version: 87d4004f2ab62d0d255e0a38f1680aa534549fe3
- name: github.com/mattn/go-colorable
version: 9056b7a9f2d1f2d96498d6d146acd1f9d5ed3d59
- name: github.com/mattn/go-isatty
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
- name: golang.org/x/sys
version: a646d33e2ee3172a661fc09bca23bb4889a41bc8
subpackages:
- unix
- name: gopkg.in/urfave/cli.v2
version: ee2e8aa5b22f55f006c27fd35765c62249a39aad
- name: gopkg.in/yaml.v2

View File

@ -2,3 +2,4 @@ package: github.com/tockins/realize
import:
- package: gopkg.in/urfave/cli.v2
- package: gopkg.in/yaml.v2
- package: github.com/fatih/color

26
main.go
View File

@ -41,6 +41,7 @@ func main() {
Aliases: []string{"s"},
Usage: "create the initial config file",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"},
&cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: true},
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true},
@ -56,16 +57,39 @@ func main() {
Aliases: []string{"s"},
Usage: "add another project in config file",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"},
&cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: true},
&cli.BoolFlag{Name: "run", Aliases: []string{"r"}, Value: true},
},
Action: func(params *cli.Context) error {
y := realize.New(params)
y.Read()
return handle(y.Add(params))
},
},
{
Name: "remove",
Category: "config",
Aliases: []string{"s"},
Usage: "remove a project in config file",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"},
},
Action: func(params *cli.Context) error {
y := realize.New(params)
return handle(y.Remove(params))
},
},
{
Name: "list",
Category: "config",
Aliases: []string{"s"},
Usage: "projects list",
Action: func(params *cli.Context) error {
y := realize.New(params)
return handle(y.List())
},
},
},
//Flags: []cli.Flag {
// &cli.StringFlag{

View File

@ -6,6 +6,8 @@ import (
"errors"
"gopkg.in/urfave/cli.v2"
"io/ioutil"
"github.com/fatih/color"
"fmt"
)
type Config struct {
@ -15,10 +17,10 @@ type Config struct {
}
type Project struct {
Name string `yaml:"app_name,omitempty"`
Run bool `yaml:"app_run,omitempty"`
Build bool `yaml:"app_build,omitempty"`
Main string `yaml:"app_main,omitempty"`
Name string `yaml:"app_name,omitempty"`
Watcher Watcher `yaml:"app_watcher,omitempty"`
}
@ -36,6 +38,7 @@ func New(params *cli.Context) *Config{
Version: "1.0",
Projects: []Project{
{
Name: params.String("name"),
Main: params.String("main"),
Run: params.Bool("run"),
Build: params.Bool("build"),
@ -49,9 +52,9 @@ func New(params *cli.Context) *Config{
}
// check for duplicates
func Duplicates(value string, arr []Project) bool{
func Duplicates(value Project, arr []Project) bool{
for _, val := range arr{
if value == val.Main{
if value.Main == val.Main || value.Name == val.Name{
return true
}
}
@ -62,7 +65,7 @@ func Duplicates(value string, arr []Project) bool{
func (h *Config) Clean() {
arr := h.Projects
for key, val := range arr {
if Duplicates(val.Main, arr[key+1:]) {
if Duplicates(val, arr[key+1:]) {
h.Projects = append(arr[:key], arr[key+1:]...)
break
}
@ -82,15 +85,20 @@ func (h *Config) Read() error{
}
}
// write and marshal
func (h *Config) Write() error{
y, err := yaml.Marshal(h)
if err != nil {
return err
}
return ioutil.WriteFile(h.file, y, 0755)
}
// Create config yaml file
func (h *Config) Create(params *cli.Context) error{
if h.Read() != nil {
if y, err := yaml.Marshal(h); err == nil {
err = ioutil.WriteFile(h.file, y, 0755)
if err != nil {
os.Remove(h.file)
return err
}
if err := h.Write(); err != nil {
os.Remove(h.file)
return err
}else{
return err
@ -101,8 +109,9 @@ func (h *Config) Create(params *cli.Context) error{
// Add another project
func (h *Config) Add(params *cli.Context) error{
if h.Read() == nil {
if err := h.Read(); err == nil {
new := Project{
Name: params.String("name"),
Main: params.String("main"),
Run: params.Bool("run"),
Build: params.Bool("build"),
@ -111,17 +120,52 @@ func (h *Config) Add(params *cli.Context) error{
Exts: []string{"go"},
},
}
if Duplicates(new.Main, h.Projects) {
return errors.New("There is already one project with same main path")
if Duplicates(new, h.Projects) {
return errors.New("There is already one project with same path or name")
}
h.Projects = append(h.Projects, new)
y, err := yaml.Marshal(h)
if err != nil {
return err
}
return ioutil.WriteFile(h.file, y, 0755)
return h.Write()
}else{
return err
}
}
// remove a project in list
func (h *Config) Remove(params *cli.Context) error{
if err := h.Read(); err == nil {
for key, val := range h.Projects {
if params.String("name") == val.Name {
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
return h.Write()
}
}
return errors.New("No project found")
}else{
return err
}
}
// List of projects
func (h *Config) List() error{
if err := h.Read(); err == nil {
green := color.New(color.FgGreen, color.Bold).SprintFunc()
greenl := color.New(color.FgHiGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
for _, val := range h.Projects {
fmt.Println(green("|"), green(val.Name))
fmt.Println(greenl("|"),"\t", green("Main Path:"), red(val.Main))
fmt.Println(greenl("|"),"\t", green("Run:"), red(val.Run))
fmt.Println(greenl("|"),"\t", green("Build:"), red(val.Build))
fmt.Println(greenl("|"),"\t", green("Watcher:"))
fmt.Println(greenl("|"),"\t\t", green("After:"), red(val.Watcher.After))
fmt.Println(greenl("|"),"\t\t", green("Before:"), red(val.Watcher.Before))
fmt.Println(greenl("|"),"\t\t", green("Extensions:"), red(val.Watcher.Exts))
fmt.Println(greenl("|"),"\t\t", green("Paths:"), red(val.Watcher.Paths))
}
return nil
}else{
return err
}
return errors.New("The configuration file doesn't exist")
}