github.com/apptainer/singularity@v3.1.1+incompatible/cmd/internal/cli/key_search.go (about)

     1  // Copyright (c) 2017-2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package cli
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/spf13/cobra"
    13  	"github.com/sylabs/singularity/docs"
    14  	"github.com/sylabs/singularity/internal/pkg/sylog"
    15  	"github.com/sylabs/singularity/pkg/sypgp"
    16  )
    17  
    18  func init() {
    19  	KeySearchCmd.Flags().SetInterspersed(false)
    20  
    21  	KeySearchCmd.Flags().StringVarP(&keyServerURL, "url", "u", defaultKeyServer, "specify the key server URL")
    22  	KeySearchCmd.Flags().SetAnnotation("url", "envkey", []string{"URL"})
    23  }
    24  
    25  // KeySearchCmd is `singularity key search' and look for public keys from a key server
    26  var KeySearchCmd = &cobra.Command{
    27  	Args:                  cobra.ExactArgs(1),
    28  	DisableFlagsInUseLine: true,
    29  	PreRun:                sylabsToken,
    30  	Run: func(cmd *cobra.Command, args []string) {
    31  		if err := doKeySearchCmd(args[0], keyServerURL); err != nil {
    32  			sylog.Errorf("search failed: %s", err)
    33  			os.Exit(2)
    34  		}
    35  	},
    36  
    37  	Use:     docs.KeySearchUse,
    38  	Short:   docs.KeySearchShort,
    39  	Long:    docs.KeySearchLong,
    40  	Example: docs.KeySearchExample,
    41  }
    42  
    43  func doKeySearchCmd(search string, url string) error {
    44  	// get keyring with matching search string
    45  	list, err := sypgp.SearchPubkey(search, url, authToken)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	fmt.Println(list)
    51  
    52  	return nil
    53  }