From bf39a8cb6b590f08f63ce6a1a879045309a49342 Mon Sep 17 00:00:00 2001 From: Suzuki Shunsuke Date: Wed, 8 Sep 2021 12:01:51 +0900 Subject: [PATCH 1/2] feat: add link and description to packages --- pkg/controller/config.go | 8 ++++++++ pkg/controller/generate.go | 9 ++++++++- pkg/controller/github_release_package.go | 14 ++++++++++++-- pkg/controller/http_package.go | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/pkg/controller/config.go b/pkg/controller/config.go index 61aad04fa..adbca2f98 100644 --- a/pkg/controller/config.go +++ b/pkg/controller/config.go @@ -97,6 +97,8 @@ type PackageInfo interface { GetFileSrc(pkg *Package, file *File) (string, error) GetPkgPath(rootDir string, pkg *Package) (string, error) RenderAsset(pkg *Package) (string, error) + GetLink() string + GetDescription() string } type mergedPackageInfo struct { @@ -108,6 +110,8 @@ type mergedPackageInfo struct { ArchiveType string `yaml:"archive_type"` Files []*File URL *text.Template + Description string + Link string } func (pkgInfo *mergedPackageInfo) GetPackageInfo() (PackageInfo, error) { @@ -120,6 +124,8 @@ func (pkgInfo *mergedPackageInfo) GetPackageInfo() (PackageInfo, error) { Asset: pkgInfo.Asset, ArchiveType: pkgInfo.ArchiveType, Files: pkgInfo.Files, + Link: pkgInfo.Link, + Description: pkgInfo.Description, }, nil case pkgInfoTypeHTTP: return &HTTPPackageInfo{ @@ -127,6 +133,8 @@ func (pkgInfo *mergedPackageInfo) GetPackageInfo() (PackageInfo, error) { ArchiveType: pkgInfo.ArchiveType, URL: pkgInfo.URL, Files: pkgInfo.Files, + Link: pkgInfo.Link, + Description: pkgInfo.Description, }, nil default: return nil, logerr.WithFields(errInvalidType, logrus.Fields{ //nolint:wrapcheck diff --git a/pkg/controller/generate.go b/pkg/controller/generate.go index b1afcbae4..d0a8f090b 100644 --- a/pkg/controller/generate.go +++ b/pkg/controller/generate.go @@ -48,7 +48,14 @@ func (ctrl *Controller) Generate(ctx context.Context, param *Param) error { //no idx, err := fuzzyfinder.Find(pkgs, func(i int) string { pkg := pkgs[i] return fmt.Sprintf("%s (%s)", pkg.PackageInfo.GetName(), pkg.RegistryName) - }) + }, + fuzzyfinder.WithPreviewWindow(func(i, w, h int) string { + pkg := pkgs[i] + return fmt.Sprintf("%s\n\n%s\n%s", + pkg.PackageInfo.GetName(), + pkg.PackageInfo.GetLink(), + pkg.PackageInfo.GetDescription()) + })) if err != nil { if errors.Is(err, fuzzyfinder.ErrAbort) { return nil diff --git a/pkg/controller/github_release_package.go b/pkg/controller/github_release_package.go index 788ff63df..53dda40f1 100644 --- a/pkg/controller/github_release_package.go +++ b/pkg/controller/github_release_package.go @@ -9,8 +9,10 @@ import ( ) type GitHubReleasePackageInfo struct { - Name string `validate:"required"` - ArchiveType string `yaml:"archive_type"` + Name string `validate:"required"` + ArchiveType string `yaml:"archive_type"` + Link string + Description string Files []*File `validate:"required,dive"` RepoOwner string `yaml:"repo_owner" validate:"required"` @@ -26,6 +28,14 @@ func (pkgInfo *GitHubReleasePackageInfo) GetType() string { return pkgInfoTypeGitHubRelease } +func (pkgInfo *GitHubReleasePackageInfo) GetLink() string { + return pkgInfo.Link +} + +func (pkgInfo *GitHubReleasePackageInfo) GetDescription() string { + return pkgInfo.Description +} + func (pkgInfo *GitHubReleasePackageInfo) GetArchiveType() string { return pkgInfo.ArchiveType } diff --git a/pkg/controller/http_package.go b/pkg/controller/http_package.go index 9eb25c3a3..5fc6389c8 100644 --- a/pkg/controller/http_package.go +++ b/pkg/controller/http_package.go @@ -10,8 +10,10 @@ import ( ) type HTTPPackageInfo struct { - Name string `validate:"required"` - ArchiveType string `yaml:"archive_type"` + Name string `validate:"required"` + ArchiveType string `yaml:"archive_type"` + Description string + Link string Files []*File `validate:"required,dive"` URL *text.Template `validate:"required"` @@ -25,6 +27,14 @@ func (pkgInfo *HTTPPackageInfo) GetType() string { return pkgInfoTypeHTTP } +func (pkgInfo *HTTPPackageInfo) GetLink() string { + return pkgInfo.Link +} + +func (pkgInfo *HTTPPackageInfo) GetDescription() string { + return pkgInfo.Description +} + func (pkgInfo *HTTPPackageInfo) GetArchiveType() string { return pkgInfo.ArchiveType } From a602bed2bdbb5a652e2d3be47d69fc64e3ce3dd9 Mon Sep 17 00:00:00 2001 From: Suzuki Shunsuke Date: Wed, 8 Sep 2021 14:24:10 +0900 Subject: [PATCH 2/2] fix: format the package description --- pkg/controller/generate.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/controller/generate.go b/pkg/controller/generate.go index d0a8f090b..3e6a511a2 100644 --- a/pkg/controller/generate.go +++ b/pkg/controller/generate.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strings" "github.com/ktr0731/go-fuzzyfinder" "github.com/sirupsen/logrus" @@ -54,7 +55,7 @@ func (ctrl *Controller) Generate(ctx context.Context, param *Param) error { //no return fmt.Sprintf("%s\n\n%s\n%s", pkg.PackageInfo.GetName(), pkg.PackageInfo.GetLink(), - pkg.PackageInfo.GetDescription()) + formatDescription(pkg.PackageInfo.GetDescription(), w/2-8)) //nolint:gomnd })) if err != nil { if errors.Is(err, fuzzyfinder.ErrAbort) { @@ -73,6 +74,23 @@ func (ctrl *Controller) Generate(ctx context.Context, param *Param) error { //no return nil } +func formatDescription(desc string, w int) string { + descRune := []rune(desc) + lenDescRune := len(descRune) + lineWidth := w - len([]rune("\n")) + numOfLines := (lenDescRune / lineWidth) + 1 + descArr := make([]string, numOfLines) + for i := 0; i < numOfLines; i++ { + start := i * lineWidth + end := start + lineWidth + if i == numOfLines-1 { + end = lenDescRune + } + descArr[i] = string(descRune[start:end]) + } + return strings.Join(descArr, "\n") +} + func (ctrl *Controller) getOutputtedPkg(ctx context.Context, pkg *FindingPackage) (*Package, error) { outputPkg := &Package{ Name: pkg.PackageInfo.GetName(),