func setNewParamPop(name string, paramst *Stack) (int, string) { newval, err_code, err_str := paramst.Top() if err_code != 0 { return err_code, err_str } var nval string = "" if newval.Type() == value.STRING { nval = newval.Actual().(string) } else { nval = ValToStr(newval) } if name == "creds" { // Define credentials as user/pass and convert into // JSON object credentials var creds Credentials creds_ret, err_code, err_str := ToCreds(nval) if err_code != 0 { return err_code, err_str } for _, v := range creds_ret { creds = append(creds, v) } ac, err := json.Marshal(creds) if err != nil { return errors.JSON_MARSHAL, "" } nval = string(ac) } go_n1ql.SetQueryParams(name, nval) return 0, "" }
/* Push value from the Top of the stack onto the parameter stack. This is used by the \PUSH command with no arguments. */ func Pushparam_Helper(param map[string]*Stack, isrestp bool, isnamep bool) (int, string) { for name, v := range param { t, err_code, err_string := v.Top() if err_code != 0 { return err_code, err_string } v.Push(t) // When passing the query rest api parameter to go_n1ql // we need to convert to string only if the value isnt // already a string if isrestp == true { var val string = "" if t.Type() == value.STRING { val = t.Actual().(string) } else { val = ValToStr(t) } if isnamep == true { name = "$" + name } else { //We know it is a query credential if name == "creds" { // Define credentials as user/pass and convert into // JSON object credentials var creds Credentials creds_ret, err_code, err_str := ToCreds(val) if err_code != 0 { return err_code, err_str } for _, v := range creds_ret { creds = append(creds, v) } ac, err := json.Marshal(creds) if err != nil { return errors.JSON_MARSHAL, "" } val = string(ac) } } go_n1ql.SetQueryParams(name, val) } } return 0, "" }
func main() { flag.Parse() if outputFlag != "" { // Redirect all output to the given file. // This is handled in the HandleInteractiveMode() method // in interactive.go. command.FILE_RW_MODE = true command.FILE_OUTPUT = outputFlag } // Set command.W = os.Stdout command.SetWriter(os.Stdout) /* Handle options and what they should do */ // TODO : Readd ... //Taken out so as to connect to both cluster and query service //using go_n1ql. /* if strings.HasPrefix(ServerFlag, "http://") == false { ServerFlag = "http://" + ServerFlag } urlRegex := "^(https?://)[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" match, _ := regexp.MatchString(urlRegex, ServerFlag) if match == false { //TODO Isha : Add error code. Throw invalid url error fmt.Println("Invalid url please check" + ServerFlag) } //-engine if strings.HasSuffix(ServerFlag, "/") == false { ServerFlag = ServerFlag + "/" } */ /* -version : Display the version of the shell and then exit. */ if versionFlag == true { dummy := []string{} cmd := command.Version{} cmd.ExecCommand(dummy) os.Exit(0) } /* Check for input url argument */ args := flag.Args() if len(args) > 1 { s_err := command.HandleError(errors.CMD_LINE_ARG, "") command.PrintError(s_err) os.Exit(1) } else { if len(args) == 1 { urlRegex := "^(https?://)[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" match, _ := regexp.MatchString(urlRegex, args[0]) if match == false { s_err := command.HandleError(errors.INVALID_URL, args[0]) command.PrintError(s_err) os.Exit(1) } else { ServerFlag = args[0] } } } /* -quiet : Display Message only if flag not specified */ if !quietFlag && NoQueryService == false { s := fmt.Sprintln("Input connection parameter is " + ServerFlag + ". Type Ctrl-D to exit.\n") _, werr := io.WriteString(command.W, s) if werr != nil { s_err := command.HandleError(errors.WRITER_OUTPUT, werr.Error()) command.PrintError(s_err) } } /* -user : Accept Admin credentials. Prompt for password and set the n1ql_creds. Append to creds so that user can also define bucket credentials using -credentials if they need to. */ var creds command.Credentials var err error var password []byte if userFlag != "" { //Check if there is a -password option. if pwdFlag != "" { password = []byte(pwdFlag) err = nil } else { // If no -p option then prompt for the password s := fmt.Sprintln("Enter Password: "******"" { s_err := command.HandleError(errors.INVALID_PASSWORD, "") command.PrintError(s_err) os.Exit(1) } else { creds = append(creds, command.Credential{"user": userFlag, "pass": string(password)}) } } else { s_err := command.HandleError(errors.INVALID_PASSWORD, err.Error()) command.PrintError(s_err) os.Exit(1) } } else { // If the -u option isnt specified and the -p option is specified // then Invalid Username error. if pwdFlag != "" { s_err := command.HandleError(errors.INVALID_USERNAME, "") command.PrintError(s_err) os.Exit(1) } } /* -credentials : Accept credentials to pass to the n1ql endpoint. Ensure that the user inputs credentials in the form a:b. It is important to apend these credentials to those given by -user. */ if userFlag == "" && credsFlag == "" { // No credentials exist. This can still be used to connect to // un-authenticated servers. // Dont output the statement if we are running in single command // mode. if scriptFlag == "" { _, werr := io.WriteString(command.W, "No Input Credentials. In order to connect to a server with authentication, please provide credentials.\n") if werr != nil { s_err := command.HandleError(errors.WRITER_OUTPUT, werr.Error()) command.PrintError(s_err) } } } else if credsFlag != "" { creds_ret, err_code, err_string := command.ToCreds(credsFlag) if err_code != 0 { s_err := command.HandleError(err_code, err_string) command.PrintError(s_err) } for _, v := range creds_ret { creds = append(creds, v) } } //Append empty credentials. This is used for cases where one of the buckets //is a SASL bucket, and we need to access the other unprotected buckets. //CBauth works this way. //if credsFlag == "" && userFlag != "" { creds = append(creds, command.Credential{"user": "", "pass": ""}) //} /* Add the credentials set by -user and -credentials to the go_n1ql creds parameter. */ if creds != nil { ac, err := json.Marshal(creds) if err != nil { //Error while Marshalling s_err := command.HandleError(errors.JSON_MARSHAL, err.Error()) command.PrintError(s_err) os.Exit(1) } go_n1ql.SetQueryParams("creds", string(ac)) } if timeoutFlag != "0ms" { go_n1ql.SetQueryParams("timeout", timeoutFlag) } // Handle the inputFlag and ScriptFlag options in HandleInteractiveMode. // This is so as to add these to the history. go_n1ql.SetPassthroughMode(true) HandleInteractiveMode(filepath.Base(os.Args[0])) }
func PushOrSet(args []string, pushvalue bool) (int, string) { // Check what kind of parameter needs to be set or pushed // depending on the pushvalue boolean value. if strings.HasPrefix(args[0], "-$") { // For Named Parameters vble := args[0] vble = vble[2:] args_str := strings.Join(args[1:], " ") err_code, err_str := PushValue_Helper(pushvalue, NamedParam, vble, args_str) if err_code != 0 { return err_code, err_str } //Pass the named parameters to the rest api using the SetQueryParams method v, err_code, err_str := NamedParam[vble].Top() if err_code != 0 { return err_code, err_str } val := ValToStr(v) vble = "$" + vble go_n1ql.SetQueryParams(vble, val) } else if strings.HasPrefix(args[0], "-") { // For query parameters vble := args[0] vble = vble[1:] vble = strings.ToLower(vble) args_str := strings.Join(args[1:], " ") err_code, err_str := PushValue_Helper(pushvalue, QueryParam, vble, args_str) if err_code != 0 { return err_code, err_str } if vble == "creds" { // Define credentials as user/pass and convert into // JSON object credentials var creds Credentials args_str := strings.Join(args[1:], " ") creds_ret, err_code, err_str := ToCreds(args_str) if err_code != 0 { return err_code, err_str } for _, v := range creds_ret { creds = append(creds, v) } ac, err := json.Marshal(creds) if err != nil { return errors.JSON_MARSHAL, "" } go_n1ql.SetQueryParams("creds", string(ac)) } else { v, err_code, err_str := QueryParam[vble].Top() if err_code != 0 { return err_code, err_str } // When passing the query rest api parameter to go_n1ql // we need to convert to string only if the value isnt // already a string var val string = "" if v.Type() == value.STRING { val = v.Actual().(string) } else { val = ValToStr(v) } go_n1ql.SetQueryParams(vble, val) } } else if strings.HasPrefix(args[0], "$") { // For User defined session variables vble := args[0] vble = vble[1:] args_str := strings.Join(args[1:], " ") err_code, err_str := PushValue_Helper(pushvalue, UserDefSV, vble, args_str) if err_code != 0 { return err_code, err_str } } else { // For Predefined session variables vble := args[0] args_str := strings.Join(args[1:], " ") err_code, err_str := PushValue_Helper(pushvalue, PreDefSV, vble, args_str) if vble == "histfile" { HISTFILE = args[1] } if err_code != 0 { return err_code, err_str } } return 0, "" }