Debian Codegraph @

pkg/utils/utils.go

raw symbols SCIP
1 package utils
2
3 import (
4 "log"
5 "os"
6 "path/filepath"
7 )
8
9 // Min returns the minimum of two integers
10 func Min(x, y int) int {
11 if x < y {
12 return x
13 }
14 return y
15 }
16
17 // Max returns the maximum of two integers
18 func Max(x, y int) int {
19 if x > y {
20 return x
21 }
22 return y
23 }
24
25 // Clamp returns a value x restricted between min and max
26 func Clamp(x int, min int, max int) int {
27 if x < min {
28 return min
29 } else if x > max {
30 return max
31 }
32 return x
33 }
34
35 // GetLazyRootDirectory finds a lazy project root directory.
36 //
37 // It's used for cheatsheet scripts and integration tests. Not to be confused with finding the
38 // root directory of _any_ random repo.
39 func GetLazyRootDirectory() string {
40 path, err := os.Getwd()
41 if err != nil {
42 panic(err)
43 }
44
45 for {
46 _, err := os.Stat(filepath.Join(path, ".git"))
47 if err == nil {
48 return path
49 }
50
51 if !os.IsNotExist(err) {
52 panic(err)
53 }
54
55 path = filepath.Dir(path)
56
57 if path == "/" {
58 log.Fatal("must run in lazy project folder or child folder")
59 }
60 }
61 }