This commit is contained in:
parent
0c09e8f29b
commit
6e515d040a
22
cmd/add.go
22
cmd/add.go
@ -27,8 +27,8 @@ var addCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPotentialProjets(in string) []string {
|
func getPotentialProjets(in string) []string {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
err := r.Settings.Read(&r)
|
err := h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func getPotentialProjets(in string) []string {
|
|||||||
}
|
}
|
||||||
for _, dir := range cmdDir {
|
for _, dir := range cmdDir {
|
||||||
exists := false
|
exists := false
|
||||||
for _, proj := range r.Projects {
|
for _, proj := range h.Projects {
|
||||||
if dir.Name() == proj.Name {
|
if dir.Name() == proj.Name {
|
||||||
exists = true
|
exists = true
|
||||||
continue
|
continue
|
||||||
@ -73,24 +73,24 @@ func init() {
|
|||||||
// Add a project to an existing config or create a new one
|
// Add a project to an existing config or create a new one
|
||||||
func add(cmd *cobra.Command, args []string) (err error) {
|
func add(cmd *cobra.Command, args []string) (err error) {
|
||||||
addConfig.Name = args[0]
|
addConfig.Name = args[0]
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err = r.Settings.Read(&r)
|
err = h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
projects := len(r.Projects)
|
projects := len(h.Projects)
|
||||||
// create and add a new project
|
// create and add a new project
|
||||||
r.Add(r.New(addConfig))
|
h.Add(h.New(addConfig))
|
||||||
if len(r.Projects) > projects {
|
if len(h.Projects) > projects {
|
||||||
// update config
|
// update config
|
||||||
err = r.Settings.Write(r)
|
err = h.Settings.Write(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println(r.Prefix(haunt.Green.Bold("project successfully added")))
|
log.Println(h.Prefix(haunt.Green.Bold("project successfully added")))
|
||||||
} else {
|
} else {
|
||||||
log.Println(r.Prefix(haunt.Green.Bold("project can't be added")))
|
log.Println(h.Prefix(haunt.Green.Bold("project can't be added")))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -20,10 +20,10 @@ func init() {
|
|||||||
|
|
||||||
// Clean remove haunt file
|
// Clean remove haunt file
|
||||||
func clean(cmd *cobra.Command, args []string) (err error) {
|
func clean(cmd *cobra.Command, args []string) (err error) {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
if err := r.Settings.Remove(haunt.RFile); err != nil {
|
if err := h.Settings.Remove(haunt.HFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println(r.Prefix(haunt.Green.Bold("config file removed successfully removed")))
|
log.Println(h.Prefix(haunt.Green.Bold("config file removed successfully removed")))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
14
cmd/init.go
14
cmd/init.go
@ -23,10 +23,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig(cmd *cobra.Command, args []string) error {
|
func defaultConfig(cmd *cobra.Command, args []string) error {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
write := true
|
write := true
|
||||||
if _, err := os.Stat(haunt.RFile); err == nil {
|
if _, err := os.Stat(haunt.HFile); err == nil {
|
||||||
fmt.Print(r.Prefix("Config file exists. Overwire? " + haunt.Magenta.Bold("[y/n] ") + haunt.Green.Bold("(n) ")))
|
fmt.Print(h.Prefix("Config file exists. Overwire? " + haunt.Magenta.Bold("[y/n] ") + haunt.Green.Bold("(n) ")))
|
||||||
var overwrite string
|
var overwrite string
|
||||||
fmt.Scanf("%s", &overwrite)
|
fmt.Scanf("%s", &overwrite)
|
||||||
write = false
|
write = false
|
||||||
@ -36,15 +36,15 @@ func defaultConfig(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if write {
|
if write {
|
||||||
r.SetDefaults()
|
h.SetDefaults()
|
||||||
err := r.Settings.Write(r)
|
err := h.Settings.Write(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println(r.Prefix(
|
log.Println(h.Prefix(
|
||||||
"Config file has successfully been saved at .haunt.yaml",
|
"Config file has successfully been saved at .haunt.yaml",
|
||||||
))
|
))
|
||||||
log.Println(r.Prefix(
|
log.Println(h.Prefix(
|
||||||
"Run haunt add --help to see how to add more projects",
|
"Run haunt add --help to see how to add more projects",
|
||||||
))
|
))
|
||||||
return nil
|
return nil
|
||||||
|
@ -23,14 +23,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getProjectNames(input string) []string {
|
func getProjectNames(input string) []string {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err := r.Settings.Read(&r)
|
err := h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, project := range r.Projects {
|
for _, project := range h.Projects {
|
||||||
if strings.HasPrefix(project.Name, input) {
|
if strings.HasPrefix(project.Name, input) {
|
||||||
names = append(names, project.Name)
|
names = append(names, project.Name)
|
||||||
}
|
}
|
||||||
@ -40,22 +40,22 @@ func getProjectNames(input string) []string {
|
|||||||
|
|
||||||
// Remove a project from an existing config
|
// Remove a project from an existing config
|
||||||
func remove(cmd *cobra.Command, args []string) (err error) {
|
func remove(cmd *cobra.Command, args []string) (err error) {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err = r.Settings.Read(&r)
|
err = h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
err = r.Remove(arg)
|
err = h.Remove(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(r.Prefix(haunt.Red.Bold(arg + " project not found")))
|
log.Println(h.Prefix(haunt.Red.Bold(arg + " project not found")))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Println(r.Prefix(haunt.Green.Bold(arg + " successfully removed")))
|
log.Println(h.Prefix(haunt.Green.Bold(arg + " successfully removed")))
|
||||||
}
|
}
|
||||||
// update config
|
// update config
|
||||||
err = r.Settings.Write(r)
|
err = h.Settings.Write(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
30
cmd/run.go
30
cmd/run.go
@ -23,14 +23,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getProjectNamesToRun(input string) []string {
|
func getProjectNamesToRun(input string) []string {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err := r.Settings.Read(&r)
|
err := h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, project := range r.Projects {
|
for _, project := range h.Projects {
|
||||||
if strings.HasPrefix(project.Name, input) {
|
if strings.HasPrefix(project.Name, input) {
|
||||||
names = append(names, project.Name)
|
names = append(names, project.Name)
|
||||||
}
|
}
|
||||||
@ -40,41 +40,41 @@ func getProjectNamesToRun(input string) []string {
|
|||||||
|
|
||||||
// haunt workflow
|
// haunt workflow
|
||||||
func run(cmd *cobra.Command, args []string) (err error) {
|
func run(cmd *cobra.Command, args []string) (err error) {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
|
|
||||||
// read a config if exist
|
// read a config if exist
|
||||||
err = r.Settings.Read(&r)
|
err = h.Settings.Read(&h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(args) >= 1 {
|
if len(args) >= 1 {
|
||||||
// filter by name flag if exist
|
// filter by name flag if exist
|
||||||
r.Projects = r.Filter(args)
|
h.Projects = h.Filter(args)
|
||||||
if len(r.Projects) == 0 {
|
if len(h.Projects) == 0 {
|
||||||
log.Println(r.Prefix("No valid project found, exiting. Check your config file or run haunt add"))
|
log.Println(h.Prefix("No valid project found, exiting. Check your config file or run haunt add"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// increase file limit
|
// increase file limit
|
||||||
if r.Settings.FileLimit != 0 {
|
if h.Settings.FileLimit != 0 {
|
||||||
if err = r.Settings.Flimit(); err != nil {
|
if err = h.Settings.Flimit(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// web server
|
// web server
|
||||||
if r.Server.Status {
|
if h.Server.Status {
|
||||||
r.Server.Parent = r
|
h.Server.Parent = h
|
||||||
err = r.Server.Start()
|
err = h.Server.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = r.Server.OpenURL()
|
err = h.Server.OpenURL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// run workflow
|
// run workflow
|
||||||
return r.Run()
|
return h.Run()
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,6 @@ func init() {
|
|||||||
|
|
||||||
// Version print current version
|
// Version print current version
|
||||||
func version(cmd *cobra.Command, args []string) {
|
func version(cmd *cobra.Command, args []string) {
|
||||||
r := haunt.NewHaunt()
|
h := haunt.NewHaunt()
|
||||||
log.Println(r.Prefix(haunt.Green.Bold(Version)))
|
log.Println(h.Prefix(haunt.Green.Bold(Version)))
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// RPrefix tool name
|
// HPrefix tool name
|
||||||
RPrefix = "haunt"
|
HPrefix = "haunt"
|
||||||
// RExt file extension
|
// HExt file extension
|
||||||
RExt = ".yaml"
|
HExt = ".yaml"
|
||||||
// RFile config file name
|
// HFile config file name
|
||||||
RFile = "." + RPrefix + RExt
|
HFile = "." + HPrefix + HExt
|
||||||
// RExtWin windows extension
|
// HExtWin windows extension
|
||||||
RExtWin = ".exe"
|
HExtWin = ".exe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -76,17 +76,17 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Haunt) SetDefaults() {
|
func (h *Haunt) SetDefaults() {
|
||||||
r.Server = Server{Parent: r, Status: true, Open: false, Port: Port}
|
h.Server = Server{Parent: h, Status: true, Open: false, Port: Port}
|
||||||
r.Settings.FileLimit = 0
|
h.Settings.FileLimit = 0
|
||||||
r.Settings.Legacy.Interval = 100 * time.Millisecond
|
h.Settings.Legacy.Interval = 100 * time.Millisecond
|
||||||
r.Settings.Legacy.Force = false
|
h.Settings.Legacy.Force = false
|
||||||
r.Settings.Errors = Resource{Name: FileErr, Status: false}
|
h.Settings.Errors = Resource{Name: FileErr, Status: false}
|
||||||
r.Settings.Errors = Resource{Name: FileOut, Status: false}
|
h.Settings.Errors = Resource{Name: FileOut, Status: false}
|
||||||
r.Settings.Errors = Resource{Name: FileLog, Status: false}
|
h.Settings.Errors = Resource{Name: FileLog, Status: false}
|
||||||
if _, err := os.Stat("main.go"); err == nil {
|
if _, err := os.Stat("main.go"); err == nil {
|
||||||
log.Println(r.Prefix(Green.Bold("Adding: " + filepath.Base(Wdir()))))
|
log.Println(h.Prefix(Green.Bold("Adding: " + filepath.Base(Wdir()))))
|
||||||
r.Projects = append(r.Projects, Project{
|
h.Projects = append(h.Projects, Project{
|
||||||
Name: filepath.Base(Wdir()),
|
Name: filepath.Base(Wdir()),
|
||||||
Path: Wdir(),
|
Path: Wdir(),
|
||||||
Tools: Tools{
|
Tools: Tools{
|
||||||
@ -103,17 +103,17 @@ func (r *Haunt) SetDefaults() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
log.Println(r.Prefix(Magenta.Bold("Skipping: " + filepath.Base(Wdir()) + " no main.go file in root")))
|
log.Println(h.Prefix(Magenta.Bold("Skipping: " + filepath.Base(Wdir()) + " no main.go file in root")))
|
||||||
}
|
}
|
||||||
subDirs, err := os.ReadDir("cmd")
|
subDirs, err := os.ReadDir("cmd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(r.Prefix("cmd directory not found, skipping"))
|
log.Println(h.Prefix("cmd directory not found, skipping"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, dir := range subDirs {
|
for _, dir := range subDirs {
|
||||||
if dir.IsDir() {
|
if dir.IsDir() {
|
||||||
log.Println(r.Prefix(Green.Bold("Adding: " + dir.Name())))
|
log.Println(h.Prefix(Green.Bold("Adding: " + dir.Name())))
|
||||||
r.Projects = append(r.Projects, Project{
|
h.Projects = append(h.Projects, Project{
|
||||||
Name: dir.Name(),
|
Name: dir.Name(),
|
||||||
Path: "cmd/" + dir.Name(),
|
Path: "cmd/" + dir.Name(),
|
||||||
Tools: Tools{
|
Tools: Tools{
|
||||||
@ -130,31 +130,31 @@ func (r *Haunt) SetDefaults() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
log.Println(r.Prefix(Magenta.Bold("Skipping: " + dir.Name() + " not a directory")))
|
log.Println(h.Prefix(Magenta.Bold("Skipping: " + dir.Name() + " not a directory")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop haunt workflow
|
// Stop haunt workflow
|
||||||
func (r *Haunt) Stop() error {
|
func (h *Haunt) Stop() error {
|
||||||
for k := range r.Projects {
|
for k := range h.Projects {
|
||||||
if r.Schema.Projects[k].exit != nil {
|
if h.Schema.Projects[k].exit != nil {
|
||||||
close(r.Schema.Projects[k].exit)
|
close(h.Schema.Projects[k].exit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run haunt workflow
|
// Run haunt workflow
|
||||||
func (r *Haunt) Run() error {
|
func (h *Haunt) Run() error {
|
||||||
if len(r.Projects) > 0 {
|
if len(h.Projects) > 0 {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(len(r.Projects))
|
wg.Add(len(h.Projects))
|
||||||
for k := range r.Projects {
|
for k := range h.Projects {
|
||||||
r.Schema.Projects[k].exit = make(chan os.Signal, 1)
|
h.Schema.Projects[k].exit = make(chan os.Signal, 1)
|
||||||
signal.Notify(r.Schema.Projects[k].exit, os.Interrupt)
|
signal.Notify(h.Schema.Projects[k].exit, os.Interrupt)
|
||||||
r.Schema.Projects[k].parent = r
|
h.Schema.Projects[k].parent = h
|
||||||
go r.Schema.Projects[k].Watch(&wg)
|
go h.Schema.Projects[k].Watch(&wg)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
} else {
|
} else {
|
||||||
@ -164,9 +164,9 @@ func (r *Haunt) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prefix a given string with tool name
|
// Prefix a given string with tool name
|
||||||
func (r *Haunt) Prefix(input string) string {
|
func (h *Haunt) Prefix(input string) string {
|
||||||
if len(input) > 0 {
|
if len(input) > 0 {
|
||||||
return fmt.Sprint(Yellow.Bold("["), strings.ToUpper(RPrefix), Yellow.Bold("]"), ": ", input)
|
return fmt.Sprint(Yellow.Bold("["), strings.ToUpper(HPrefix), Yellow.Bold("]"), ": ", input)
|
||||||
}
|
}
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
@ -637,13 +637,13 @@ func (p *Project) run(path string, stream chan Response, stop <-chan bool) (err
|
|||||||
}
|
}
|
||||||
if _, err = os.Stat(path); err == nil {
|
if _, err = os.Stat(path); err == nil {
|
||||||
build = exec.Command(path, args...)
|
build = exec.Command(path, args...)
|
||||||
} else if _, err = os.Stat(path + RExtWin); err == nil {
|
} else if _, err = os.Stat(path + HExtWin); err == nil {
|
||||||
build = exec.Command(path+RExtWin, args...)
|
build = exec.Command(path+HExtWin, args...)
|
||||||
} else {
|
} else {
|
||||||
if _, err = os.Stat(path); err == nil {
|
if _, err = os.Stat(path); err == nil {
|
||||||
build = exec.Command(path, args...)
|
build = exec.Command(path, args...)
|
||||||
} else if _, err = os.Stat(path + RExtWin); err == nil {
|
} else if _, err = os.Stat(path + HExtWin); err == nil {
|
||||||
build = exec.Command(path+RExtWin, args...)
|
build = exec.Command(path+HExtWin, args...)
|
||||||
} else {
|
} else {
|
||||||
return errors.New("project not found")
|
return errors.New("project not found")
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@ import (
|
|||||||
const (
|
const (
|
||||||
Permission = 0o775
|
Permission = 0o775
|
||||||
File = ".haunt.yaml"
|
File = ".haunt.yaml"
|
||||||
FileOut = ".r.outputs.log"
|
FileOut = ".h.outputs.log"
|
||||||
FileErr = ".r.errors.log"
|
FileErr = ".h.errors.log"
|
||||||
FileLog = ".r.logs.log"
|
FileLog = ".h.logs.log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Settings defines a group of general settings and options
|
// Settings defines a group of general settings and options
|
||||||
@ -71,10 +71,10 @@ func (s *Settings) Remove(d string) error {
|
|||||||
// Read config file
|
// Read config file
|
||||||
func (s *Settings) Read(out interface{}) error {
|
func (s *Settings) Read(out interface{}) error {
|
||||||
// backward compatibility
|
// backward compatibility
|
||||||
if _, err := os.Stat(RFile); err != nil {
|
if _, err := os.Stat(HFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
content, err := s.Stream(RFile)
|
content, err := s.Stream(HFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = yaml.Unmarshal(content, out)
|
err = yaml.Unmarshal(content, out)
|
||||||
return err
|
return err
|
||||||
@ -88,7 +88,7 @@ func (s *Settings) Write(out interface{}) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Fatal(os.WriteFile(RFile, y, Permission))
|
s.Fatal(os.WriteFile(HFile, y, Permission))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user