add --with-timestamps option to search repo cmd

Signed-off-by: Kazuhiro Suzuki <ksauzzmsg@gmail.com>
pull/13421/head
Kazuhiro Suzuki 11 months ago
parent 769d083913
commit ef94826e76

@ -73,6 +73,7 @@ type searchRepoOptions struct {
repoCacheDir string repoCacheDir string
outputFormat output.Format outputFormat output.Format
failOnNoResult bool failOnNoResult bool
withTimeStamps bool
} }
func newSearchRepoCmd(out io.Writer) *cobra.Command { func newSearchRepoCmd(out io.Writer) *cobra.Command {
@ -96,6 +97,7 @@ func newSearchRepoCmd(out io.Writer) *cobra.Command {
f.StringVar(&o.version, "version", "", "search using semantic versioning constraints on repositories you have added") f.StringVar(&o.version, "version", "", "search using semantic versioning constraints on repositories you have added")
f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table")
f.BoolVar(&o.failOnNoResult, "fail-on-no-result", false, "search fails if no results are found") f.BoolVar(&o.failOnNoResult, "fail-on-no-result", false, "search fails if no results are found")
f.BoolVar(&o.withTimeStamps, "with-timestamps", false, "print created timestamps on output table")
bindOutputFlag(cmd, &o.outputFormat) bindOutputFlag(cmd, &o.outputFormat)
@ -127,7 +129,7 @@ func (o *searchRepoOptions) run(out io.Writer, args []string) error {
return err return err
} }
return o.outputFormat.Write(out, &repoSearchWriter{data, o.maxColWidth, o.failOnNoResult}) return o.outputFormat.Write(out, &repoSearchWriter{data, o.maxColWidth, o.failOnNoResult, o.withTimeStamps})
} }
func (o *searchRepoOptions) setupSearchedVersion() { func (o *searchRepoOptions) setupSearchedVersion() {
@ -205,12 +207,14 @@ type repoChartElement struct {
Version string `json:"version"` Version string `json:"version"`
AppVersion string `json:"app_version"` AppVersion string `json:"app_version"`
Description string `json:"description"` Description string `json:"description"`
Created string `json:"created"`
} }
type repoSearchWriter struct { type repoSearchWriter struct {
results []*search.Result results []*search.Result
columnWidth uint columnWidth uint
failOnNoResult bool failOnNoResult bool
withTimestamps bool
} }
func (r *repoSearchWriter) WriteTable(out io.Writer) error { func (r *repoSearchWriter) WriteTable(out io.Writer) error {
@ -228,9 +232,17 @@ func (r *repoSearchWriter) WriteTable(out io.Writer) error {
} }
table := uitable.New() table := uitable.New()
table.MaxColWidth = r.columnWidth table.MaxColWidth = r.columnWidth
table.AddRow("NAME", "CHART VERSION", "APP VERSION", "DESCRIPTION", "CREATED") if r.withTimestamps {
for _, r := range r.results { table.AddRow("NAME", "CHART VERSION", "APP VERSION", "DESCRIPTION", "CREATED")
table.AddRow(r.Name, r.Chart.Version, r.Chart.AppVersion, r.Chart.Description, r.Chart.Created.Format(time.RFC3339)) } else {
table.AddRow("NAME", "CHART VERSION", "APP VERSION", "DESCRIPTION")
}
for _, ret := range r.results {
if r.withTimestamps {
table.AddRow(ret.Name, ret.Chart.Version, ret.Chart.AppVersion, ret.Chart.Description, ret.Chart.Created.Format(time.RFC3339))
} else {
table.AddRow(ret.Name, ret.Chart.Version, ret.Chart.AppVersion, ret.Chart.Description)
}
} }
return output.EncodeTable(out, table) return output.EncodeTable(out, table)
} }
@ -253,7 +265,7 @@ func (r *repoSearchWriter) encodeByFormat(out io.Writer, format output.Format) e
chartList := make([]repoChartElement, 0, len(r.results)) chartList := make([]repoChartElement, 0, len(r.results))
for _, r := range r.results { for _, r := range r.results {
chartList = append(chartList, repoChartElement{r.Name, r.Chart.Version, r.Chart.AppVersion, r.Chart.Description}) chartList = append(chartList, repoChartElement{r.Name, r.Chart.Version, r.Chart.AppVersion, r.Chart.Description, r.Chart.Created.Format(time.RFC3339)})
} }
switch format { switch format {

@ -86,6 +86,10 @@ func TestSearchRepositoriesCmd(t *testing.T) {
name: "search for 'alpine', expect valid yaml output", name: "search for 'alpine', expect valid yaml output",
cmd: "search repo alpine --output yaml", cmd: "search repo alpine --output yaml",
golden: "output/search-output-yaml.txt", golden: "output/search-output-yaml.txt",
}, {
name: "search for 'alpine' with timestamps, expect valid table output with timestamps",
cmd: "search repo alpine --with-timestamps",
golden: "output/search-output-timestamps.txt",
}} }}
settings.Debug = true settings.Debug = true

@ -1 +1 @@
[{"name":"testing/mariadb","version":"0.3.0","app_version":"","description":"Chart for MariaDB"}] [{"name":"testing/mariadb","version":"0.3.0","app_version":"","description":"Chart for MariaDB","created":"2018-04-23T08:20:27Z"}]

@ -0,0 +1,2 @@
NAME CHART VERSION APP VERSION DESCRIPTION CREATED
testing/alpine 0.2.0 2.3.4 Deploy a basic Alpine Linux pod 2018-07-09T11:34:37Z

@ -1,4 +1,5 @@
- app_version: 2.3.4 - app_version: 2.3.4
created: "2018-07-09T11:34:37Z"
description: Deploy a basic Alpine Linux pod description: Deploy a basic Alpine Linux pod
name: testing/alpine name: testing/alpine
version: 0.2.0 version: 0.2.0

Loading…
Cancel
Save