fix: reuse existing license if user didn't set any#104
fix: reuse existing license if user didn't set any#104suzaku wants to merge 1 commit intospf13:mainfrom
Conversation
db6f837 to
4b53e45
Compare
GitHub now supports creating files like LICENSE for newly created repos. Running `cobra-cli init` after cloning a newly created repo will now empty that existing license.
4b53e45 to
efba9c9
Compare
| } | ||
| licenseFile, err := os.Create(fmt.Sprintf("%s/LICENSE", p.AbsolutePath)) | ||
|
|
||
| licensePath := p.AbsolutePath + "/LICENSE" |
There was a problem hiding this comment.
This is nicer, but the rest of the code use the previous style so this create inconsistency.
It would be better to keep the current style for this change, and if changing the way paths are created is needed, do this in the entire file or project.
| } | ||
| } | ||
|
|
||
| licenseFile, err := os.Create(licensePath) |
There was a problem hiding this comment.
Using exiting license makes sense, but this check ignores errors from os.Stat(), (other then fs.ErrNotExits). If the file exits and os.Stat failed, something is very wrong and failing is the best option.
But I think there is a better way - do we really want to overwrite existing license file if the user specify the license type? This does not feel like a careful way to modify user source tree.
So we can ensure that we never overwrite existing license, and handle the special case when the user did not specify the license, in which we will reuse the existing license.
Something like:
licenseFile, err := os.OpenFile(licensePath, O_CREATE|O_EXCL, 0755)
if err != nil {
if errors.Is(err, fs.ErrExist) && p.Legal.Name == "None" {
// User did not specify the license - use the existing one.
return nil
}
return err
}
GitHub now supports creating files like LICENSE for newly created repos.
Running
cobra-cli initafter cloning a newly created repo will nowempty that existing license.