read,write file moved to config folder

This commit is contained in:
alessio 2016-10-21 17:30:12 +02:00
parent cf1461b5f1
commit e131e37349
9 changed files with 85 additions and 68 deletions

View File

@ -25,7 +25,7 @@ var R Realize
// Realize struct contains the general app informations // Realize struct contains the general app informations
type Realize struct { type Realize struct {
c.Utils c.Config
Name, Description, Author, Email, Host string Name, Description, Author, Email, Host string
Version string Version string
Limit uint64 Limit uint64
@ -50,7 +50,7 @@ func init() {
Sync: make(chan string), Sync: make(chan string),
} }
R.Blueprint = w.Blueprint{ R.Blueprint = w.Blueprint{
Utils: R.Utils, Config: R.Config,
Files: R.Files, Files: R.Files,
Sync: R.Sync, Sync: R.Sync,
} }

View File

@ -55,6 +55,7 @@ func (h *Blueprint) Fast(params *cli.Context) error {
// Add a new project // Add a new project
func (h *Blueprint) Add(params *cli.Context) error { func (h *Blueprint) Add(params *cli.Context) error {
p := Project{ p := Project{
Name: h.name(params),
Path: filepath.Clean(params.String("path")), Path: filepath.Clean(params.String("path")),
Build: params.Bool("build"), Build: params.Bool("build"),
Bin: boolFlag(params.Bool("no-bin")), 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 { if _, err := duplicates(p, h.Projects); err != nil {
return err return err
} }
@ -93,7 +93,7 @@ func (h *Blueprint) Clean() {
// Read, Check and remove duplicates from the config file // Read, Check and remove duplicates from the config file
func (h *Blueprint) Read() error { func (h *Blueprint) Read() error {
content, err := read(h.Files["config"]) content, err := h.Stream(h.Files["config"])
if err == nil { if err == nil {
err = yaml.Unmarshal(content, h) err = yaml.Unmarshal(content, h)
if err == nil { if err == nil {
@ -114,7 +114,7 @@ func (h *Blueprint) Create() error {
if err != nil { if err != nil {
return err return err
} }
return write(h.Files["config"], y) return h.Write(h.Files["config"], y)
} }
// Inserts a new project in the list // Inserts a new project in the list
@ -178,3 +178,16 @@ func (h *Blueprint) List() error {
} }
return err 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
}

View File

@ -30,7 +30,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
defer func() { defer func() {
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Failed to stop: " + err.Error()}) 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"}) p.Buffer.StdLog = append(p.Buffer.StdLog, BufferOut{Time: time.Now(), Text: "Ended"})
log.Println(pname(p.Name, 2), ":", RedS("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"] { if p.Watcher.Output["file"] {
path := filepath.Join(p.base, p.parent.Files["output"]) path := filepath.Join(p.base, p.parent.Files["output"])
f := create(path) f := p.Create(path)
t := time.Now() t := time.Now()
if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil { if _, err := f.WriteString(t.Format("2006-01-02 15:04:05") + " : " + output.Text() + "\r\n"); err != nil {
log.Fatal(err) p.Fatal("", err)
} }
} }
} }

View File

@ -25,7 +25,7 @@ type logWriter struct{}
// Projects struct contains a projects list // Projects struct contains a projects list
type Blueprint struct { type Blueprint struct {
c.Utils c.Config
Projects []Project `yaml:"projects,omitempty"` Projects []Project `yaml:"projects,omitempty"`
Files map[string]string `yaml:"-"` Files map[string]string `yaml:"-"`
Sync chan string `yaml:"-"` Sync chan string `yaml:"-"`
@ -33,7 +33,7 @@ type Blueprint struct {
// Project defines the informations of a single project // Project defines the informations of a single project
type Project struct { type Project struct {
c.Utils c.Config
LastChangedOn time.Time `yaml:"-"` LastChangedOn time.Time `yaml:"-"`
base string base string
Name string `yaml:"app_name,omitempty"` Name string `yaml:"app_name,omitempty"`

View File

@ -4,46 +4,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings" "strings"
"time" "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 // argsParam parse one by one the given argumentes
func argsParam(params *cli.Context) []string { func argsParam(params *cli.Context) []string {
argsN := params.NArg() argsN := params.NArg()
@ -57,19 +21,6 @@ func argsParam(params *cli.Context) []string {
return nil 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 // BoolParam is used to check the presence of a bool flag
func boolFlag(b bool) bool { func boolFlag(b bool) bool {
if b { if b {

View File

@ -78,7 +78,7 @@ func (p *Project) watching() {
err := p.fmt(event.Name[:i] + ext) err := p.fmt(event.Name[:i] + ext)
if err != nil { if err != nil {
log.Fatal(Red(err)) p.Fatal("", err)
} else { } else {
go p.routines(channel, &wr) go p.routines(channel, &wr)
p.LastChangedOn = time.Now().Truncate(time.Second) p.LastChangedOn = time.Now().Truncate(time.Second)

18
config/config.go Normal file
View File

@ -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()

30
config/io.go Normal file
View File

@ -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
}

View File

@ -6,17 +6,22 @@ import (
"path/filepath" "path/filepath"
) )
type Utils struct{} func (c *Config) Wdir() string {
func (u *Utils) Wdir() string {
dir, err := os.Getwd() dir, err := os.Getwd()
u.Validate(err) c.Validate(err)
return filepath.Base(dir) return filepath.Base(dir)
} }
func (u *Utils) Validate(err error) error { func (c *Config) Validate(err error) error {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(Red(err))
} }
return nil return nil
} }
func (c *Config) Fatal(msg string, err error){
if(msg != "") {
log.Fatal(Red(msg), err.Error())
}
log.Fatal(err.Error())
}