read,write file moved to config folder
This commit is contained in:
parent
cf1461b5f1
commit
e131e37349
|
@ -25,7 +25,7 @@ var R Realize
|
|||
|
||||
// Realize struct contains the general app informations
|
||||
type Realize struct {
|
||||
c.Utils
|
||||
c.Config
|
||||
Name, Description, Author, Email, Host string
|
||||
Version string
|
||||
Limit uint64
|
||||
|
@ -50,9 +50,9 @@ func init() {
|
|||
Sync: make(chan string),
|
||||
}
|
||||
R.Blueprint = w.Blueprint{
|
||||
Utils: R.Utils,
|
||||
Files: R.Files,
|
||||
Sync: R.Sync,
|
||||
Config: R.Config,
|
||||
Files: R.Files,
|
||||
Sync: R.Sync,
|
||||
}
|
||||
R.Server = s.Server{
|
||||
Blueprint: &R.Blueprint,
|
||||
|
|
19
cli/cmd.go
19
cli/cmd.go
|
@ -55,6 +55,7 @@ func (h *Blueprint) Fast(params *cli.Context) error {
|
|||
// Add a new project
|
||||
func (h *Blueprint) Add(params *cli.Context) error {
|
||||
p := Project{
|
||||
Name: h.name(params),
|
||||
Path: filepath.Clean(params.String("path")),
|
||||
Build: params.Bool("build"),
|
||||
Bin: boolFlag(params.Bool("no-bin")),
|
||||
|
@ -72,7 +73,6 @@ func (h *Blueprint) Add(params *cli.Context) error {
|
|||
},
|
||||
},
|
||||
}
|
||||
p.Name = p.nameFlag(params)
|
||||
if _, err := duplicates(p, h.Projects); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ func (h *Blueprint) Clean() {
|
|||
|
||||
// Read, Check and remove duplicates from the config file
|
||||
func (h *Blueprint) Read() error {
|
||||
content, err := read(h.Files["config"])
|
||||
content, err := h.Stream(h.Files["config"])
|
||||
if err == nil {
|
||||
err = yaml.Unmarshal(content, h)
|
||||
if err == nil {
|
||||
|
@ -114,7 +114,7 @@ func (h *Blueprint) Create() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return write(h.Files["config"], y)
|
||||
return h.Write(h.Files["config"], y)
|
||||
}
|
||||
|
||||
// Inserts a new project in the list
|
||||
|
@ -178,3 +178,16 @@ func (h *Blueprint) List() error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// NameParam check the project name presence. If empty takes the working directory name
|
||||
func (p *Blueprint) name(params *cli.Context) string {
|
||||
var name string
|
||||
if params.String("name") == "" && params.String("path") == "" {
|
||||
return p.Wdir()
|
||||
} else if params.String("path") != "/" {
|
||||
name = filepath.Base(params.String("path"))
|
||||
} else {
|
||||
name = params.String("name")
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
|||
defer func() {
|
||||
if err := build.Process.Kill(); err != nil {
|
||||
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()})
|
||||
log.Fatal(Red("Failed to stop: "), Red(err))
|
||||
p.Fatal("Failed to stop:", err)
|
||||
}
|
||||
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"})
|
||||
log.Println(pname(p.Name, 2), ":", RedS("Ended"))
|
||||
|
@ -70,10 +70,10 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
|||
}
|
||||
if p.Watcher.Output["file"] {
|
||||
path := filepath.Join(p.base, p.parent.Files["output"])
|
||||
f := create(path)
|
||||
f := p.Create(path)
|
||||
t := time.Now()
|
||||
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil {
|
||||
log.Fatal(err)
|
||||
p.Fatal("", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ type logWriter struct{}
|
|||
|
||||
// Projects struct contains a projects list
|
||||
type Blueprint struct {
|
||||
c.Utils
|
||||
c.Config
|
||||
Projects []Project `yaml:"projects,omitempty"`
|
||||
Files map[string]string `yaml:"-"`
|
||||
Sync chan string `yaml:"-"`
|
||||
|
@ -33,7 +33,7 @@ type Blueprint struct {
|
|||
|
||||
// Project defines the informations of a single project
|
||||
type Project struct {
|
||||
c.Utils
|
||||
c.Config
|
||||
LastChangedOn time.Time `yaml:"-"`
|
||||
base string
|
||||
Name string `yaml:"app_name,omitempty"`
|
||||
|
|
49
cli/utils.go
49
cli/utils.go
|
@ -4,46 +4,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Read a file given a name and return its byte stream
|
||||
func read(file string) ([]byte, error) {
|
||||
_, err := os.Stat(file)
|
||||
if err == nil {
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return content, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Write a file given a name and a byte stream
|
||||
func write(name string, data []byte) error {
|
||||
err := ioutil.WriteFile(name, data, 0655)
|
||||
if err != nil {
|
||||
log.Fatal(Red(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create a new file and return its pointer
|
||||
func create(file string) *os.File {
|
||||
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// argsParam parse one by one the given argumentes
|
||||
func argsParam(params *cli.Context) []string {
|
||||
argsN := params.NArg()
|
||||
|
@ -57,19 +21,6 @@ func argsParam(params *cli.Context) []string {
|
|||
return nil
|
||||
}
|
||||
|
||||
// NameParam check the project name presence. If empty takes the working directory name
|
||||
func (p *Project) nameFlag(params *cli.Context) string {
|
||||
var name string
|
||||
if params.String("name") == "" && params.String("path") == "" {
|
||||
return p.Wdir()
|
||||
} else if params.String("path") != "/" {
|
||||
name = filepath.Base(params.String("path"))
|
||||
} else {
|
||||
name = params.String("name")
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// BoolParam is used to check the presence of a bool flag
|
||||
func boolFlag(b bool) bool {
|
||||
if b {
|
||||
|
|
|
@ -78,7 +78,7 @@ func (p *Project) watching() {
|
|||
|
||||
err := p.fmt(event.Name[:i] + ext)
|
||||
if err != nil {
|
||||
log.Fatal(Red(err))
|
||||
p.Fatal("", err)
|
||||
} else {
|
||||
go p.routines(channel, &wr)
|
||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
type Config struct{}
|
||||
|
||||
var Red, Blue, Yellow, Magenta = color.New(color.FgRed).SprintFunc(),
|
||||
color.New(color.FgBlue).SprintFunc(),
|
||||
color.New(color.FgYellow).SprintFunc(),
|
||||
color.New(color.FgMagenta).SprintFunc()
|
||||
|
||||
var GreenB, RedB, BlueB, YellowB, MagentaB = color.New(color.FgGreen, color.Bold).SprintFunc(),
|
||||
color.New(color.FgRed, color.Bold).SprintFunc(),
|
||||
color.New(color.FgBlue, color.Bold).SprintFunc(),
|
||||
color.New(color.FgYellow, color.Bold).SprintFunc(),
|
||||
color.New(color.FgMagenta, color.Bold).SprintFunc()
|
|
@ -0,0 +1,30 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Scan return a byte stream of a given file
|
||||
func (c *Config) Stream(file string) ([]byte, error) {
|
||||
_, err := os.Stat(file)
|
||||
if err == nil {
|
||||
content, err := ioutil.ReadFile(file)
|
||||
c.Validate(err)
|
||||
return content, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Write a file given a name and a byte stream
|
||||
func (c *Config) Write(name string, data []byte) error {
|
||||
err := ioutil.WriteFile(name, data, 0655)
|
||||
return c.Validate(err)
|
||||
}
|
||||
|
||||
// Create a new file and return its pointer
|
||||
func (c *Config) Create(file string) *os.File {
|
||||
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655)
|
||||
c.Validate(err)
|
||||
return out
|
||||
}
|
|
@ -6,17 +6,22 @@ import (
|
|||
"path/filepath"
|
||||
)
|
||||
|
||||
type Utils struct{}
|
||||
|
||||
func (u *Utils) Wdir() string {
|
||||
func (c *Config) Wdir() string {
|
||||
dir, err := os.Getwd()
|
||||
u.Validate(err)
|
||||
c.Validate(err)
|
||||
return filepath.Base(dir)
|
||||
}
|
||||
|
||||
func (u *Utils) Validate(err error) error {
|
||||
func (c *Config) Validate(err error) error {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(Red(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) Fatal(msg string, err error){
|
||||
if(msg != "") {
|
||||
log.Fatal(Red(msg), err.Error())
|
||||
}
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue