Skip to content

Commit

Permalink
Merge pull request #5 from Ahmedhossamdev/feat/basic-flags
Browse files Browse the repository at this point in the history
Add flags and Update readme file
  • Loading branch information
Ahmedhossamdev authored Jan 18, 2025
2 parents c4602c3 + f78252e commit 03bdffa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ printlayout
To print the directory structure of a specific folder:

```bash
printlayout /path/to/your/folder
printlayout -dir /path/to/your/folder
```

### Example Output
Expand Down Expand Up @@ -87,7 +87,7 @@ go run ./cmd/main.go /path/to/your/folder
### Run the project and export the output to a file

```bash
go run ./cmd/main.go /path/to/your/folder /path/to/output/file
go run ./cmd/main.go -dir /path/to/your/folder -output /path/to/output/file
```

### Run Tests
Expand Down
26 changes: 7 additions & 19 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,16 @@ package main

import (
"PrintLayout/pkg/printer"
"os"
"flag"
)

func main() {
config := printer.Config{}

// TODO: Add advanced command-line options (depth, hidden files, etc, ignore files, etc, ignore files that in .gitignore)
flag.StringVar(&config.DirPath, "dir", ".", "Directory path to print the structure of")
flag.StringVar(&config.OutputPath, "output", "", "Output file path")

// Get the root directory
root := "."
if len(os.Args) > 1 {
root = os.Args[1]
}
flag.Parse()

// the output file
outputFile := ".";
if len(os.Args) > 2 {
outputFile = os.Args[2]
}

if outputFile == "." {
printer.PrintProjectStructure(root)
} else {
printer.PrintProjectStructureAndAddToDir(root, outputFile)
}
}
printer.PrintProjectStructure(config.DirPath, config.OutputPath)
}
52 changes: 26 additions & 26 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@ import (
"strings"
)

// PrintProjectStructureAndAddToDir prints the directory structure of the given root directory and writes it to the output file.
func PrintProjectStructureAndAddToDir(root string, outputFile string) {
// Config holds the flag values
type Config struct {
DirPath string
OutputPath string
}

// HandleFlags
func HandleFlags(config Config) {
PrintProjectStructure(config.DirPath, config.OutputPath)
}

// PrintProjectStructure prints the directory structure of the given root directory.
// It always prints the structure to the console and writes to the output file if provided.
func PrintProjectStructure(root string, outputFile string) {
absRoot, err := filepath.Abs(root)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

absOutputFile, err := filepath.Abs(outputFile)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

rootName := filepath.Base(absRoot)
output := fmt.Sprintf("%s/\n", rootName)

output += getTreeOutput(absRoot, "")

if absOutputFile != "" {
err := os.WriteFile(absOutputFile, []byte(output), 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
}
} else {
fmt.Print(output)
}
fmt.Print(output)

fmt.Println(output)
if outputFile != "" {
writeToFile(output, outputFile)
}
}

// PrintProjectStructure prints the folder structure starting from the specified directory.
func PrintProjectStructure(root string) {
absRoot, err := filepath.Abs(root)
// writeToFile writes the output to the specified file
func writeToFile(output, outputFile string) {
absOutputFile, err := filepath.Abs(outputFile)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}

rootName := filepath.Base(absRoot)
fmt.Printf("%s/\n", rootName)
fmt.Print(getTreeOutput(absRoot, ""))
err = os.WriteFile(absOutputFile, []byte(output), 0644)
if err != nil {
fmt.Println("Error writing to file:", err)
}
}

// getTreeOutput returns the directory tree structure as a string.
Expand Down Expand Up @@ -104,4 +104,4 @@ func getIndent(isLast bool) string {
return " "
}
return "│ "
}
}
2 changes: 1 addition & 1 deletion pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestPrintProjectStructure(t *testing.T) {
os.Chdir(tmpDir)

output := captureOutput(func() {
PrintProjectStructure(".")
PrintProjectStructure(".", "")
})

rootName := filepath.Base(tmpDir)
Expand Down

0 comments on commit 03bdffa

Please sign in to comment.