Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions internal/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/DevSymphony/sym-cli/internal/config"
"github.com/DevSymphony/sym-cli/internal/git"
"github.com/DevSymphony/sym-cli/internal/github"
Expand Down Expand Up @@ -82,6 +83,13 @@ func runInit(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// If force flag is set, remove existing code-policy.json
if initForce {
if err := removeExistingCodePolicy(); err != nil {
fmt.Printf("⚠ Warning: Failed to remove existing code-policy.json: %v\n", err)
}
}

// Create roles with current user as admin
newRoles := roles.Roles{
"admin": []string{user.Login},
Expand Down Expand Up @@ -165,3 +173,41 @@ func createDefaultPolicy(cfg *config.Config) error {

return policy.SavePolicy(defaultPolicy, cfg.PolicyPath)
}

// removeExistingCodePolicy removes all files generated by convert command
// including linter configurations and code-policy.json
func removeExistingCodePolicy() error {
// Files generated by convert command
convertGeneratedFiles := []string{
"code-policy.json",
".eslintrc.json",
"checkstyle.xml",
"pmd-ruleset.xml",
}

// Check and remove from .sym directory
symDir, err := getSymDir()
if err == nil {
for _, filename := range convertGeneratedFiles {
filePath := filepath.Join(symDir, filename)
if _, err := os.Stat(filePath); err == nil {
if err := os.Remove(filePath); err != nil {
fmt.Printf("⚠ Warning: Failed to remove %s: %v\n", filePath, err)
} else {
fmt.Printf("✓ Removed existing %s\n", filePath)
}
}
}
}

// Check and remove from current directory (legacy mode for code-policy.json)
legacyPath := "code-policy.json"
if _, err := os.Stat(legacyPath); err == nil {
if err := os.Remove(legacyPath); err != nil {
return fmt.Errorf("failed to remove %s: %w", legacyPath, err)
}
fmt.Printf("✓ Removed existing %s\n", legacyPath)
}

return nil
}