func SetupClientConfig()

in commands/commands.go [39:92]


func SetupClientConfig(cmd *cobra.Command, args []string) error {
	baseURL, err := whisk.GetURLBase(Properties.APIHost, DefaultOpenWhiskApiPath)

	// Determine if the parent command will require the API host to be set
	apiHostRequired := (cmd.Parent().Name() == "property" && cmd.Name() == "get" && (Flags.property.auth ||
		Flags.property.cert || Flags.property.key || Flags.property.apihost ||
		Flags.property.apiversion || Flags.property.cliversion)) ||
		(cmd.Parent().Name() == "property" && cmd.Name() == "set" && (len(Flags.property.apihostSet) > 0 ||
			len(Flags.property.apiversionSet) > 0 || len(Flags.Global.Auth) > 0)) ||
		(cmd.Parent().Name() == "sdk" && cmd.Name() == "install" && len(args) > 0 && args[0] == "bashauto")

	// Display an error if the parent command requires an API host to be set, and the current API host is not valid
	if err != nil && !apiHostRequired {
		whisk.Debug(whisk.DbgError, "whisk.GetURLBase(%s, %s) error: %s\n", Properties.APIHost, DefaultOpenWhiskApiPath, err)
		errMsg := wski18n.T("The API host is not valid: {{.err}}", map[string]interface{}{"err": err})
		whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_GENERAL,
			whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
		return whiskErr
	}

	clientConfig := &whisk.Config{
		Cert:              Properties.Cert,
		Key:               Properties.Key,
		AuthToken:         Properties.Auth,
		Namespace:         Properties.Namespace,
		BaseURL:           baseURL,
		Version:           Properties.APIVersion,
		Insecure:          Flags.Global.Insecure,
		Host:              Properties.APIHost,
		UserAgent:         UserAgent + "/1.0 (" + Properties.CLIVersion + ") " + runtime.GOOS + " " + runtime.GOARCH,
		AdditionalHeaders: AdditionalHeaders,
	}

	if len(clientConfig.Host) == 0 {
		config, _ := whisk.GetDefaultConfig()
		clientConfig.Host = config.Host
		if len(clientConfig.Host) == 0 {
			clientConfig.Host = "openwhisk.ng.bluemix.net"
		}
	}

	// Setup client
	Client, err = whisk.NewClient(http.DefaultClient, clientConfig)

	if err != nil {
		whisk.Debug(whisk.DbgError, "whisk.NewClient(%#v, %#v) error: %s\n", http.DefaultClient, clientConfig, err)
		errMsg := wski18n.T("Unable to initialize server connection: {{.err}}", map[string]interface{}{"err": err})
		whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_GENERAL,
			whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
		return whiskErr
	}

	return nil
}