-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add marketplace app #32
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c6ad3c
add marketplace
Ehsan-saradar a6f415b
Update go.work
Ehsan-saradar 179827e
Rename go.work to go.work.example
Ehsan-saradar d729fe0
Use AppsConfig from ignite cli
Ehsan-saradar decd6d1
Add test for tree view
Ehsan-saradar b481aba
Remove tools.go
Ehsan-saradar a46b69b
Refactor test cases
Ehsan-saradar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
.vscode | ||
*.test | ||
*.ign | ||
go.work | ||
go.work.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ use ( | |
./explorer | ||
./hermes | ||
./integration | ||
./official/marketplace | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems is not necessary for the apps repo to have a
go.work
, so it was renamed togo.work.example
to make the workspace optional to users