github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/list/list.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package list
    10  
    11  import (
    12  	"fmt"
    13  
    14  	"github.com/vchain-us/vcn/pkg/cmd/internal/cli"
    15  
    16  	"github.com/spf13/cobra"
    17  	"github.com/vchain-us/vcn/internal/assert"
    18  	"github.com/vchain-us/vcn/pkg/api"
    19  	"github.com/vchain-us/vcn/pkg/store"
    20  )
    21  
    22  // NewCommand returns the cobra command for `vcn list`
    23  func NewCommand() *cobra.Command {
    24  	cmd := &cobra.Command{
    25  		Use:     "list",
    26  		Aliases: []string{"l"},
    27  		Short:   "List your notarized assets",
    28  		Long:    ``,
    29  		RunE:    runList,
    30  		Args:    cobra.NoArgs,
    31  	}
    32  
    33  	cmd.Flags().UintP("page", "p", 0, "page number")
    34  
    35  	return cmd
    36  }
    37  
    38  const (
    39  	listFooter = `
    40  Shown %d-%d of %d assets (current page %d)
    41  
    42  To list next page, run:
    43  vcn list --page %d
    44  `
    45  )
    46  
    47  func runList(cmd *cobra.Command, args []string) error {
    48  	output, err := cmd.Flags().GetString("output")
    49  	if err != nil {
    50  		return err
    51  	}
    52  	page, err := cmd.Flags().GetUint("page")
    53  	if err != nil {
    54  		return err
    55  	}
    56  	cmd.SilenceUsage = true
    57  
    58  	if store.Config().CurrentContext.LcHost != "" {
    59  		fmt.Printf("Not supported with Codenotary Cloud credentials\n")
    60  		return nil
    61  	}
    62  
    63  	if err := assert.UserLogin(); err != nil {
    64  		return err
    65  	}
    66  	u := api.NewUser(store.Config().CurrentContext.Email)
    67  
    68  	artifacts, err := u.ListArtifacts(page)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	if output == "" {
    73  		fmt.Printf("Listing assets for %s...\n\n", u.Email())
    74  	}
    75  	if err = cli.PrintList(output, artifacts.Content); err != nil {
    76  		return err
    77  	}
    78  	if output == "" {
    79  		if l := uint64(len(artifacts.Content)); l > 0 {
    80  			offset := artifacts.Pageable.PageSize * artifacts.Pageable.PageNumber
    81  			fmt.Printf(
    82  				"%s's assets: %d-%d of %d (current page %d)\n\n",
    83  				u.Email(),
    84  				1+offset,
    85  				uint64(len(artifacts.Content))+offset,
    86  				artifacts.TotalElements,
    87  				artifacts.Pageable.PageNumber,
    88  			)
    89  
    90  			if (offset + artifacts.Pageable.PageSize) <= artifacts.TotalElements {
    91  				fmt.Printf(
    92  					"To list next page, run:\nvcn list --page %d\n\n",
    93  					artifacts.Pageable.PageNumber+1,
    94  				)
    95  			}
    96  		} else {
    97  			fmt.Printf("No results.\n\n")
    98  		}
    99  	}
   100  	return nil
   101  }