-
Notifications
You must be signed in to change notification settings - Fork 323
/
fileutil.go
39 lines (33 loc) · 1.21 KB
/
fileutil.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
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package fileutil
import (
"os"
"path"
"strings"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/pkg/log"
)
// FileExists checks if a file or a directory already exists
func FileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// GetFileAbsPath returns the absolute path of a file in blockchain directory
func GetFileAbsPath(filename string) string {
pwd, err := os.Getwd()
if err != nil {
log.L().Fatal("Fail to get absolute path of genesis actions yaml file.", zap.Error(err))
}
firstIndex := strings.LastIndex(pwd, "iotex-core")
index := strings.Index(pwd[firstIndex:], "/")
if index == -1 {
return path.Join(pwd, "blockchain", filename)
}
return path.Join(pwd[0:firstIndex+index], "blockchain", filename)
}