refactoring: arrange error handing and application initializing
This commit is contained in:
parent
1060df80f6
commit
503dcab792
119
realize.go
119
realize.go
|
@ -27,19 +27,25 @@ const (
|
||||||
interval = 200
|
interval = 200
|
||||||
)
|
)
|
||||||
|
|
||||||
var r realize
|
// Cli commands
|
||||||
|
func main() {
|
||||||
// Realize struct contains the general app informations
|
// Realize struct contains the general app informations
|
||||||
type realize struct {
|
type realize struct {
|
||||||
settings.Settings `yaml:"settings,omitempty"`
|
settings.Settings `yaml:"settings,omitempty"`
|
||||||
Sync chan string `yaml:"-"`
|
Sync chan string `yaml:"-"`
|
||||||
Blueprint watcher.Blueprint `yaml:"-"`
|
Blueprint watcher.Blueprint `yaml:"-"`
|
||||||
Server server.Server `yaml:"-"`
|
Server server.Server `yaml:"-"`
|
||||||
Projects *[]watcher.Project `yaml:"projects" json:"projects"`
|
Projects *[]watcher.Project `yaml:"projects" json:"projects"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var r realize
|
||||||
|
// Before of every exec of a cli method
|
||||||
|
before := func(*cli.Context) error {
|
||||||
|
gopath := os.Getenv("GOPATH")
|
||||||
|
if gopath == "" {
|
||||||
|
return errors.New("$GOPATH isn't set up properly")
|
||||||
|
}
|
||||||
|
|
||||||
// Realize struct initialization
|
|
||||||
func init() {
|
|
||||||
r = realize{
|
r = realize{
|
||||||
Sync: make(chan string),
|
Sync: make(chan string),
|
||||||
Settings: settings.Settings{
|
Settings: settings.Settings{
|
||||||
|
@ -72,34 +78,19 @@ func init() {
|
||||||
r.Projects = &r.Blueprint.Projects
|
r.Projects = &r.Blueprint.Projects
|
||||||
|
|
||||||
// read if exist
|
// read if exist
|
||||||
r.Read(&r)
|
if err := r.Read(&r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// increase the file limit
|
// increase the file limit
|
||||||
if r.Config.Flimit != 0 {
|
if r.Config.Flimit != 0 {
|
||||||
r.Flimit()
|
if err := r.Flimit(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Before of every exec of a cli method
|
|
||||||
func before() error {
|
|
||||||
gopath := os.Getenv("GOPATH")
|
|
||||||
if gopath == "" {
|
|
||||||
return handle(errors.New("$GOPATH isn't set up properly"))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
// Handle errors
|
|
||||||
func handle(err error) error {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(style.Red.Bold(err.Error()))
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cli commands
|
|
||||||
func main() {
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
Name: "Realize",
|
Name: "Realize",
|
||||||
Version: appVersion,
|
Version: appVersion,
|
||||||
|
@ -142,16 +133,22 @@ func main() {
|
||||||
r.Config.Create = false
|
r.Config.Create = false
|
||||||
}
|
}
|
||||||
r.Blueprint.Projects = []watcher.Project{}
|
r.Blueprint.Projects = []watcher.Project{}
|
||||||
handle(r.Blueprint.Add(p))
|
if err := r.Blueprint.Add(p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := r.Server.Start(p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := r.Blueprint.Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := r.Record(r); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
handle(r.Server.Start(p))
|
|
||||||
handle(r.Blueprint.Run())
|
|
||||||
handle(r.Record(r))
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: before,
|
||||||
return before()
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "add",
|
Name: "add",
|
||||||
|
@ -170,16 +167,18 @@ func main() {
|
||||||
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
|
&cli.BoolFlag{Name: "no-install", Aliases: []string{"ni"}, Value: false, Usage: "Disable go install"},
|
||||||
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
|
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Ignore existing configurations."},
|
||||||
},
|
},
|
||||||
Action: func(p *cli.Context) (err error) {
|
Action: func(p *cli.Context) error {
|
||||||
fmt.Println(p.String("path"))
|
fmt.Println(p.String("path"))
|
||||||
handle(r.Blueprint.Add(p))
|
if err := r.Blueprint.Add(p); err != nil {
|
||||||
handle(r.Record(r))
|
return err
|
||||||
|
}
|
||||||
|
if err := r.Record(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully added."))
|
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully added."))
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: before,
|
||||||
return before()
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "init",
|
Name: "init",
|
||||||
|
@ -866,13 +865,13 @@ func main() {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
handle(r.Record(r))
|
if err := r.Record(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your configuration was successful."))
|
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your configuration was successful."))
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: before,
|
||||||
return before()
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "remove",
|
Name: "remove",
|
||||||
|
@ -883,14 +882,16 @@ func main() {
|
||||||
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""},
|
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""},
|
||||||
},
|
},
|
||||||
Action: func(p *cli.Context) error {
|
Action: func(p *cli.Context) error {
|
||||||
handle(r.Blueprint.Remove(p))
|
if err := r.Blueprint.Remove(p); err != nil {
|
||||||
handle(r.Record(r))
|
return err
|
||||||
|
}
|
||||||
|
if err := r.Record(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully removed."))
|
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Your project was successfully removed."))
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: before,
|
||||||
return before()
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
|
@ -898,11 +899,9 @@ func main() {
|
||||||
Aliases: []string{"l"},
|
Aliases: []string{"l"},
|
||||||
Description: "Print projects list.",
|
Description: "Print projects list.",
|
||||||
Action: func(p *cli.Context) error {
|
Action: func(p *cli.Context) error {
|
||||||
return handle(r.Blueprint.List())
|
return r.Blueprint.List()
|
||||||
},
|
|
||||||
Before: func(c *cli.Context) error {
|
|
||||||
return before()
|
|
||||||
},
|
},
|
||||||
|
Before: before,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "clean",
|
Name: "clean",
|
||||||
|
@ -910,15 +909,19 @@ func main() {
|
||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Description: "Remove realize folder.",
|
Description: "Remove realize folder.",
|
||||||
Action: func(p *cli.Context) error {
|
Action: func(p *cli.Context) error {
|
||||||
handle(r.Settings.Remove())
|
if err := r.Settings.Remove(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed."))
|
fmt.Println(style.Yellow.Bold("[")+"REALIZE"+style.Yellow.Bold("]"), style.Green.Bold("Realize folder successfully removed."))
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: before,
|
||||||
return before()
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Run(os.Args)
|
|
||||||
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
fmt.Println(style.Red.Bold(err))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,13 @@ package settings
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
// Flimit defines the max number of watched files
|
// Flimit defines the max number of watched files
|
||||||
func (s *Settings) Flimit() {
|
func (s *Settings) Flimit() error {
|
||||||
var rLimit syscall.Rlimit
|
var rLimit syscall.Rlimit
|
||||||
rLimit.Max = uint64(s.Config.Flimit)
|
rLimit.Max = uint64(s.Config.Flimit)
|
||||||
rLimit.Cur = uint64(s.Config.Flimit)
|
rLimit.Cur = uint64(s.Config.Flimit)
|
||||||
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
||||||
if err != nil {
|
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
s.Fatal(err, "Error setting rlimit")
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
// build windows
|
||||||
package settings
|
package settings
|
||||||
|
|
||||||
// Flimit defines the max number of watched files
|
// Flimit defines the max number of watched files
|
||||||
func (s *Settings) Flimit() {
|
func (s *Settings) Flimit() error {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue