...

Source file src/github.com/sassoftware/relic/cmdline/token/token.go

Documentation: github.com/sassoftware/relic/cmdline/token

     1  //
     2  // Copyright (c) SAS Institute Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package token
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"os"
    23  	"sort"
    24  	"strings"
    25  
    26  	"github.com/sassoftware/relic/cmdline/shared"
    27  	"github.com/sassoftware/relic/config"
    28  	"github.com/sassoftware/relic/token"
    29  	"github.com/sassoftware/relic/token/open"
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var TokenCmd = &cobra.Command{
    34  	Use:   "token",
    35  	Short: "View and manipulate token objects",
    36  }
    37  
    38  var TokensCmd = &cobra.Command{
    39  	Use:   "list",
    40  	Short: "List tokens provided by a driver",
    41  	RunE:  tokensCmd,
    42  }
    43  
    44  var ContentsCmd = &cobra.Command{
    45  	Use:   "contents",
    46  	Short: "List keys in a token",
    47  	RunE:  contentsCmd,
    48  }
    49  
    50  var (
    51  	argType     string
    52  	argProvider string
    53  	argId       string
    54  	argValues   bool
    55  )
    56  
    57  func init() {
    58  	shared.RootCmd.AddCommand(TokenCmd)
    59  	TokenCmd.PersistentFlags().StringVarP(&argToken, "token", "t", "", "Name of token")
    60  	TokenCmd.PersistentFlags().StringVar(&argProvider, "provider", "", "Provider module path")
    61  
    62  	TokenCmd.AddCommand(TokensCmd)
    63  
    64  	TokenCmd.AddCommand(ContentsCmd)
    65  	ContentsCmd.Flags().StringVarP(&argLabel, "label", "l", "", "Display objects with this label only")
    66  	ContentsCmd.Flags().StringVarP(&argId, "id", "i", "", "Display objects with this ID only")
    67  	ContentsCmd.Flags().BoolVarP(&argValues, "values", "v", false, "Show contents of objects")
    68  
    69  	shared.AddLateHook(addProviderTypeHelp) // deferred so token providers can init()
    70  }
    71  
    72  func addProviderTypeHelp() {
    73  	var listable []string
    74  	for ptype := range token.Listers {
    75  		listable = append(listable, ptype)
    76  	}
    77  	sort.Strings(listable)
    78  	TokenCmd.PersistentFlags().StringVar(&argType, "type", "", fmt.Sprintf("Provider type (%s)", strings.Join(listable, ", ")))
    79  }
    80  
    81  func tokensCmd(cmd *cobra.Command, args []string) error {
    82  	if argToken == "" && (argType == "" || argProvider == "") {
    83  		return errors.New("--token, or --type and --provider, are required")
    84  	}
    85  	if err := shared.InitConfig(); err != nil {
    86  		return err
    87  	}
    88  	if argToken != "" {
    89  		tokenConf, err := shared.CurrentConfig.GetToken(argToken)
    90  		if err != nil {
    91  			return err
    92  		}
    93  		if argType == "" {
    94  			argType = tokenConf.Type
    95  		}
    96  		if argProvider == "" {
    97  			argProvider = tokenConf.Provider
    98  		}
    99  	}
   100  	return shared.Fail(open.List(argType, argProvider, os.Stdout))
   101  }
   102  
   103  func contentsCmd(cmd *cobra.Command, args []string) error {
   104  	if argToken == "" && (argType == "" || argProvider == "") {
   105  		return errors.New("--token, or --type and --provider, are required")
   106  	}
   107  	if err := shared.InitConfig(); err != nil {
   108  		return err
   109  	}
   110  	var tokenConf *config.TokenConfig
   111  	if argToken != "" {
   112  		var err error
   113  		tokenConf, err = shared.CurrentConfig.GetToken(argToken)
   114  		if err != nil {
   115  			return err
   116  		}
   117  	} else {
   118  		argToken = ":new-token:"
   119  		tokenConf = shared.CurrentConfig.NewToken(argToken)
   120  	}
   121  	if argType != "" {
   122  		tokenConf.Type = argType
   123  	}
   124  	if argProvider != "" {
   125  		tokenConf.Provider = argProvider
   126  	}
   127  	tok, err := openToken(argToken)
   128  	if err != nil {
   129  		return err
   130  	}
   131  	return shared.Fail(tok.ListKeys(token.ListOptions{
   132  		Output: os.Stdout,
   133  		Label:  argLabel,
   134  		ID:     argId,
   135  		Values: argValues,
   136  	}))
   137  }
   138  

View as plain text