Skip to content
Merged
Show file tree
Hide file tree
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
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Folder Structure CLI is a command-line tool written in Go that creates a folder
- Create folders and files based on a JSON structure
- Simple command-line interface
- Recursive creation of nested structures
- Optional protection against overwriting existing files and folders
- Error handling and reporting

## Installation
Expand Down Expand Up @@ -40,17 +41,38 @@ Folder Structure CLI is a command-line tool written in Go that creates a folder

### Basic Command
```bash
./folder-structure-cli create [json_file_path] [output_path]
./folder-structure-cli create [json_file_path] [output_path] [flags]
```
- `[json_file_path]`: Path to the JSON file describing the folder structure
- `[output_path]`: Path where the folder structure will be created

### Example
### Available Flags

- `--no-overwrite` or `-n`: Do not overwrite existing files and folders. When this flag is used, any existing files or folders will be skipped instead of being overwritten.

### Examples

#### Basic usage (overwrites existing files by default):
```bash
./folder-structure-cli create structure.json ./output
```

#### With no-overwrite protection:
```bash
./folder-structure-cli create structure.json ./output --no-overwrite
```

or using the short flag:
```bash
./folder-structure-cli create structure.json ./output -n
```

When using the `--no-overwrite` flag:
- Existing files and folders will be skipped
- A message will be displayed for each skipped item
- New files and folders will still be created normally
- This helps prevent accidental data loss

### Version Information

To display the version of the CLI:
Expand All @@ -62,6 +84,7 @@ or
```bash
./folder-structure-cli --version
```

## JSON Structure

The JSON file should describe the folder structure. Use `null` for files and nested objects for folders.
Expand Down Expand Up @@ -94,12 +117,29 @@ output/
│ └── file3.txt
└── file4.txt
```

## Safety Features

### No-Overwrite Protection

When using the `--no-overwrite` flag, the tool will:
- Check if each file or folder already exists before creating it
- Skip creation if the item already exists
- Display a message indicating which items were skipped
- Continue processing the rest of the structure

This is particularly useful when:
- Adding new files to an existing project structure
- Running the command multiple times
- Protecting important existing files from being accidentally overwritten

## Error Handling

The CLI will print error messages for:
- Invalid JSON files
- File read/write errors
- Invalid folder structures
- Permission issues

## Development

Expand All @@ -108,13 +148,17 @@ The CLI will print error messages for:
folder-structure-cli/
├── cmd/
│ ├── root.go
│ └── create.go
│ ├── create.go
│ ├── create_test.go
│ └── version.go
├── main.go
└── go.mod
```
- `main.go`: Entry point of the application
- `cmd/root.go`: Defines the root command and version flag
- `cmd/create.go`: Implements the `create` command
- `cmd/create.go`: Implements the `create` command with no-overwrite functionality
- `cmd/create_test.go`: Tests for the create command including no-overwrite scenarios
- `cmd/version.go`: Version command implementation

### Adding New Commands

Expand All @@ -124,10 +168,22 @@ To add a new command:
2. Define the command structure and functionality
3. Add the command to the root command in the `init()` function

### Running Tests

```bash
go test ./...
```

or with coverage:

```bash
make test
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.
This project is licensed under the MIT License - see the LICENSE file for details.
16 changes: 0 additions & 16 deletions TODO

This file was deleted.

49 changes: 40 additions & 9 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/spf13/cobra"
)

var (
noOverwrite bool
)

var createCmd = &cobra.Command{
Use: "create [json_file_path] [output_path]",
Short: "Create folder structure from JSON",
Expand All @@ -19,6 +23,7 @@ var createCmd = &cobra.Command{
}

func init() {
createCmd.Flags().BoolVarP(&noOverwrite, "no-overwrite", "n", false, "Do not overwrite existing files and folders")
RootCmd.AddCommand(createCmd)
}

Expand All @@ -42,7 +47,7 @@ func runCreate(cmd *cobra.Command, args []string) {
}

// Create folder structure
err = createStructure(outputPath, structure)
err = createStructure(outputPath, structure, noOverwrite)
if err != nil {
fmt.Printf("Error creating folder structure: %v\n", err)
return
Expand All @@ -51,23 +56,49 @@ func runCreate(cmd *cobra.Command, args []string) {
fmt.Println("Folder structure created successfully.")
}

func createStructure(basePath string, structure map[string]interface{}) error {
func createStructure(basePath string, structure map[string]interface{}, noOverwrite bool) error {
for key, value := range structure {
itemPath := filepath.Join(basePath, key)

if value == nil {
// Create file
// Handle file creation
if noOverwrite {
if _, err := os.Stat(itemPath); err == nil {
fmt.Printf("Skipping existing file: %s\n", itemPath)
continue
}
}

// Ensure the parent directory exists
parentDir := filepath.Dir(itemPath)
if err := os.MkdirAll(parentDir, os.ModePerm); err != nil {
return fmt.Errorf("error creating parent directory %s: %v", parentDir, err)
}

_, err := os.Create(itemPath)
if err != nil {
return fmt.Errorf("error creating file %s: %v", itemPath, err)
}
} else if subStructure, ok := value.(map[string]interface{}); ok {
// Create directory
err := os.MkdirAll(itemPath, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating directory %s: %v", itemPath, err)
// Handle directory creation
dirExists := false
if _, err := os.Stat(itemPath); err == nil {
dirExists = true
if noOverwrite {
fmt.Printf("Directory already exists: %s\n", itemPath)
}
}
// Recursively create its structure
err = createStructure(itemPath, subStructure)

// Create directory if it doesn't exist
if !dirExists {
err := os.MkdirAll(itemPath, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating directory %s: %v", itemPath, err)
}
}

// Always process the directory contents, regardless of whether the directory existed
err := createStructure(itemPath, subStructure, noOverwrite)
if err != nil {
return err
}
Expand Down
Loading