Skip to content

Commit

Permalink
Merge pull request #402 from gauravgahlot/template-list-quiet
Browse files Browse the repository at this point in the history
Only display template IDs
  • Loading branch information
Cbkhare authored Dec 24, 2020
2 parents a04ccb4 + cd54c41 commit 06519be
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions cmd/tink-cli/cmd/template/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ var (
updatedAt = "Updated At"
)

var (
quiet bool
t table.Writer
)

// listCmd represents the list subcommand for template command
var listCmd = &cobra.Command{
Use: "list",
Expand All @@ -34,15 +39,19 @@ var listCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
t := table.NewWriter()
if quiet {
listTemplates()
return
}
t = table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{id, name, createdAt, updatedAt})
listTemplates(cmd, t)
listTemplates()
t.Render()
},
}

func listTemplates(cmd *cobra.Command, t table.Writer) {
func listTemplates() {
list, err := client.TemplateClient.ListTemplates(context.Background(), &template.ListRequest{
FilterBy: &template.ListRequest_Name{
Name: "*",
Expand All @@ -54,19 +63,32 @@ func listTemplates(cmd *cobra.Command, t table.Writer) {

var tmp *template.WorkflowTemplate
for tmp, err = list.Recv(); err == nil && tmp.Name != ""; tmp, err = list.Recv() {
printOutput(t, tmp)
}

if err != nil && err != io.EOF {
log.Fatal(err)
}
}

func printOutput(t table.Writer, tmp *template.WorkflowTemplate) {
if quiet {
fmt.Println(tmp.Id)
} else {
cr := tmp.CreatedAt
up := tmp.UpdatedAt
t.AppendRows([]table.Row{
{tmp.Id, tmp.Name, time.Unix(cr.Seconds, 0), time.Unix(up.Seconds, 0)},
})
}
}

if err != nil && err != io.EOF {
log.Fatal(err)
}
func addListFlags() {
flags := listCmd.Flags()
flags.BoolVarP(&quiet, "quiet", "q", false, "only display template IDs")
}

func init() {
listCmd.DisableFlagsInUseLine = true
addListFlags()
SubCommands = append(SubCommands, listCmd)
}

0 comments on commit 06519be

Please sign in to comment.