Skip to content

Commit

Permalink
log cmd: porcelain format; ability to delete logs
Browse files Browse the repository at this point in the history
  • Loading branch information
simcap committed Feb 28, 2017
1 parent b9139ad commit fd26a25
Showing 1 changed file with 73 additions and 21 deletions.
94 changes: 73 additions & 21 deletions commands/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ import (
"github.com/oklog/ulid"
"github.com/spf13/cobra"
"github.com/wallix/awless/database"
"github.com/wallix/awless/template"
)

var (
logPorcelainFlag bool
deleteLogsFlag bool
)

func init() {
RootCmd.AddCommand(logCmd)

logCmd.Flags().BoolVarP(&logPorcelainFlag, "porcelain", "p", false, "Format for machine consumption")
logCmd.Flags().BoolVarP(&deleteLogsFlag, "delete", "d", false, "Delete all logs from local db")
}

var logCmd = &cobra.Command{
Expand All @@ -40,43 +49,86 @@ var logCmd = &cobra.Command{
RunE: func(c *cobra.Command, args []string) error {
db, err, dbclose := database.Current()
exitOn(err)

if deleteLogsFlag {
return db.DeleteTemplateExecutions()
}

all, err := db.ListTemplateExecutions()
dbclose()
exitOn(err)

for _, templ := range all {
var buff bytes.Buffer

for _, done := range templ.Executed {
line := fmt.Sprintf("\t%s", done.Line)
if done.Err != "" {
buff.WriteString(renderRedFn(line))
buff.WriteByte('\n')
buff.WriteString(formatMultiLineErrMsg(done.Err))
} else {
buff.WriteString(renderGreenFn(line))
}
buff.WriteByte('\n')
}

uid, err := ulid.Parse(templ.ID)
exitOn(err)

date := time.Unix(int64(uid.Time())/int64(1000), time.Nanosecond.Nanoseconds())

fmt.Printf("Date: %s\n", date.Format(time.Stamp))
if templ.IsRevertible() {
fmt.Printf("Revert id: %s\n", templ.ID)
if logPorcelainFlag {
formatForMachine(&buff, templ)
} else {
fmt.Println("Revert id: <not revertible>")
formatForHuman(&buff, templ)
}

fmt.Println(buff.String())
}

return nil
},
}

func formatForMachine(buff *bytes.Buffer, templ *template.TemplateExecution) {
sep := '\t'

buff.WriteString(parseULIDDate(templ.ID))
buff.WriteRune(sep)
if templ.IsRevertible() {
buff.WriteString(templ.ID)
} else {
buff.WriteString("<not revertible>")
}
buff.WriteByte('\n')
for _, done := range templ.Executed {
if done.Err != "" {
buff.WriteString("KO")
} else {
buff.WriteString("OK")
}
buff.WriteRune(sep)
buff.WriteString(done.Result)
buff.WriteRune(sep)
buff.WriteString(done.Line)
buff.WriteByte('\n')
}
}

func formatForHuman(buff *bytes.Buffer, templ *template.TemplateExecution) {
for _, done := range templ.Executed {
line := fmt.Sprintf("\t%s", done.Line)
if done.Err != "" {
buff.WriteString(renderRedFn(line))
buff.WriteByte('\n')
buff.WriteString(formatMultiLineErrMsg(done.Err))
} else {
buff.WriteString(renderGreenFn(line))
}
buff.WriteByte('\n')
}

fmt.Printf("Date: %s\n", parseULIDDate(templ.ID))
if templ.IsRevertible() {
fmt.Printf("Revert id: %s\n", templ.ID)
} else {
fmt.Println("Revert id: <not revertible>")
}
}

func parseULIDDate(uid string) string {
parsed, err := ulid.Parse(uid)
exitOn(err)

date := time.Unix(int64(parsed.Time())/int64(1000), time.Nanosecond.Nanoseconds())

return date.Format(time.Stamp)
}

func formatMultiLineErrMsg(msg string) string {
notabs := strings.Replace(msg, "\t", "", -1)
var indented []string
Expand Down

0 comments on commit fd26a25

Please sign in to comment.