github.com/Axway/agent-sdk@v1.1.101/samples/watchclient/pkg/cmd/cmd.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/Axway/agent-sdk/samples/watchclient/pkg/client"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/pflag"
    11  	"github.com/spf13/viper"
    12  )
    13  
    14  var cfg = &client.Config{}
    15  
    16  // NewRootCmd creates a new cobra.Command
    17  func NewRootCmd() *cobra.Command {
    18  	cmd := &cobra.Command{
    19  		Use:     "server",
    20  		Short:   "The Event Server for Control Plane",
    21  		Version: "0.0.1",
    22  		RunE:    run,
    23  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    24  			return initViperConfig(cmd)
    25  		},
    26  	}
    27  
    28  	initFlags(cmd)
    29  
    30  	return cmd
    31  }
    32  
    33  func initFlags(cmd *cobra.Command) {
    34  	cmd.Flags().String("tenant_id", "", "The org ID for watching the resource")
    35  	cmd.Flags().String("host", "127.0.0.1", "The Service GRPC Host to connect")
    36  	cmd.Flags().Uint32("port", 8000, "The Service GRPC port to connect")
    37  	cmd.MarkFlagRequired("tenant_id")
    38  
    39  	cmd.Flags().String("topic_self_link", "", "The self link of the WatchTopic")
    40  	cmd.MarkFlagRequired("topic_self_link")
    41  
    42  	cmd.Flags().String("auth.private_key", "./private_key.pem", "The private key associated with service account(default : ./private_key.pem)")
    43  	cmd.Flags().String("auth.public_key", "./public_key.pem", "The public key associated with service account(default : ./public_key.pem)")
    44  	cmd.Flags().String("auth.key_password", "", "The password for private key")
    45  	cmd.Flags().String("auth.url", "https://login.axwaytest.net/auth", "The AxwayID auth URL")
    46  	cmd.Flags().String("auth.client_id", "", "The service account client ID")
    47  	cmd.Flags().Duration("auth.timeout", 10*time.Second, "The connection timeout for AxwayID")
    48  	cmd.MarkFlagRequired("auth.client_id")
    49  
    50  	cmd.Flags().Bool("insecure", false, "Do not verify the server cert on TLS connection")
    51  	cmd.Flags().Bool("use_harvester", true, "Use harvester to sync events")
    52  	cmd.Flags().String("harvester_host", "", "The Harvester Host")
    53  	cmd.Flags().Uint32("harvester_port", 0, "The Harvester port")
    54  	cmd.Flags().String("log_level", "info", "log level")
    55  	cmd.Flags().String("log_format", "json", "line or json")
    56  }
    57  
    58  func initViperConfig(cmd *cobra.Command) error {
    59  	v := viper.New()
    60  	// All env vars must start with WC.
    61  	v.SetEnvPrefix("wc")
    62  	// Allows viper to tie a runtime flag, such as --auth.client_id to an env variable, AUTH_CLIENT_ID, and allows
    63  	// viper to unmarshal values into nested structs. --auth.client_id -> config.auth.clientID
    64  	v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
    65  	v.AutomaticEnv()
    66  	bindFlagsToViperConfig(cmd, v)
    67  
    68  	err := v.Unmarshal(cfg)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  // bindFlagsToViperConfig - For each flag, look up its corresponding env var, and use the env var if the flag is not set.
    77  func bindFlagsToViperConfig(cmd *cobra.Command, v *viper.Viper) {
    78  	cmd.Flags().VisitAll(func(f *pflag.Flag) {
    79  		name := strings.ToUpper(f.Name)
    80  		// Binds an environment variable to a viper arg. Ex: --tenant_id == TENANT_ID
    81  		if err := v.BindPFlag(name, f); err != nil {
    82  			panic(err)
    83  		}
    84  
    85  		// Apply the viper config value to the flag when the flag is not set and viper has a value
    86  		if !f.Changed && v.IsSet(f.Name) {
    87  			val := v.Get(f.Name)
    88  			err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
    89  			if err != nil {
    90  				panic(err)
    91  			}
    92  		}
    93  	})
    94  }
    95  
    96  func run(_ *cobra.Command, _ []string) error {
    97  	logger, err := getLogger(cfg.Level, cfg.Format)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	wc, err := client.NewWatchClient(cfg, logger)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	wc.Watch()
   107  	return nil
   108  }