package app import ( "context" "time" c "cnb.cool/cnb/go-cnb/cnb" "cnb.cool/looc/git-cnb/cnb" "cnb.cool/looc/git-cnb/ui" "github.com/spf13/cobra" ) var ReleasesCmd = &cobra.Command{ Use: "release", Short: "print release of this repo", Long: "print release of this repo", Run: func(cmd *cobra.Command, args []string) { ListReleases(cmd.Context()) }, } func ListReleases(ctx context.Context) { releases, err := cnb.ListReleases(ctx, Client, Repo) if err != nil { ui.ErrorWithExit(err.Error()) } table := &ui.Table{ Titles: []ui.TableTitle{}, Items: [][]string{}, } table.Titles = []ui.TableTitle{ {Title: "TITLE", Width: 15}, {Title: "TAG NAME", Width: 15}, {Title: "TYPE", Width: 15}, {Title: "PUBLISHED", Width: 30}, } addReleasesToTable(table, releases) table.Print() } func addReleasesToTable(table *ui.Table, releases []c.Release) { for _, release := range releases { pre := "" if release.Prerelease { pre = "预发布" } table.Items = append(table.Items, []string{ release.Name, release.TagName, pre, parsePublishedAt(release.PublishedAt)}) } } func parsePublishedAt(publishedAt string) string { t, err := time.Parse(time.RFC3339, publishedAt) if err != nil { return "" } formattedTime := t.Format("2006-01-02 15:04:05") return formattedTime }