forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
39 lines (34 loc) · 934 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package utils
import (
"errors"
"fmt"
"io"
"os"
"github.com/mattn/go-isatty"
)
func WriteStringToFileIndexed() func(filePath, data string, writer io.Writer) error {
index := 0
return func(filePath, data string, writer io.Writer) error {
if err := os.WriteFile(filePath,
[]byte(data), 0o600); err != nil {
return fmt.Errorf("failed to write file at %s: %w", filePath, err)
}
index++
_, err := fmt.Fprintf(writer, "%d. writing file at %s\n", index, filePath)
return err
}
}
// IsPathOccupied checks whether the targeted path is already occupied
func IsPathOccupied(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
return true, nil
}
// IsTerminal checks if file descriptor is terminal or not
func IsTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}