forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
97588: cli: warn the user for common mistakes around `options=-ccluster` r=rafiss a=knz Fixes cockroachdb#97586. Epic: CRDB-23559 Examples: ``` % ./cockroach sql --url='postgres://localhost:26257/?-ccluster=foo' warning: found raw URL parameter "-ccluster"; are you sure you did not mean to use "options=-ccluster" instead? % ./cockroach sql --url='postgres://localhost:26257/?options=-cluster=foo' warning: found "-cluster=" in URL "options" field; are you sure you did not mean to use "options=-ccluster" instead? ``` Release note: None Co-authored-by: Raphael 'kena' Poss <[email protected]>
- Loading branch information
Showing
9 changed files
with
235 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgurl | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// ParseExtendedOptions is a parser for the value part of the special | ||
// option "options". | ||
// | ||
// The options must be separated by space and have one of the | ||
// following patterns: '-c key=value', '-ckey=value', '--key=value' | ||
func ParseExtendedOptions(optionsString string) (res url.Values, err error) { | ||
res = url.Values{} | ||
lastWasDashC := false | ||
opts := splitOptions(optionsString) | ||
|
||
for i := 0; i < len(opts); i++ { | ||
prefix := "" | ||
if len(opts[i]) > 1 { | ||
prefix = opts[i][:2] | ||
} | ||
|
||
switch { | ||
case opts[i] == "-c": | ||
lastWasDashC = true | ||
continue | ||
case lastWasDashC: | ||
lastWasDashC = false | ||
// if the last option was '-c' parse current option with no regard to | ||
// the prefix | ||
prefix = "" | ||
case prefix == "--" || prefix == "-c": | ||
lastWasDashC = false | ||
default: | ||
return nil, errors.Newf( | ||
"option %q is invalid, must have prefix '-c' or '--'", opts[i]) | ||
} | ||
|
||
key, value, err := splitOption(opts[i], prefix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res.Set(key, value) | ||
} | ||
return res, nil | ||
} | ||
|
||
// splitOptions slices the given string into substrings separated by space | ||
// unless the space is escaped using backslashes '\\'. It also skips multiple | ||
// subsequent spaces. | ||
func splitOptions(options string) []string { | ||
var res []string | ||
var sb strings.Builder | ||
i := 0 | ||
for i < len(options) { | ||
sb.Reset() | ||
// skip leading spaces. | ||
for i < len(options) && unicode.IsSpace(rune(options[i])) { | ||
i++ | ||
} | ||
if i == len(options) { | ||
break | ||
} | ||
|
||
lastWasEscape := false | ||
|
||
for i < len(options) { | ||
if unicode.IsSpace(rune(options[i])) && !lastWasEscape { | ||
break | ||
} | ||
if !lastWasEscape && options[i] == '\\' { | ||
lastWasEscape = true | ||
} else { | ||
lastWasEscape = false | ||
sb.WriteByte(options[i]) | ||
} | ||
i++ | ||
} | ||
|
||
res = append(res, sb.String()) | ||
} | ||
|
||
return res | ||
} | ||
|
||
// splitOption splits the given opt argument into substrings separated by '='. | ||
// It returns an error if the given option does not comply with the pattern | ||
// "key=value" and the number of elements in the result is not two. | ||
// splitOption removes the prefix from the key and replaces '-' with '_' so | ||
// "--option-name=value" becomes [option_name, value]. | ||
func splitOption(opt, prefix string) (string, string, error) { | ||
kv := strings.Split(opt, "=") | ||
|
||
if len(kv) != 2 { | ||
return "", "", errors.Newf( | ||
"option %q is invalid, check '='", opt) | ||
} | ||
|
||
kv[0] = strings.TrimPrefix(kv[0], prefix) | ||
|
||
return strings.ReplaceAll(kv[0], "-", "_"), kv[1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.