Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2016 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package clientcmd |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "net/url" |
| 22 | "strconv" |
| 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | // ParseTimeout returns a parsed duration from a string |
| 27 | // A duration string value must be a positive integer, optionally followed by a corresponding time unit (s|m|h). |
| 28 | func ParseTimeout(duration string) (time.Duration, error) { |
| 29 | if i, err := strconv.ParseInt(duration, 10, 64); err == nil && i >= 0 { |
| 30 | return (time.Duration(i) * time.Second), nil |
| 31 | } |
| 32 | if requestTimeout, err := time.ParseDuration(duration); err == nil { |
| 33 | return requestTimeout, nil |
| 34 | } |
| 35 | return 0, fmt.Errorf("Invalid timeout value. Timeout must be a single integer in seconds, or an integer followed by a corresponding time unit (e.g. 1s | 2m | 3h)") |
| 36 | } |
| 37 | |
| 38 | func parseProxyURL(proxyURL string) (*url.URL, error) { |
| 39 | u, err := url.Parse(proxyURL) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("could not parse: %v", proxyURL) |
| 42 | } |
| 43 | |
| 44 | switch u.Scheme { |
| 45 | case "http", "https", "socks5": |
| 46 | default: |
| 47 | return nil, fmt.Errorf("unsupported scheme %q, must be http, https, or socks5", u.Scheme) |
| 48 | } |
| 49 | return u, nil |
| 50 | } |