Skip to content

Commit

Permalink
Fix reading content in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
peteraba committed Sep 9, 2020
1 parent bf19791 commit fb8b417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
*.pem
*.log
bin
docker\-compose.yml
airtmp/
build/
Expand Down
31 changes: 29 additions & 2 deletions cmd/roadmapper/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"

_ "github.com/lib/pq"
Expand Down Expand Up @@ -107,11 +110,17 @@ func createCLICommand(logger *zap.Logger) *cli.Command {
&cli.StringFlag{Name: "markToday", Usage: "weather or not to add a line to mark the current day", Value: "", EnvVars: []string{"MARK_TODAY"}},
},
Action: func(c *cli.Context) error {
content, err := readContent(c.String("input"))
if err != nil {
logger.Error("failed to render roadmap", zap.Error(err))
return err
}

io := roadmap.NewIO()
err := Render(
err = Render(
io,
logger,
c.String("input"),
content,
c.String("output"),
c.String("formatFile"),
c.String("dateFormat"),
Expand All @@ -129,6 +138,24 @@ func createCLICommand(logger *zap.Logger) *cli.Command {
}
}

func readContent(input string) (string, error) {
if input != "" {
content, err := ioutil.ReadFile(input)
if err != nil {
return "", err
}
return string(content), nil
}

lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return strings.Join(lines, "\n"), nil
}

func createVersionCommand() *cli.Command {
return &cli.Command{
Name: "version",
Expand Down

0 comments on commit fb8b417

Please sign in to comment.