blob: b609d1a766c02852260761516ed7651d9ee7746a [file] [log] [blame]
Kent Hagermane566c2e2019-06-03 17:56:42 -04001/*
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 "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).
27func 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}