mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	resolve the config file from the sudo user
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
		
							parent
							
								
									074442df7a
								
							
						
					
					
						commit
						afde6450ee
					
				
					 4 changed files with 30 additions and 3 deletions
				
			
		| 
						 | 
				
			
			@ -29,9 +29,20 @@ var (
 | 
			
		|||
	configDir = os.Getenv("DOCKER_CONFIG")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func getDefaultConfigDir(confFile string) string {
 | 
			
		||||
	confDir := filepath.Join(homedir.Get(), confFile)
 | 
			
		||||
	// if the directory doesn't exist, maybe we called docker with sudo
 | 
			
		||||
	if _, err := os.Stat(configDir); err != nil {
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			return filepath.Join(homedir.GetWithSudoUser(), confFile)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return confDir
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	if configDir == "" {
 | 
			
		||||
		configDir = filepath.Join(homedir.Get(), ".docker")
 | 
			
		||||
		configDir = getDefaultConfigDir(".docker")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -178,7 +189,7 @@ func Load(configDir string) (*ConfigFile, error) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// Can't find latest config file so check for the old one
 | 
			
		||||
	confFile := filepath.Join(homedir.Get(), oldConfigfile)
 | 
			
		||||
	confFile := getDefaultConfigDir(oldConfigfile)
 | 
			
		||||
	if _, err := os.Stat(confFile); err != nil {
 | 
			
		||||
		return &configFile, nil //missing file is not an error
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,6 +78,9 @@ For example:
 | 
			
		|||
Instructs Docker to use the configuration files in your `~/testconfigs/`
 | 
			
		||||
directory when running the `ps` command.
 | 
			
		||||
 | 
			
		||||
> **Note**: If you run docker commands with `sudo`, Docker first looks for a configuration
 | 
			
		||||
> file in `/root/.docker/`, before looking in `~/.docker/` for the user that did the sudo call.
 | 
			
		||||
 | 
			
		||||
Docker manages most of the files in the configuration directory
 | 
			
		||||
and you should not modify them. However, you *can modify* the
 | 
			
		||||
`config.json` file to control certain aspects of how the `docker`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -142,7 +142,7 @@ func rawJSON(value interface{}) *json.RawMessage {
 | 
			
		|||
// ValidateID checks whether an ID string is a valid image ID.
 | 
			
		||||
func ValidateID(id string) error {
 | 
			
		||||
	if ok := validHex.MatchString(id); !ok {
 | 
			
		||||
		return fmt.Errorf("image ID '%s' is invalid ", id)
 | 
			
		||||
		return fmt.Errorf("image ID %q is invalid", id)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,6 +29,19 @@ func Get() string {
 | 
			
		|||
	return home
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetWithSudoUser returns the home directory of the user who called sudo (if
 | 
			
		||||
// available, retrieved from $SUDO_USER). It fallbacks to Get if any error occurs.
 | 
			
		||||
// Returned path should be used with "path/filepath" to form new paths.
 | 
			
		||||
func GetWithSudoUser() string {
 | 
			
		||||
	sudoUser := os.Getenv("SUDO_USER")
 | 
			
		||||
	if sudoUser != "" {
 | 
			
		||||
		if user, err := user.LookupUser(sudoUser); err == nil {
 | 
			
		||||
			return user.Home
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return Get()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetShortcutString returns the string that is shortcut to user's home directory
 | 
			
		||||
// in the native shell of the platform running on.
 | 
			
		||||
func GetShortcutString() string {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue