github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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  	"github.com/dolthub/dolt/go/libraries/doltcore/dconfig"
    24  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    25  )
    26  
    27  const (
    28  	credsDir = "creds"
    29  
    30  	configFile       = "config.json"
    31  	GlobalConfigFile = "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(dconfig.EnvDoltRootPath); ok && doltRootPath != "" {
    45  		return filesys.LocalFS.Abs(doltRootPath)
    46  	}
    47  
    48  	var home string
    49  	if homeEnvPath, ok := os.LookupEnv(dconfig.EnvHome); ok && homeEnvPath != "" {
    50  		home = homeEnvPath
    51  	} else if usr, err := user.Current(); err != nil {
    52  		return "", err
    53  	} else {
    54  		home = usr.HomeDir
    55  	}
    56  
    57  	_, err := os.Stat(home)
    58  	if err != nil {
    59  		return "", err
    60  	}
    61  	return home, nil
    62  }
    63  
    64  func getCredsDir(hdp HomeDirProvider) (string, error) {
    65  	homeDir, err := hdp()
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  
    70  	return filepath.Join(homeDir, dbfactory.DoltDir, credsDir), nil
    71  }
    72  
    73  func getGlobalCfgPath(hdp HomeDirProvider) (string, error) {
    74  	homeDir, err := hdp()
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  
    79  	return filepath.Join(homeDir, dbfactory.DoltDir, GlobalConfigFile), nil
    80  }
    81  
    82  func getLocalConfigPath() string {
    83  	return filepath.Join(dbfactory.DoltDir, configFile)
    84  }
    85  
    86  func getRepoStateFile() string {
    87  	return filepath.Join(dbfactory.DoltDir, repoStateFile)
    88  }
    89  
    90  func getHomeDir(hdp HomeDirProvider) (string, error) {
    91  	homeDir, err := hdp()
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	return homeDir, nil
    96  }