Skip to content

Commit

Permalink
feat: Add what did I do
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Jan 1, 2024
1 parent 192f31a commit a5d56ab
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 115 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nousefreak/projecthelper
go 1.21.1

require (
github.com/NoUseFreak/go-parallel v0.0.0-20190604224018-2ba374bf3cf1
github.com/erikgeiser/promptkit v0.9.0
github.com/google/go-github/v57 v57.0.0
github.com/ktr0731/go-fuzzyfinder v0.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/NoUseFreak/go-parallel v0.0.0-20190604224018-2ba374bf3cf1 h1:ikIoLWQxSLM8ij5jTtrwd4G4Nu9URZqpHLjr6rp/pdw=
github.com/NoUseFreak/go-parallel v0.0.0-20190604224018-2ba374bf3cf1/go.mod h1:SgEeD5hW/Qrgzcq6Xj9sXq+To5azjcbCvljmqxFaXY0=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
Expand Down
38 changes: 38 additions & 0 deletions internal/pkg/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package color

import (
"fmt"
)

const (
Reset int = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)

const (
FgBlack int = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)

const (
Escape = "\x1b"
)


func Color(a int, msg string) string {
return fmt.Sprintf("%s[%dm%s%s[%dm", Escape, a, msg, Escape, Reset)
}
95 changes: 33 additions & 62 deletions internal/pkg/command/log.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
package command

import (
"bytes"
"fmt"
"os"
"strings"
"bytes"
"os"
"strings"

"github.com/sirupsen/logrus"
)


const (
Reset int = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
ReverseVideo
Concealed
CrossedOut
)

const (
FgBlack int = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)

const (
Escape = "\x1b"
"github.com/nousefreak/projecthelper/internal/pkg/color"
"github.com/sirupsen/logrus"
)

func init() {
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&cliFormatter{})
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&cliFormatter{})
}

var CmdOutput = os.Stdout
Expand All @@ -49,33 +20,33 @@ type cliFormatter struct {
}

func (f cliFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}

switch entry.Level {
case logrus.InfoLevel:
b.WriteString(f.color(FgGreen, "* "))
case logrus.WarnLevel:
b.WriteString(f.color(FgYellow, "W "))
case logrus.ErrorLevel:
b.WriteString(f.color(FgRed, "E "))
case logrus.FatalLevel:
b.WriteString(f.color(FgRed, "F "))
default:
b.WriteString(" ")
}

entry.Message = strings.TrimSuffix(entry.Message, "\n")
b.WriteString(entry.Message)

b.WriteString("\n")
return b.Bytes(), nil
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}

switch entry.Level {
case logrus.InfoLevel:
b.WriteString(color.Color(color.FgGreen, "* "))
case logrus.WarnLevel:
b.WriteString(color.Color(color.FgYellow, "W "))
case logrus.ErrorLevel:
b.WriteString(color.Color(color.FgRed, "E "))
case logrus.FatalLevel:
b.WriteString(color.Color(color.FgRed, "F "))
default:
b.WriteString(" ")
}

entry.Message = strings.TrimSuffix(entry.Message, "\n")
b.WriteString(entry.Message)

b.WriteString("\n")
return b.Bytes(), nil
}

func (f cliFormatter) color(a int, msg string) string {
return fmt.Sprintf("%s[%dm%s%s[%dm", Escape, a, msg, Escape, Reset)
return color.Color(a, msg)
}
1 change: 1 addition & 0 deletions internal/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Execute() {
rootCmd.AddCommand(getUpdateCmd())
rootCmd.AddCommand(getVersionCmd())
rootCmd.AddCommand(getRepoCmd())
rootCmd.AddCommand(getWDIDCmd())

cobra.OnInitialize(config.InitConfig)

Expand Down
62 changes: 62 additions & 0 deletions internal/pkg/command/wdid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package command

import (
"fmt"
"os"
"time"

"github.com/nousefreak/projecthelper/internal/pkg/color"
"github.com/nousefreak/projecthelper/internal/pkg/config"
"github.com/nousefreak/projecthelper/internal/pkg/repo"
"github.com/nousefreak/projecthelper/internal/pkg/wdid"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/term"
)

func getWDIDCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "wdid",
Short: "wdid",
Long: `wdid`,
Run: func(cmd *cobra.Command, args []string) {

amount := "1"
unit := "day"

if len(args) >= 1 {
amount = args[0]
}
if len(args) >= 2 {
unit = args[1]
}

window := fmt.Sprintf("%s %s ago", amount, unit)

repoPaths, err := repo.GetRepoPaths(config.GetBaseDir())
if err != nil {
logrus.Fatal(err)
}

logrus.Infof("Looking for your commits in %d repos since %s", len(repoPaths), window)
reports, err := wdid.GetWDIDReport(window, repoPaths)
if err != nil {
logrus.Fatal(err)
}

width, _, _ := term.GetSize(0)
out := os.Stderr
for name, group := range reports {
fmt.Fprintln(out, "")
fmt.Fprintf(out, "%s\n", color.Color(color.FgGreen, name))
for _, r := range group {
t := time.Unix(r.Timestamp(), 0)
fmt.Fprintf(out, " - %-*s %s\n", width-22, r.ChangeLine(), color.Color(color.FgBlue, t.Format("2006-01-02 15:04")))
}
}

},
}
return cmd

}
85 changes: 32 additions & 53 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,43 @@ import (
)

func GetRepoPathsAsync(baseDir string, result *[]string) error {
entries, err := os.ReadDir(baseDir)
if err != nil {
return err
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

if entry.Name() == ".git" {
*result = append(*result, baseDir)
return nil
}

err := GetRepoPathsAsync(fmt.Sprintf("%s/%s", baseDir, entry.Name()), result)
if err != nil {
return err
}
}

return nil
entries, err := os.ReadDir(baseDir)
if err != nil {
return err
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

if entry.Name() == ".git" {
*result = append(*result, baseDir)
return nil
}

err := GetRepoPathsAsync(fmt.Sprintf("%s/%s", baseDir, entry.Name()), result)
if err != nil {
return err
}
}

return nil
}

func GetRepoPaths(baseDir string) ([]string, error) {
result := []string{}

entries, err := os.ReadDir(baseDir)
if err != nil {
return result, err
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

if entry.Name() == ".git" {
return []string{baseDir}, nil
}

paths, err := GetRepoPaths(fmt.Sprintf("%s/%s", baseDir, entry.Name()))
if err != nil {
return result, err
}

result = append(result, paths...)
}
result := []string{}
err := GetRepoPathsAsync(baseDir, &result)

return result, nil
return result, err
}

func FilterRepoPaths(paths []string, filter string) []string {
result := []string{}
for _, path := range paths {
if strings.Contains(path, filter) {
result = append(result, path)
}
}
return result
result := []string{}
for _, path := range paths {
if strings.Contains(path, filter) {
result = append(result, path)
}
}
return result
}
Loading

0 comments on commit a5d56ab

Please sign in to comment.