Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [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 | "strconv" |
| 22 | "time" |
| 23 | ) |
| 24 | |
| 25 | // ParseTimeout returns a parsed duration from a string |
| 26 | // A duration string value must be a positive integer, optionally followed by a corresponding time unit (s|m|h). |
| 27 | func ParseTimeout(duration string) (time.Duration, error) { |
| 28 | if i, err := strconv.ParseInt(duration, 10, 64); err == nil && i >= 0 { |
| 29 | return (time.Duration(i) * time.Second), nil |
| 30 | } |
| 31 | if requestTimeout, err := time.ParseDuration(duration); err == nil { |
| 32 | return requestTimeout, nil |
| 33 | } |
| 34 | 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)") |
| 35 | } |