github.com/tommi2day/pwcli@v0.0.0-20240317203041-4d1177a5ab91/cmd/list.go (about)

     1  // Package cmd commands
     2  package cmd
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/tommi2day/gomodules/common"
     9  	"github.com/tommi2day/gomodules/pwlib"
    10  
    11  	log "github.com/sirupsen/logrus"
    12  
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // listCmd represents the list command
    17  var listCmd = &cobra.Command{
    18  	Use:          "list",
    19  	Short:        "list passwords",
    20  	Long:         `List all available password records`,
    21  	SilenceUsage: true,
    22  	RunE:         listpass,
    23  }
    24  
    25  func listpass(cmd *cobra.Command, _ []string) error {
    26  	log.Debug("list called")
    27  	kp, _ := cmd.Flags().GetString("keypass")
    28  	if kp != "" {
    29  		pc.KeyPass = kp
    30  		log.Debugf("use alternate password: %s", kp)
    31  	}
    32  	if method == typeKMS {
    33  		if kmsKeyID == "" {
    34  			kmsKeyID = common.GetStringEnv("KMS_KEYID", "")
    35  			log.Debugf("KMS KeyID from environment: '%s'", kmsKeyID)
    36  		}
    37  		if kmsKeyID == "" {
    38  			return fmt.Errorf("need parameter kms_keyid to proceed")
    39  		}
    40  		if kmsEndpoint != "" {
    41  			log.Debugf("use KMS endpoint %s", kmsEndpoint)
    42  			_ = os.Setenv("KMS_ENDPOINT", kmsEndpoint)
    43  		}
    44  		log.Debugf("use KMS method with keyid %s", kmsKeyID)
    45  		pc.KMSKeyID = kmsKeyID
    46  	}
    47  	pwlib.SilentCheck = false
    48  	lines, err := pc.ListPasswords()
    49  	if err == nil {
    50  		for _, l := range lines {
    51  			fmt.Println(l)
    52  		}
    53  		log.Infof("List returned %d lines", len(lines))
    54  	}
    55  	return err
    56  }
    57  
    58  func init() {
    59  	RootCmd.AddCommand(listCmd)
    60  	// don't have variables populated here
    61  	listCmd.Flags().StringP("keypass", "p", "", "dedicated password for the private key")
    62  	listCmd.Flags().StringVar(&kmsKeyID, "kms_keyid", kmsKeyID, "KMS KeyID")
    63  	listCmd.Flags().StringVar(&kmsEndpoint, "kms_endpoint", kmsEndpoint, "KMS Endpoint Url")
    64  }