cmd/config.go
changeset 0 5abace724584
child 10 e4c490882126
equal deleted inserted replaced
-1:000000000000 0:5abace724584
       
     1 // Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
       
     2 //
       
     3 // Licensed under the MIT license.
       
     4 // Please see the LICENSE file is this directory.
       
     5 
       
     6 package cmd
       
     7 
       
     8 import (
       
     9 	"github.com/spf13/cobra"
       
    10 	"github.com/spf13/viper"
       
    11 
       
    12 	"github.com/McKael/madonctl/printer"
       
    13 )
       
    14 
       
    15 var configCmd = &cobra.Command{
       
    16 	Use:   "config",
       
    17 	Short: "Display configuration",
       
    18 }
       
    19 
       
    20 func init() {
       
    21 	RootCmd.AddCommand(configCmd)
       
    22 
       
    23 	// Subcommands
       
    24 	configCmd.AddCommand(configSubcommands...)
       
    25 }
       
    26 
       
    27 var configSubcommands = []*cobra.Command{
       
    28 	&cobra.Command{
       
    29 		Use:   "dump",
       
    30 		Short: "Dump the configuration",
       
    31 		RunE: func(cmd *cobra.Command, args []string) error {
       
    32 			return configDump()
       
    33 		},
       
    34 	},
       
    35 	&cobra.Command{
       
    36 		Use:     "whoami",
       
    37 		Aliases: []string{"token"},
       
    38 		Short:   "Display user token",
       
    39 		RunE: func(cmd *cobra.Command, args []string) error {
       
    40 			return configDisplayToken()
       
    41 		},
       
    42 	},
       
    43 }
       
    44 
       
    45 const configurationTemplate = `---
       
    46 instance: '{{.InstanceURL}}'
       
    47 app_id: '{{.ID}}'
       
    48 app_secret: '{{.Secret}}'
       
    49 
       
    50 {{if .UserToken}}token: {{.UserToken.access_token}}{{else}}#token: ''{{end}}
       
    51 #login: ''
       
    52 #password: ''
       
    53 safe_mode: true
       
    54 ...
       
    55 `
       
    56 
       
    57 func configDump() error {
       
    58 	if viper.GetBool("safe_mode") {
       
    59 		errPrint("Cannot dump: disabled by configuration (safe_mode)")
       
    60 		return nil
       
    61 	}
       
    62 
       
    63 	if err := madonInitClient(); err != nil {
       
    64 		return err
       
    65 	}
       
    66 	// Try to sign in, but don't mind if it fails
       
    67 	if err := madonLogin(); err != nil {
       
    68 		errPrint("Info: could not log in: %s", err)
       
    69 	}
       
    70 
       
    71 	var p printer.ResourcePrinter
       
    72 	var err error
       
    73 
       
    74 	if getOutputFormat() == "plain" {
       
    75 		cfile := viper.ConfigFileUsed()
       
    76 		if cfile == "" {
       
    77 			cfile = defaultConfigFile
       
    78 		}
       
    79 		errPrint("You can copy the following lines into a configuration file.")
       
    80 		errPrint("E.g. %s -i INSTANCE -L USERNAME -P PASS config dump > %s\n", AppName, cfile)
       
    81 		p, err = printer.NewPrinterTemplate(configurationTemplate)
       
    82 	} else {
       
    83 		p, err = getPrinter()
       
    84 	}
       
    85 	if err != nil {
       
    86 		return err
       
    87 	}
       
    88 	return p.PrintObj(gClient, nil, "")
       
    89 }
       
    90 
       
    91 func configDisplayToken() error {
       
    92 	if viper.GetBool("safe_mode") {
       
    93 		errPrint("Cannot dump: disabled by configuration (safe_mode)")
       
    94 		return nil
       
    95 	}
       
    96 
       
    97 	if err := madonInit(true); err != nil {
       
    98 		return err
       
    99 	}
       
   100 
       
   101 	p, err := getPrinter()
       
   102 	if err != nil {
       
   103 		return err
       
   104 	}
       
   105 	return p.PrintObj(gClient.UserToken, nil, "")
       
   106 }