本文整理汇总了Golang中github.com/mitchellh/go-homedir.Dir函数的典型用法代码示例。如果您正苦于以下问题:Golang Dir函数的具体用法?Golang Dir怎么用?Golang Dir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Dir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getConfigDir
func getConfigDir() string {
d, e := homedir.Dir()
if e != nil {
logrus.Fatalf("config.go: failed to get home dir: error=%v", e)
}
return path.Join(d, ".otsimo")
}
示例2: TestDefaultKeys
func TestDefaultKeys(t *testing.T) {
util.AppFs = afero.NewMemMapFs()
// Don't pull in keys from the host OS. Setting this environment variable
// is safe because it won't affect the parent shell.
os.Setenv("SSH_AUTH_SOCK", "")
dir, err := homedir.Dir()
if err != nil {
t.Errorf("Failed to get homedir: %q", err.Error())
return
}
sshDir := filepath.Join(dir, ".ssh")
if err := util.AppFs.MkdirAll(sshDir, 0600); err != nil {
t.Errorf("Failed to create SSH directory: %q", err.Error())
return
}
for _, key := range []string{"id_rsa", "id_dsa", "ignored"} {
if err := writeRandomKey(filepath.Join(sshDir, key)); err != nil {
t.Errorf("Failed to write key: %q", err.Error())
return
}
}
signers := defaultSigners()
if len(signers) != 2 {
t.Errorf("Expected two default signers, but got %v", signers)
}
}
示例3: parseConfig
func parseConfig() *viper.Viper {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(os.Stderr)
}
// Get home directory for current user
homeDir, err := homedir.Dir()
if err != nil {
fatalf("Cannot get current user home directory: %v", err)
}
if homeDir == "" {
fatalf("Cannot get current user home directory")
}
// By default our trust directory (where keys are stored) is in ~/.notary/
mainViper.SetDefault("trust_dir", filepath.Join(homeDir, filepath.Dir(configDir)))
// If there was a commandline configFile set, we parse that.
// If there wasn't we attempt to find it on the default location ~/.notary/config
if configFile != "" {
configFileExt = strings.TrimPrefix(filepath.Ext(configFile), ".")
configFileName = strings.TrimSuffix(filepath.Base(configFile), filepath.Ext(configFile))
configPath = filepath.Dir(configFile)
} else {
configPath = filepath.Join(homeDir, filepath.Dir(configDir))
}
// Setup the configuration details into viper
mainViper.SetConfigName(configFileName)
mainViper.SetConfigType(configFileExt)
mainViper.AddConfigPath(configPath)
// Find and read the config file
err = mainViper.ReadInConfig()
if err != nil {
logrus.Debugf("Configuration file not found, using defaults")
// If we were passed in a configFile via -c, bail if it doesn't exist,
// otherwise ignore it: we can use the defaults
if configFile != "" || !os.IsNotExist(err) {
fatalf("error opening config file %v", err)
}
}
// At this point we either have the default value or the one set by the config.
// Either way, the command-line flag has precedence and overwrites the value
if trustDir != "" {
mainViper.Set("trust_dir", trustDir)
}
// Expands all the possible ~/ that have been given, either through -d or config
// If there is no error, use it, if not, attempt to use whatever the user gave us
expandedTrustDir, err := homedir.Expand(mainViper.GetString("trust_dir"))
if err == nil {
mainViper.Set("trust_dir", expandedTrustDir)
}
logrus.Debugf("Using the following trust directory: %s", mainViper.GetString("trust_dir"))
return mainViper
}
示例4: New
// Opens/creates a config file for the specified appName.
// The path to config file is ~/.config/appName/config.ini.
func New(appName string) (g *GlobalConf, err error) {
hmdr, err := homedir.Dir()
if err != nil {
return
}
// Create config file's directory.
dirPath := path.Join(hmdr, ".config", appName)
if err = os.MkdirAll(dirPath, 0755); err != nil {
return
}
// Touch a config file if it doesn't exit.
filePath := path.Join(dirPath, defaultConfigFileName)
if _, err = os.Stat(filePath); err != nil {
if !os.IsNotExist(err) {
return
}
// create file
if err = ioutil.WriteFile(filePath, []byte{}, 0644); err != nil {
return
}
}
opts := Options{Filename: filePath}
return NewWithOptions(&opts)
}
示例5: GetLocalShell
func GetLocalShell() (*sh.Shell, error) {
apiAddress := os.Getenv("IPFS_API")
if apiAddress != "" {
// IPFS_API takes priority.
return sh.NewShell(apiAddress), nil
}
apiFilePath := ""
ipath := os.Getenv("IPFS_PATH")
if ipath != "" {
apiFilePath = filepath.Join(ipath, "api")
}
home, err := hd.Dir() // Get home directory
if err == nil {
apiFilePath = filepath.Join(home, ".ipfs", "api")
}
// Read the file (hopefully) containing the location of an ipfs daemon
apiFileContent, err := ioutil.ReadFile(apiFilePath)
if err != nil {
return nil, err
}
multiAddr := strings.Trim(string(apiFileContent), "\n\t ")
apiAddress, err = multiaddrToNormal(multiAddr)
if err != nil {
return nil, err
}
return sh.NewShell(apiAddress), nil
}
示例6: getHomeDir
func getHomeDir() string {
home, err := home.Dir()
if err != nil {
log.WithField("err", err).Fatal("failed to deterine a home directory")
}
return home
}
示例7: init
func init() {
currentHomeDir, err := homedir.Dir()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("Cannot fetch your home directory: %v", err))
os.Exit(1)
}
homeDir = currentHomeDir
}
示例8: vagrantDir
func vagrantDir() (string, error) {
dir, err := homedir.Dir()
if err != nil {
return "", err
}
vagrantDir := dir + "/.vagrant/"
return vagrantDir, nil
}
示例9: TestGetBucketsDirPath
func TestGetBucketsDirPath(t *testing.T) {
actual, err := getBucketsDirPath()
if err != nil {
t.Errorf("occurred error when get bucket directory path: %v", err)
}
home, _ := homedir.Dir()
expected := filepath.Join(home, ".honoka", "buckets")
if actual != expected {
t.Errorf("actual does not match expected. actual: %s , expected: %s", actual, expected)
}
}
示例10: HomeDir
// HomeDir is the home directory for the user. Light wrapper on the homedir lib.
func HomeDir() (string, error) {
homish, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("Unable to acquire home directory: %v", err)
}
home, err := homedir.Expand(homish)
if err != nil {
return "", fmt.Errorf("Unable to expand home directory: %v", err)
}
return home, nil
}
示例11: parseConfig
func parseConfig() {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(os.Stderr)
}
if trustDir == "" {
// Get home directory for current user
homeDir, err := homedir.Dir()
if err != nil {
fatalf("cannot get current user home directory: %v", err)
}
if homeDir == "" {
fatalf("cannot get current user home directory")
}
trustDir = filepath.Join(homeDir, filepath.Dir(configDir))
logrus.Debugf("no trust directory provided, using default: %s", trustDir)
} else {
logrus.Debugf("trust directory provided: %s", trustDir)
}
// If there was a commandline configFile set, we parse that.
// If there wasn't we attempt to find it on the default location ~/.notary/config
if configFile != "" {
configFileExt = strings.TrimPrefix(filepath.Ext(configFile), ".")
configFileName = strings.TrimSuffix(filepath.Base(configFile), filepath.Ext(configFile))
configPath = filepath.Dir(configFile)
} else {
configPath = trustDir
}
// Setup the configuration details into viper
mainViper.SetConfigName(configFileName)
mainViper.SetConfigType(configFileExt)
mainViper.AddConfigPath(configPath)
// Find and read the config file
err := mainViper.ReadInConfig()
if err != nil {
logrus.Debugf("configuration file not found, using defaults")
// Ignore if the configuration file doesn't exist, we can use the defaults
if !os.IsNotExist(err) {
fatalf("fatal error config file: %v", err)
}
}
}
示例12: init
func init() {
stdcli.RegisterCommand(cli.Command{
Name: "login",
Description: "log into your convox rack",
Usage: "[hostname]",
Action: cmdLogin,
Flags: []cli.Flag{
cli.StringFlag{
Name: "password, p",
Usage: "Password to use for authentication. If not specified, prompt for password.",
},
},
})
home, err := homedir.Dir()
if err != nil {
stdcli.Error(err)
return
}
ConfigRoot = filepath.Join(home, ".convox")
if root := os.Getenv("CONVOX_CONFIG"); root != "" {
ConfigRoot = root
}
stat, err := os.Stat(ConfigRoot)
if err != nil && !os.IsNotExist(err) {
stdcli.Error(err)
return
}
if stat != nil && !stat.IsDir() {
err := upgradeConfig()
if err != nil {
stdcli.Error(err)
return
}
}
}
示例13: saveRuntimeInfo
func saveRuntimeInfo() {
homeDir, _ := home.Dir()
parentPath := path.Join(homeDir, ".logk")
_, err := os.Stat(parentPath)
if err != nil {
os.Mkdir(parentPath, 0777)
}
file, err := os.OpenFile(path.Join(parentPath, "runtimeinfo"), os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
defer file.Close()
for k, v := range runtimeInfo {
file.Write([]byte(fmt.Sprintf("%s %d\n", k, v)))
}
}
示例14: CompatLoad
func CompatLoad() (*CompatConfigFile, error) {
dockerCfg := cliconfig.NewConfigFile(filepath.Join(cliconfig.ConfigDir(), cliconfig.ConfigFileName))
configFile := CompatConfigFile{
dockerCfg,
dockerCfg.Filename(),
}
// Try .docker/config.json first
if _, err := os.Stat(configFile.filename); err == nil {
file, err := os.Open(configFile.filename)
if err != nil {
return &configFile, err
}
defer file.Close()
err = configFile.LoadFromReader(file)
return &configFile, err
} else if !os.IsNotExist(err) {
return &configFile, err
}
// Try the old .dockercfg
homeDir, _ := homedir.Dir()
configFile.filename = filepath.Join(homeDir, ".dockercfg")
if _, err := os.Stat(configFile.filename); err != nil {
return &configFile, nil //missing file is not an error
}
file, err := os.Open(configFile.filename)
if err != nil {
return &configFile, err
}
defer file.Close()
err = configFile.LegacyLoadFromReader(file)
if err != nil {
return &configFile, err
}
return &configFile, nil
}
示例15: recoverRuntimeInfo
func recoverRuntimeInfo() {
homeDir, _ := home.Dir()
file, err := os.OpenFile(path.Join(homeDir, ".logk", "runtimeinfo"), os.O_RDONLY, 0666)
if err != nil {
return
}
defer file.Close()
buf := bufio.NewReader(file)
for {
line, err := buf.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
var f string
var p int64
fmt.Sscanf(line, "%s %d", &f, &p)
stat, err := os.Stat(f)
if err != nil {
p = int64(0)
} else {
if stat.Size() < p {
p = 0
}
}
runtimeInfo[f] = p
}
}