I'm trying to mirror the functionality of other programs like the awscli and oh-my-zsh, where they're able to programmatically generate files in the current user's home directory, but I'm running into an issue with replicating this functionality.
My guess is that it's something to do with the permissions param (2nd param in os.MkdirAll
), due to the syscall.Errno(30)
I'm receiving back, but I'm not super sure what the correct permission to pass in is.
fullPath := /.testProject/config
// check if the file exists
if _, err = os.Stat(fullPath); err != nil {
// if it doesn't exist, create it
if err = os.MkdirAll(fullPath, 0700); err != nil {
// currently errors out to `(*fs.PathError){Op:"mkdir",
// Path:"/.testProject", Err:syscall.Errno(30)}
return err
}
}
// return nil if successful write to
// $HOME/.testProject/config
return nil
Solution
s/o to @larsks for helping me remember how to fetch home directories. Final working code block:
dirname, err := os.UserHomeDir()
if err != nil {
return err
}
fullPath := dirname + "/.testProject/config"
// check if the file exists
if _, err = os.Stat(fullPath); err != nil {
// if it doesn't exist, create it
if err = os.MkdirAll(fullPath, 0700); err != nil {
return err
}
}
// creates file at $HOME/.testProject/config
return nil