Skip to content

Commit

Permalink
refactor: improved readability in parsetContent() (#16)
Browse files Browse the repository at this point in the history
- Handle each case in parseContent separately for clarity.
- Integrate the updated parseContent into the main.go.
- Ensure consistent error handling and function documentation.
  • Loading branch information
supitsdu authored Jun 25, 2024
1 parent 2cec189 commit 0b0c13e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,27 @@ func readFromFile(filePath string) (string, error) {

// parseContent determines the content string based on the flags and arguments
func parseContent(directText *string, args []string) (string, error) {
if *directText != "" {
if directText != nil && *directText != "" {
// Use the provided direct text
return *directText, nil
} else if len(args) > 0 {
// Read the content from all provided file paths
var sb strings.Builder
for _, filePath := range args {
content, err := readFromFile(filePath)
if err != nil {
return "", err
}
sb.WriteString(content + "\n")
}
return sb.String(), nil
} else {
}

if len(args) == 0 {
// Read from stdin
return readFromStdin()
}

// Read the content from all provided file paths
var sb strings.Builder
for _, filePath := range args {
content, err := readFromFile(filePath)
if err != nil {
return "", err
}
sb.WriteString(content + "\n")
}

return sb.String(), nil
}

func main() {
Expand Down Expand Up @@ -84,7 +87,7 @@ func main() {
// Refactor the code to call parseContent and validate flag args
contentStr, err := parseContent(directText, flag.Args())
if err != nil {
fmt.Printf("Error: %v\n", err)
fmt.Printf("Error parsing content: %v\n", err)
os.Exit(1)
}

Expand Down

0 comments on commit 0b0c13e

Please sign in to comment.