Commit 850c796

Anton Medvedev <anton@medv.io>
2025-11-30 13:30:39
Add output directory validation
1 parent f6b0f38
Changed files (1)
main.go
@@ -54,6 +54,7 @@ func main() {
 
 	_, noFiles := os.LookupEnv("NO_FILES")
 	_, noCommitsList := os.LookupEnv("NO_COMMITS_LIST")
+	_, noOutputDirCheck := os.LookupEnv("NO_OUTPUT_DIR_CHECK")
 
 	flag.StringVar(&flagOwner, "owner", "", "Project owner")
 	flag.StringVar(&flagName, "name", "", "Project name")
@@ -76,6 +77,21 @@ func main() {
 		os.Exit(0)
 	}
 
+	outputDir, err := filepath.Abs(flagOutput)
+	if err != nil {
+		panic(err)
+	}
+
+	if !noOutputDirCheck {
+		if fi, err := os.Stat(outputDir); err == nil && fi.IsDir() {
+			if entries, err := os.ReadDir(outputDir); err == nil && len(entries) > 0 {
+				echo(fmt.Sprintf("Output directory %q is not empty.", outputDir))
+				echo("Please remove its contents or choose a different --output directory.")
+				os.Exit(1)
+			}
+		}
+	}
+
 	absInput, err := filepath.Abs(input)
 	if err != nil {
 		panic(err)
@@ -129,7 +145,7 @@ func main() {
 		Owner:      flagOwner,
 		Name:       flagName,
 		RepoDir:    input,
-		OutputDir:  flagOutput,
+		OutputDir:  outputDir,
 		Style:      flagTheme,
 		Dark:       themeColor == "dark",
 		DefaultRef: git.Ref(flagDefaultBranch),