clean
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2023-03-05 23:39:01 -08:00
parent 0c09e8f29b
commit 6e515d040a
9 changed files with 94 additions and 94 deletions

View File

@ -27,8 +27,8 @@ var addCmd = &cobra.Command{
}
func getPotentialProjets(in string) []string {
r := haunt.NewHaunt()
err := r.Settings.Read(&r)
h := haunt.NewHaunt()
err := h.Settings.Read(&h)
if err != nil {
return []string{}
}
@ -39,7 +39,7 @@ func getPotentialProjets(in string) []string {
}
for _, dir := range cmdDir {
exists := false
for _, proj := range r.Projects {
for _, proj := range h.Projects {
if dir.Name() == proj.Name {
exists = true
continue
@ -73,24 +73,24 @@ func init() {
// Add a project to an existing config or create a new one
func add(cmd *cobra.Command, args []string) (err error) {
addConfig.Name = args[0]
r := haunt.NewHaunt()
h := haunt.NewHaunt()
// read a config if exist
err = r.Settings.Read(&r)
err = h.Settings.Read(&h)
if err != nil {
return err
}
projects := len(r.Projects)
projects := len(h.Projects)
// create and add a new project
r.Add(r.New(addConfig))
if len(r.Projects) > projects {
h.Add(h.New(addConfig))
if len(h.Projects) > projects {
// update config
err = r.Settings.Write(r)
err = h.Settings.Write(h)
if err != nil {
return err
}
log.Println(r.Prefix(haunt.Green.Bold("project successfully added")))
log.Println(h.Prefix(haunt.Green.Bold("project successfully added")))
} 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
}

View File

@ -20,10 +20,10 @@ func init() {
// Clean remove haunt file
func clean(cmd *cobra.Command, args []string) (err error) {
r := haunt.NewHaunt()
if err := r.Settings.Remove(haunt.RFile); err != nil {
h := haunt.NewHaunt()
if err := h.Settings.Remove(haunt.HFile); err != nil {
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
}

View File

@ -23,10 +23,10 @@ func init() {
}
func defaultConfig(cmd *cobra.Command, args []string) error {
r := haunt.NewHaunt()
h := haunt.NewHaunt()
write := true
if _, err := os.Stat(haunt.RFile); err == nil {
fmt.Print(r.Prefix("Config file exists. Overwire? " + haunt.Magenta.Bold("[y/n] ") + haunt.Green.Bold("(n) ")))
if _, err := os.Stat(haunt.HFile); err == nil {
fmt.Print(h.Prefix("Config file exists. Overwire? " + haunt.Magenta.Bold("[y/n] ") + haunt.Green.Bold("(n) ")))
var overwrite string
fmt.Scanf("%s", &overwrite)
write = false
@ -36,15 +36,15 @@ func defaultConfig(cmd *cobra.Command, args []string) error {
}
}
if write {
r.SetDefaults()
err := r.Settings.Write(r)
h.SetDefaults()
err := h.Settings.Write(h)
if err != nil {
return err
}
log.Println(r.Prefix(
log.Println(h.Prefix(
"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",
))
return nil

View File

@ -23,14 +23,14 @@ func init() {
}
func getProjectNames(input string) []string {
r := haunt.NewHaunt()
h := haunt.NewHaunt()
// read a config if exist
err := r.Settings.Read(&r)
err := h.Settings.Read(&h)
if err != nil {
return []string{}
}
names := []string{}
for _, project := range r.Projects {
for _, project := range h.Projects {
if strings.HasPrefix(project.Name, input) {
names = append(names, project.Name)
}
@ -40,22 +40,22 @@ func getProjectNames(input string) []string {
// Remove a project from an existing config
func remove(cmd *cobra.Command, args []string) (err error) {
r := haunt.NewHaunt()
h := haunt.NewHaunt()
// read a config if exist
err = r.Settings.Read(&r)
err = h.Settings.Read(&h)
if err != nil {
return err
}
for _, arg := range args {
err = r.Remove(arg)
err = h.Remove(arg)
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
}
log.Println(r.Prefix(haunt.Green.Bold(arg + " successfully removed")))
log.Println(h.Prefix(haunt.Green.Bold(arg + " successfully removed")))
}
// update config
err = r.Settings.Write(r)
err = h.Settings.Write(h)
if err != nil {
return err
}

View File

@ -23,14 +23,14 @@ func init() {
}
func getProjectNamesToRun(input string) []string {
r := haunt.NewHaunt()
h := haunt.NewHaunt()
// read a config if exist
err := r.Settings.Read(&r)
err := h.Settings.Read(&h)
if err != nil {
return []string{}
}
names := []string{}
for _, project := range r.Projects {
for _, project := range h.Projects {
if strings.HasPrefix(project.Name, input) {
names = append(names, project.Name)
}
@ -40,41 +40,41 @@ func getProjectNamesToRun(input string) []string {
// haunt workflow
func run(cmd *cobra.Command, args []string) (err error) {
r := haunt.NewHaunt()
h := haunt.NewHaunt()
// read a config if exist
err = r.Settings.Read(&r)
err = h.Settings.Read(&h)
if err != nil {
return err
}
if len(args) >= 1 {
// filter by name flag if exist
r.Projects = r.Filter(args)
if len(r.Projects) == 0 {
log.Println(r.Prefix("No valid project found, exiting. Check your config file or run haunt add"))
h.Projects = h.Filter(args)
if len(h.Projects) == 0 {
log.Println(h.Prefix("No valid project found, exiting. Check your config file or run haunt add"))
return
}
}
// increase file limit
if r.Settings.FileLimit != 0 {
if err = r.Settings.Flimit(); err != nil {
if h.Settings.FileLimit != 0 {
if err = h.Settings.Flimit(); err != nil {
return err
}
}
// web server
if r.Server.Status {
r.Server.Parent = r
err = r.Server.Start()
if h.Server.Status {
h.Server.Parent = h
err = h.Server.Start()
if err != nil {
return err
}
err = r.Server.OpenURL()
err = h.Server.OpenURL()
if err != nil {
return err
}
}
// run workflow
return r.Run()
return h.Run()
}

View File

@ -38,6 +38,6 @@ func init() {
// Version print current version
func version(cmd *cobra.Command, args []string) {
r := haunt.NewHaunt()
log.Println(r.Prefix(haunt.Green.Bold(Version)))
h := haunt.NewHaunt()
log.Println(h.Prefix(haunt.Green.Bold(Version)))
}

View File

@ -16,14 +16,14 @@ import (
)
var (
// RPrefix tool name
RPrefix = "haunt"
// RExt file extension
RExt = ".yaml"
// RFile config file name
RFile = "." + RPrefix + RExt
// RExtWin windows extension
RExtWin = ".exe"
// HPrefix tool name
HPrefix = "haunt"
// HExt file extension
HExt = ".yaml"
// HFile config file name
HFile = "." + HPrefix + HExt
// HExtWin windows extension
HExtWin = ".exe"
)
type (
@ -76,17 +76,17 @@ func init() {
}
}
func (r *Haunt) SetDefaults() {
r.Server = Server{Parent: r, Status: true, Open: false, Port: Port}
r.Settings.FileLimit = 0
r.Settings.Legacy.Interval = 100 * time.Millisecond
r.Settings.Legacy.Force = false
r.Settings.Errors = Resource{Name: FileErr, Status: false}
r.Settings.Errors = Resource{Name: FileOut, Status: false}
r.Settings.Errors = Resource{Name: FileLog, Status: false}
func (h *Haunt) SetDefaults() {
h.Server = Server{Parent: h, Status: true, Open: false, Port: Port}
h.Settings.FileLimit = 0
h.Settings.Legacy.Interval = 100 * time.Millisecond
h.Settings.Legacy.Force = false
h.Settings.Errors = Resource{Name: FileErr, Status: false}
h.Settings.Errors = Resource{Name: FileOut, Status: false}
h.Settings.Errors = Resource{Name: FileLog, Status: false}
if _, err := os.Stat("main.go"); err == nil {
log.Println(r.Prefix(Green.Bold("Adding: " + filepath.Base(Wdir()))))
r.Projects = append(r.Projects, Project{
log.Println(h.Prefix(Green.Bold("Adding: " + filepath.Base(Wdir()))))
h.Projects = append(h.Projects, Project{
Name: filepath.Base(Wdir()),
Path: Wdir(),
Tools: Tools{
@ -103,17 +103,17 @@ func (r *Haunt) SetDefaults() {
},
})
} 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")
if err != nil {
log.Println(r.Prefix("cmd directory not found, skipping"))
log.Println(h.Prefix("cmd directory not found, skipping"))
return
}
for _, dir := range subDirs {
if dir.IsDir() {
log.Println(r.Prefix(Green.Bold("Adding: " + dir.Name())))
r.Projects = append(r.Projects, Project{
log.Println(h.Prefix(Green.Bold("Adding: " + dir.Name())))
h.Projects = append(h.Projects, Project{
Name: dir.Name(),
Path: "cmd/" + dir.Name(),
Tools: Tools{
@ -130,31 +130,31 @@ func (r *Haunt) SetDefaults() {
},
})
} 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
func (r *Haunt) Stop() error {
for k := range r.Projects {
if r.Schema.Projects[k].exit != nil {
close(r.Schema.Projects[k].exit)
func (h *Haunt) Stop() error {
for k := range h.Projects {
if h.Schema.Projects[k].exit != nil {
close(h.Schema.Projects[k].exit)
}
}
return nil
}
// Run haunt workflow
func (r *Haunt) Run() error {
if len(r.Projects) > 0 {
func (h *Haunt) Run() error {
if len(h.Projects) > 0 {
var wg sync.WaitGroup
wg.Add(len(r.Projects))
for k := range r.Projects {
r.Schema.Projects[k].exit = make(chan os.Signal, 1)
signal.Notify(r.Schema.Projects[k].exit, os.Interrupt)
r.Schema.Projects[k].parent = r
go r.Schema.Projects[k].Watch(&wg)
wg.Add(len(h.Projects))
for k := range h.Projects {
h.Schema.Projects[k].exit = make(chan os.Signal, 1)
signal.Notify(h.Schema.Projects[k].exit, os.Interrupt)
h.Schema.Projects[k].parent = h
go h.Schema.Projects[k].Watch(&wg)
}
wg.Wait()
} else {
@ -164,9 +164,9 @@ func (r *Haunt) Run() error {
}
// 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 {
return fmt.Sprint(Yellow.Bold("["), strings.ToUpper(RPrefix), Yellow.Bold("]"), ": ", input)
return fmt.Sprint(Yellow.Bold("["), strings.ToUpper(HPrefix), Yellow.Bold("]"), ": ", input)
}
return input
}

View File

@ -637,13 +637,13 @@ func (p *Project) run(path string, stream chan Response, stop <-chan bool) (err
}
if _, err = os.Stat(path); err == nil {
build = exec.Command(path, args...)
} else if _, err = os.Stat(path + RExtWin); err == nil {
build = exec.Command(path+RExtWin, args...)
} else if _, err = os.Stat(path + HExtWin); err == nil {
build = exec.Command(path+HExtWin, args...)
} else {
if _, err = os.Stat(path); err == nil {
build = exec.Command(path, args...)
} else if _, err = os.Stat(path + RExtWin); err == nil {
build = exec.Command(path+RExtWin, args...)
} else if _, err = os.Stat(path + HExtWin); err == nil {
build = exec.Command(path+HExtWin, args...)
} else {
return errors.New("project not found")
}

View File

@ -13,9 +13,9 @@ import (
const (
Permission = 0o775
File = ".haunt.yaml"
FileOut = ".r.outputs.log"
FileErr = ".r.errors.log"
FileLog = ".r.logs.log"
FileOut = ".h.outputs.log"
FileErr = ".h.errors.log"
FileLog = ".h.logs.log"
)
// Settings defines a group of general settings and options
@ -71,10 +71,10 @@ func (s *Settings) Remove(d string) error {
// Read config file
func (s *Settings) Read(out interface{}) error {
// backward compatibility
if _, err := os.Stat(RFile); err != nil {
if _, err := os.Stat(HFile); err != nil {
return err
}
content, err := s.Stream(RFile)
content, err := s.Stream(HFile)
if err == nil {
err = yaml.Unmarshal(content, out)
return err
@ -88,7 +88,7 @@ func (s *Settings) Write(out interface{}) error {
if err != nil {
return err
}
s.Fatal(os.WriteFile(RFile, y, Permission))
s.Fatal(os.WriteFile(HFile, y, Permission))
return nil
}