| 1 |
// Copyright 2023 The Chromium Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
package helpers |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
func Check(e error) { |
| 14 |
if e != nil { |
| 15 |
panic(e) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
func Abs(x int) int { |
| 20 |
if x < 0 { |
| 21 |
return -x |
| 22 |
} |
| 23 |
return x |
| 24 |
} |
| 25 |
|
| 26 |
func SplitAsInts(str string, sep string) []int { |
| 27 |
arr := strings.Split(str, sep) |
| 28 |
var result []int |
| 29 |
for _, i := range arr { |
| 30 |
j, err := strconv.Atoi(i) |
| 31 |
if err != nil { |
| 32 |
return nil |
| 33 |
} |
| 34 |
result = append(result, j) |
| 35 |
} |
| 36 |
return result |
| 37 |
} |
| 38 |
|
| 39 |
func ExpandPath(path string) string { |
| 40 |
if strings.HasPrefix(path, "~/") { |
| 41 |
home, err := os.UserHomeDir() |
| 42 |
Check(err) |
| 43 |
return filepath.Join(home, (path)[2:]) |
| 44 |
} |
| 45 |
return path |
| 46 |
} |
| 47 |
|
| 48 |
func WriteTextFile(fullFileName string, text string) { |
| 49 |
err := os.WriteFile(fullFileName, []byte(text), 0666) |
| 50 |
Check(err) |
| 51 |
} |