Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improved readability in parsetContent() #16

Merged
merged 1 commit into from
Jun 25, 2024
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
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
Loading