github.com/rrashidov/libpak@v0.0.0-20230911084305-75119185bb4d/sherpa/exists.go (about) 1 /* 2 * Copyright 2018-2022 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package sherpa 18 19 import "os" 20 21 // Exists returns true if the path exists. 22 func Exists(path string) (bool, error) { 23 if _, err := os.Stat(path); err == nil { 24 return true, nil 25 } else if os.IsNotExist(err) { 26 return false, nil 27 } else { 28 return false, err 29 } 30 } 31 32 // FileExists returns true if the path exists and is a regular file. 33 func FileExists(path string) (bool, error) { 34 if stat, err := os.Stat(path); err == nil { 35 return stat.Mode().IsRegular(), nil 36 } else if os.IsNotExist(err) { 37 return false, nil 38 } else { 39 return false, err 40 } 41 } 42 43 // DirExists returns true if the path exists and is a directory. 44 func DirExists(path string) (bool, error) { 45 if stat, err := os.Stat(path); err == nil { 46 return stat.IsDir(), nil 47 } else if os.IsNotExist(err) { 48 return false, nil 49 } else { 50 return false, err 51 } 52 } 53 54 // SymlinkExists returns true if the path exists and is a symlink. 55 func SymlinkExists(path string) (bool, error) { 56 if stat, err := os.Lstat(path); err == nil { 57 return stat.Mode()&os.ModeSymlink != 0, nil 58 } else if os.IsNotExist(err) { 59 return false, nil 60 } else { 61 return false, err 62 } 63 }