Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: client/internal/common/utils.go

Issue 1135173003: Create packages client/internal/ retry and lhttp. (Closed) Base URL: git@github.com:luci/luci-go@3_UI
Patch Set: . Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package common 5 package common
6 6
7 import ( 7 import (
8 "errors"
9 "fmt" 8 "fmt"
10 "io" 9 "io"
11 "net/url"
12 "os" 10 "os"
13 "runtime" 11 "runtime"
14 "strings"
15 "time" 12 "time"
16 13
17 "github.com/luci/luci-go/client/internal/imported" 14 "github.com/luci/luci-go/client/internal/imported"
18 ) 15 )
19 16
20 var units = []string{"b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib" } 17 var units = []string{"b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib" }
21 18
22 // Size represents a size in bytes that knows how to print itself. 19 // Size represents a size in bytes that knows how to print itself.
23 type Size int64 20 type Size int64
24 21
(...skipping 12 matching lines...) Expand all
37 } 34 }
38 if i == 0 { 35 if i == 0 {
39 return fmt.Sprintf("%d%s", s, units[i]) 36 return fmt.Sprintf("%d%s", s, units[i])
40 } 37 }
41 if v >= 10 { 38 if v >= 10 {
42 return fmt.Sprintf("%.1f%s", v, units[i]) 39 return fmt.Sprintf("%.1f%s", v, units[i])
43 } 40 }
44 return fmt.Sprintf("%.2f%s", v, units[i]) 41 return fmt.Sprintf("%.2f%s", v, units[i])
45 } 42 }
46 43
47 // URLToHTTPS ensures the url is https://.
48 func URLToHTTPS(s string) (string, error) {
49 u, err := url.Parse(s)
50 if err != nil {
51 return "", err
52 }
53 if u.Scheme != "" && u.Scheme != "https" {
54 return "", errors.New("Only https:// scheme is accepted. It can be omitted.")
55 }
56 if !strings.HasPrefix(s, "https://") {
57 s = "https://" + s
58 }
59 if _, err = url.Parse(s); err != nil {
60 return "", err
61 }
62 return s, nil
63 }
64
65 // IsDirectory returns true if path is a directory and is accessible. 44 // IsDirectory returns true if path is a directory and is accessible.
66 func IsDirectory(path string) bool { 45 func IsDirectory(path string) bool {
67 fileInfo, err := os.Stat(path) 46 fileInfo, err := os.Stat(path)
68 return err == nil && fileInfo.IsDir() 47 return err == nil && fileInfo.IsDir()
69 } 48 }
70 49
71 func IsWindows() bool { 50 func IsWindows() bool {
72 return runtime.GOOS == "windows" 51 return runtime.GOOS == "windows"
73 } 52 }
74 53
75 // Round rounds a time.Duration at round. 54 // Round rounds a time.Duration at round.
76 func Round(value time.Duration, resolution time.Duration) time.Duration { 55 func Round(value time.Duration, resolution time.Duration) time.Duration {
77 if value < 0 { 56 if value < 0 {
78 value -= resolution / 2 57 value -= resolution / 2
79 } else { 58 } else {
80 value += resolution / 2 59 value += resolution / 2
81 } 60 }
82 return value / resolution * resolution 61 return value / resolution * resolution
83 } 62 }
84 63
85 // IsTerminal returns true if the specified io.Writer is a terminal. 64 // IsTerminal returns true if the specified io.Writer is a terminal.
86 func IsTerminal(out io.Writer) bool { 65 func IsTerminal(out io.Writer) bool {
87 f, ok := out.(*os.File) 66 f, ok := out.(*os.File)
88 if !ok { 67 if !ok {
89 return false 68 return false
90 } 69 }
91 return imported.IsTerminal(int(f.Fd())) 70 return imported.IsTerminal(int(f.Fd()))
92 } 71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698