github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/paths.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package env
    16  
    17  import (
    18  	"os"
    19  	"os/user"
    20  	"path/filepath"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
    23  )
    24  
    25  const (
    26  	homeEnvVar         = "HOME"
    27  	doltRootPathEnvVar = "DOLT_ROOT_PATH"
    28  	credsDir           = "creds"
    29  
    30  	configFile   = "config.json"
    31  	globalConfig = "config_global.json"
    32  
    33  	repoStateFile = "repo_state.json"
    34  )
    35  
    36  // HomeDirProvider is a function that returns the users home directory.  This is where global dolt state is stored for
    37  // the current user
    38  type HomeDirProvider func() (string, error)
    39  
    40  // GetCurrentUserHomeDir will return the current user's home directory by default.  This directory is where global dolt
    41  // state will be stored inside of the .dolt directory.  The environment variable DOLT_ROOT_PATH can be used to
    42  // provide a different directory where the root .dolt directory should be located and global state will be stored there.
    43  func GetCurrentUserHomeDir() (string, error) {
    44  	if doltRootPath, ok := os.LookupEnv(doltRootPathEnvVar); ok && doltRootPath != "" {
    45  		return doltRootPath, nil
    46  	}
    47  	if homeEnvPath, ok := os.LookupEnv(homeEnvVar); ok && homeEnvPath != "" {
    48  		return homeEnvPath, nil
    49  	}
    50  	if usr, err := user.Current(); err != nil {
    51  		return "", err
    52  	} else {
    53  		return usr.HomeDir, nil
    54  	}
    55  }
    56  
    57  func getCredsDir(hdp HomeDirProvider) (string, error) {
    58  	homeDir, err := hdp()
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  
    63  	return filepath.Join(homeDir, dbfactory.DoltDir, credsDir), nil
    64  }
    65  
    66  func getGlobalCfgPath(hdp HomeDirProvider) (string, error) {
    67  	homeDir, err := hdp()
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  
    72  	return filepath.Join(homeDir, dbfactory.DoltDir, globalConfig), nil
    73  }
    74  
    75  func getLocalConfigPath() string {
    76  	return filepath.Join(dbfactory.DoltDir, configFile)
    77  }
    78  
    79  func getRepoStateFile() string {
    80  	return filepath.Join(dbfactory.DoltDir, repoStateFile)
    81  }
    82  
    83  func getHomeDir(hdp HomeDirProvider) (string, error) {
    84  	homeDir, err := hdp()
    85  	if err != nil {
    86  		return "", err
    87  	}
    88  	return homeDir, nil
    89  }