blob: d7572232aaeaf8af849d393e95046ecae622e831 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package clientcmd
18
19import (
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).
28func 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
38func 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}