Skip to content

Commit

Permalink
Merge pull request #212 from suzuki-shunsuke/feat/add-link-and-descri…
Browse files Browse the repository at this point in the history
…ption-to-packages

feat: add link and description to packages
  • Loading branch information
suzuki-shunsuke authored Sep 8, 2021
2 parents 91cb892 + a602bed commit f3fd1be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -120,13 +124,17 @@ 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{
Name: pkgInfo.Name,
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
Expand Down
27 changes: 26 additions & 1 deletion pkg/controller/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/ktr0731/go-fuzzyfinder"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -48,7 +49,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(),
formatDescription(pkg.PackageInfo.GetDescription(), w/2-8)) //nolint:gomnd
}))
if err != nil {
if errors.Is(err, fuzzyfinder.ErrAbort) {
return nil
Expand All @@ -66,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(),
Expand Down
14 changes: 12 additions & 2 deletions pkg/controller/github_release_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/controller/http_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
Expand Down

0 comments on commit f3fd1be

Please sign in to comment.