// Copyright 2020 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE and LICENSE.gogs file. package osutil import ( "os" "os/user" ) // IsFile returns true if given path exists as a file (i.e. not a directory). func IsFile(path string) bool { f, e := os.Stat(path) if e != nil { return false } return !f.IsDir() } // IsDir returns true if given path is a directory, and returns false when it's // a file or does not exist. func IsDir(dir string) bool { f, e := os.Stat(dir) if e != nil { return false } return f.IsDir() } // IsExist returns true if a file or directory exists. func IsExist(path string) bool { _, err := os.Stat(path) return err == nil || os.IsExist(err) } // CurrentUsername returns the username of the current user. func CurrentUsername() string { // To get the real user of the current process, you should use user.Current(), // and USER may not be the real user of the process (for example, executing it //through sudo will display the original user) if currentUser, err := user.Current(); err == nil && currentUser != nil && len(currentUser.Username) > 0 { return currentUser.Username } username := os.Getenv("USER") if len(username) > 0 { return username } username = os.Getenv("USERNAME") if len(username) > 0 { return username } return username }