From 6e515d040a75df634672b8d462ce5ef1bcc9eb7c Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 5 Mar 2023 23:39:01 -0800 Subject: [PATCH] clean --- cmd/add.go | 22 ++++++------- cmd/clean.go | 6 ++-- cmd/init.go | 14 ++++---- cmd/remove.go | 18 +++++------ cmd/run.go | 30 +++++++++--------- cmd/version.go | 4 +-- src/haunt/cli.go | 74 +++++++++++++++++++++---------------------- src/haunt/projects.go | 8 ++--- src/haunt/settings.go | 12 +++---- 9 files changed, 94 insertions(+), 94 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index c7c22e5..dbfdefc 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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 } diff --git a/cmd/clean.go b/cmd/clean.go index 8230770..0bfc049 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -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 } diff --git a/cmd/init.go b/cmd/init.go index 0a4ab27..2988d55 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 diff --git a/cmd/remove.go b/cmd/remove.go index 7d642d6..b08e230 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -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 } diff --git a/cmd/run.go b/cmd/run.go index f174121..588fcb7 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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() } diff --git a/cmd/version.go b/cmd/version.go index b98728d..0832243 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -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))) } diff --git a/src/haunt/cli.go b/src/haunt/cli.go index 3cb18f0..89af9ba 100644 --- a/src/haunt/cli.go +++ b/src/haunt/cli.go @@ -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 } diff --git a/src/haunt/projects.go b/src/haunt/projects.go index 7599c85..3fef207 100644 --- a/src/haunt/projects.go +++ b/src/haunt/projects.go @@ -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") } diff --git a/src/haunt/settings.go b/src/haunt/settings.go index 247f56c..cadb08f 100644 --- a/src/haunt/settings.go +++ b/src/haunt/settings.go @@ -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 }