Skip to content

Commit

Permalink
feat: Add marketplace app (#32)
Browse files Browse the repository at this point in the history
* add marketplace

* Update go.work

* Rename go.work to go.work.example

* Use AppsConfig from ignite cli

* Add test for tree view

* Remove tools.go

* Refactor test cases
  • Loading branch information
Ehsan-saradar authored Feb 1, 2024
1 parent 9ea4d6e commit be0006a
Show file tree
Hide file tree
Showing 15 changed files with 2,526 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.vscode
*.test
*.ign
go.work
go.work.sum
1 change: 1 addition & 0 deletions go.work → go.work.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use (
./explorer
./hermes
./integration
./official/marketplace
)
118 changes: 118 additions & 0 deletions official/marketplace/cmd/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cmd

import (
"fmt"
"hash/fnv"
"io"
"os"
"path"
"strconv"
"strings"
"text/tabwriter"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"github.com/ignite/apps/official/marketplace/pkg/apps"
"github.com/ignite/apps/official/marketplace/pkg/xgithub"
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/spf13/cobra"
)

var (
linkStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("10")).
Underline(true)
installaitonStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("9")).
MarginLeft(15)
commandStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("2")).
Bold(true)
)

// NewInfo creates a new info command that shows the details of an ignite application repository.
func NewInfo() *cobra.Command {
return &cobra.Command{
Use: "info [package-url]",
Short: "Show the details of an ignite application repository",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
githubToken, _ := cmd.Flags().GetString(githubTokenFlag)

session := cliui.New(cliui.StartSpinner())
defer session.End()

session.StartSpinner("🔎 Fetching repository details from GitHub...")

client := xgithub.NewClient(githubToken)
repo, err := apps.GetRepositoryDetails(cmd.Context(), client, args[0])
if err != nil {
return err
}

session.StopSpinner()

printRepoDetails(repo)

return nil
},
}
}

func printRepoDetails(repo *apps.AppRepositoryDetails) {
w := &tabwriter.Writer{}
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
printItem := func(s string, v interface{}) {
fmt.Fprintf(w, "%s:\t%v\n", s, v)
}

printItem("Description", repo.Description)
tags := make([]string, len(repo.Tags))
for i, tag := range repo.Tags {
tags[i] = lipgloss.NewStyle().Background(colorFromText(tag)).Render(tag)
}
printItem("Tags", strings.Join(tags, " "))
printItem("Stars", strconv.Itoa(repo.Stars))
printItem("License", repo.License)
printItem("Updated At", repo.UpdatedAt.Format(time.DateTime)+" "+updatedAtStyle.Render("("+humanize.Time(repo.UpdatedAt)+")"))
printItem("URL", linkStyle.Render(repo.URL))
printItem("Apps", "")
w.Flush()

printAppsTable(repo)
}

func colorFromText(text string) lipgloss.Color {
h := fnv.New64a()
h.Write([]byte(text))
return lipgloss.Color(strconv.FormatUint(h.Sum64()%16, 10))
}

func printAppsTable(repo *apps.AppRepositoryDetails) {
printItem := func(w io.Writer, s string, v interface{}) {
fmt.Fprintf(w, "\t%s:\t%v\n", s, v)
}

for i, app := range repo.Apps {
w := &tabwriter.Writer{}
w.Init(os.Stdout, 16, 8, 0, '\t', 0)

printItem(w, "Name", app.Name)
printItem(w, "Description", app.Description)
printItem(w, "Path", app.Path)
printItem(w, "Go Version", app.GoVersion)
printItem(w, "Ignite Version", app.IgniteVersion)
w.Flush()

fmt.Println(installaitonStyle.Render(fmt.Sprintf(
"🚀 Install via: %s",
commandStyle.Render(fmt.Sprintf("ignite app -g install %s", path.Join(repo.PackageURL, app.Path))),
)))

if i < len(repo.Apps)-1 {
fmt.Fprintln(w)
}
}
}
100 changes: 100 additions & 0 deletions official/marketplace/cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"github.com/ignite/apps/official/marketplace/pkg/apps"
"github.com/ignite/apps/official/marketplace/pkg/tree"
"github.com/ignite/apps/official/marketplace/pkg/xgithub"
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/spf13/cobra"
)

const (
minStarsFlag = "min-stars"
queryFlag = "query"
descriptionLimit = 75
)

var (
starsCountStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("3"))
updatedAtStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
)

// NewList creates a new list command that searches all the ignite apps in GitHub.
func NewList() *cobra.Command {
c := &cobra.Command{
Use: "list",
Short: "List all the ignite apps",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
githubToken, _ := cmd.Flags().GetString(githubTokenFlag)
query, _ := cmd.Flags().GetString(queryFlag)
minStars, _ := cmd.Flags().GetUint(minStarsFlag)

session := cliui.New(cliui.StartSpinner())
defer session.End()

session.StartSpinner("🔎 Searching for ignite apps on GitHub...")
client := xgithub.NewClient(githubToken)
repos, err := apps.Search(cmd.Context(), client, query, minStars)
if err != nil {
return err
}
session.StopSpinner()

if len(repos) < 1 {
session.Println("❌ No ignite application were found")
return nil
}

printRepoTree(session, repos)

return nil
},
}

c.Flags().StringP(queryFlag, "q", "", "Query string to search for")
c.Flags().Uint(minStarsFlag, 10, "Minimum number of stars to search for")

return c
}

func printRepoTree(sess *cliui.Session, repos []apps.AppRepository) {
for i, repo := range repos {
node := tree.NewNode(fmt.Sprintf(
"📦 %-50s %s %s",
repo.PackageURL,
starsCountStyle.Render(humanizeInt(repo.Stars, "⭐️")),
updatedAtStyle.Render("("+humanize.Time(repo.UpdatedAt)+")"),
))
node.AddChild(nil) // Add a nil child to add a line break.
for _, app := range repo.Apps {
node.AddChild(tree.NewNode(fmt.Sprintf(
"🔥 %-20s %s",
app.Name,
limitTextlength(app.Description, descriptionLimit),
)))
}
sess.Print(node)

if i < len(repos)-1 {
sess.Println()
}
}
}

func humanizeInt(n int, unit string) string {
value, suffix := humanize.ComputeSI(float64(n))
return humanize.FtoaWithDigits(value, 1) + suffix + " " + unit
}

func limitTextlength(text string, limit int) string {
if len(text) > limit {
return text[:limit-3] + "..."
}

return text
}
36 changes: 36 additions & 0 deletions official/marketplace/cmd/marketplace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"github.com/spf13/cobra"
)

const (
githubTokenFlag = "github-token"
)

// NewMarketplace creates a new marketplace command that holds
// some other sub commands related to running marketplace like
// list and info.
func NewMarketplace() *cobra.Command {
c := &cobra.Command{
Use: "marketplace [command]",
Aliases: []string{"mp"},
Short: "Run marketplace commands",
Long: `Marketplace is a command line tool that helps you to search for ignite apps
using GitHub search API. It also helps you to get more information about an app.
Please note that Github API has a very limited rate limit for unauthenticated requests
so it's recommended to use the --github-token flag you want to use marketplace commands frequently.`,
SilenceUsage: true,
SilenceErrors: true,
}

// add sub commands.
c.AddCommand(
NewList(),
NewInfo(),
)

c.PersistentFlags().String(githubTokenFlag, "", "GitHub access token")

return c
}
Loading

0 comments on commit be0006a

Please sign in to comment.