Skip to content

Commit

Permalink
feat: print beautifully
Browse files Browse the repository at this point in the history
  • Loading branch information
DenChenn committed May 26, 2023
1 parent d3e86ab commit c0f6f77
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 39 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/DenChenn/blunder
go 1.20

require (
github.com/briandowns/spinner v1.23.0
github.com/enescakir/emoji v1.0.0
github.com/gin-gonic/gin v1.7.7
github.com/sashabaranov/go-openai v1.9.4
github.com/urfave/cli/v2 v2.25.3
Expand All @@ -12,6 +14,7 @@ require (

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand All @@ -20,6 +23,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -28,6 +32,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
Expand Down Expand Up @@ -36,6 +42,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -75,11 +84,14 @@ golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
Expand Down
40 changes: 19 additions & 21 deletions internal/codegen/cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package cmd

import (
"errors"
"github.com/DenChenn/blunder/internal/codegen/gpt"
"github.com/DenChenn/blunder/internal/codegen/model"
"github.com/DenChenn/blunder/internal/codegen/template"
"github.com/DenChenn/blunder/internal/codegen/util"
"github.com/DenChenn/blunder/internal/constant"
"github.com/DenChenn/blunder/internal/util"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)

const (
GeneratedDirName = "generated"
ErrorFileName = "error.go"
ErrorFileTemplateFileName = "error.go.tmpl"
GenerateBlunderYamlTemplateFileName = "gen_blunder.yaml.tmpl"
)

var Gen = &cli.Command{
Name: "gen",
Usage: "generate all errors",
Expand All @@ -31,25 +24,27 @@ var Gen = &cli.Command{
},
},
Action: func(cCtx *cli.Context) error {
sGen := util.PrintLoading("generating all errors...", "all errors are generated successfully")

blunderPath := util.LocateBlunderYamlPath()
if blunderPath == "" {
return errors.New("blunder.yaml not found, please init the project first")
return util.PrintErrAndReturn("blunder.yaml not found, please init the project first")
}
blunderRootPath := util.GetFileDirPath(blunderPath)

f, err := os.ReadFile(blunderPath)
if err != nil {
return err
return util.PrintErrAndReturn(err.Error())
}

var blunderConfig model.Blunder
if err := yaml.Unmarshal(f, &blunderConfig); err != nil {
return err
return util.PrintErrAndReturn(err.Error())
}

// check all error code is provided
if !checkAllErrorCodeIsProvided(&blunderConfig) {
return errors.New("in blunder.yaml, all error code must be provided")
return util.PrintErrAndReturn("in blunder.yaml, all error code must be provided")
}

// complete error detail with gpt3
Expand All @@ -58,14 +53,15 @@ var Gen = &cli.Command{
// check if user provide gpt3 api token
_, exist := os.LookupEnv("OPENAI_API_TOKEN")
if !exist {
return errors.New("OPENAI_API_TOKEN not found, please export it with your own openai api token")
return util.PrintErrAndReturn("OPENAI_API_TOKEN not found, please export with your own openai api token")
}

sComplete := util.PrintLoading("auto-completing error detail with gpt3...", "error detail is completed successfully")
hasSomethingToComplete, indexMap, errorCodes := determineWhichToComplete(&blunderConfig)
if hasSomethingToComplete {
completed, err := gpt.CompleteErrorDetail(errorCodes)
if err != nil {
return err
return util.PrintErrAndReturn(err.Error())
}

for _, ec := range completed {
Expand All @@ -85,28 +81,30 @@ var Gen = &cli.Command{
}

// generate blunder.yaml again to record the completion
if err := template.Generate(blunderPath, GenerateBlunderYamlTemplateFileName, &blunderConfig); err != nil {
return err
if err := template.Generate(blunderPath, constant.GenerateBlunderYamlTemplateFileName, &blunderConfig); err != nil {
return util.PrintErrAndReturn(err.Error())
}
sComplete.Stop()
}

generatedRootPath := filepath.Join(blunderRootPath, GeneratedDirName)
generatedRootPath := filepath.Join(blunderRootPath, constant.GeneratedDirName)
clearGeneratedFolder(generatedRootPath)

for _, detail := range blunderConfig.Details {
errorFilePath := filepath.Join(generatedRootPath, detail.Package, ErrorFileName)
errorFilePath := filepath.Join(generatedRootPath, detail.Package, constant.ErrorFileName)

// generate id for this error
for i := range detail.Errors {
id := util.GetId(errorFilePath + detail.Errors[i].Code)
detail.Errors[i].Id = id
}

if err := template.Generate(errorFilePath, ErrorFileTemplateFileName, &detail); err != nil {
return err
if err := template.Generate(errorFilePath, constant.ErrorFileTemplateFileName, &detail); err != nil {
return util.PrintErrAndReturn(err.Error())
}
}

sGen.Stop()
return nil
},
}
Expand Down
18 changes: 8 additions & 10 deletions internal/codegen/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package cmd

import (
"errors"
"github.com/DenChenn/blunder/internal/codegen/template"
"github.com/DenChenn/blunder/internal/codegen/util"
"github.com/DenChenn/blunder/internal/constant"
"github.com/DenChenn/blunder/internal/util"
"github.com/urfave/cli/v2"
"path/filepath"
)

const (
BlunderYamlTemplateFileName = "blunder.yaml.tmpl"
)

var Init = &cli.Command{
Name: "init",
Usage: "generate error",
Action: func(cCtx *cli.Context) error {
initPath := cCtx.Args().Get(0)
if initPath == "" {
return errors.New("you should specify the path to init")
return util.PrintErrAndReturn("you should specify the path to init")
}

blunderYamlPath := filepath.Join(initPath, "errors", util.BlunderYamlFileName)
if err := template.Generate(blunderYamlPath, BlunderYamlTemplateFileName, nil); err != nil {
return err
s := util.PrintLoading("generating blunder.yaml ...", "blunder.yaml is generated successfully")
blunderYamlPath := filepath.Join(initPath, "errors", constant.BlunderYamlFileName)
if err := template.Generate(blunderYamlPath, constant.BlunderYamlTemplateFileName, nil); err != nil {
return util.PrintErrAndReturn(err.Error())
}
s.Stop()

return nil
},
Expand Down
2 changes: 0 additions & 2 deletions internal/codegen/gpt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func CompleteErrorDetail(errorCodes []string) ([]*model.ErrorDescription, error)
return nil, fmt.Errorf("ChatCompletion error: %v\n", err)
}

fmt.Println(resp.Choices[0].Message.Content)

groups := strings.Split(resp.Choices[0].Message.Content, groupSeparator)
if len(groups) != len(errorCodes) {
return nil, fmt.Errorf("response with wrong format from GPT-3 in group")
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/template/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package template
import (
"embed"
"fmt"
"github.com/DenChenn/blunder/internal/codegen/util"
"github.com/DenChenn/blunder/internal/util"
"os"
"text/template"
)
Expand Down
10 changes: 10 additions & 0 deletions internal/constant/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package constant

const (
BlunderYamlFileName = "blunder.yaml"
GeneratedDirName = "generated"
ErrorFileName = "error.go"
ErrorFileTemplateFileName = "error.go.tmpl"
GenerateBlunderYamlTemplateFileName = "gen_blunder.yaml.tmpl"
BlunderYamlTemplateFileName = "blunder.yaml.tmpl"
)
9 changes: 4 additions & 5 deletions internal/codegen/util/file.go → internal/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ package util

import (
"fmt"
"github.com/DenChenn/blunder/internal/constant"
"io/fs"
"os"
"path/filepath"
)

func GetRootPath() string {
func GetCWD() string {
wd, _ := os.Getwd()
return wd
}

const BlunderYamlFileName = "blunder.yaml"

func LocateBlunderYamlPath() string {
root := GetRootPath()
root := GetCWD()
location := ""
if err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() && info.Name() == BlunderYamlFileName {
if !info.IsDir() && info.Name() == constant.BlunderYamlFileName {
location = path
return nil
}
Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions internal/util/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import (
"fmt"
"github.com/briandowns/spinner"
"github.com/enescakir/emoji"
"time"
)

func PrintLoading(suffix string, final string) *spinner.Spinner {
s := spinner.New(spinner.CharSets[1], 100*time.Millisecond)
s.Suffix = " " + suffix + "\n"
s.FinalMSG = fmt.Sprintf("%v %v\n", emoji.CheckMark, final)
s.Start()
return s
}

func PrintErrAndReturn(s string) error {
fmt.Printf("%v %v\n", emoji.ExclamationMark, s)
return nil
}

0 comments on commit c0f6f77

Please sign in to comment.