pulumi/cmd/logs.go
Matt Ellis f5dc3a2b53 Add a very barebones pulumi logs command
This is the smallest possible thing that could work for both the local
development case and the case where we are targeting the Pulumi
Service.

Pulling down the pulumiframework and component packages here is a bit
of a feel bad, but we plan to rework the model soon enough to a
provider model which will remove the need for us to hold on to this
code (and will bring back the factoring where the CLI does not have
baked in knowledge of the Pulumi Cloud Framework).

Fixes #527
2017-11-14 12:26:55 -08:00

65 lines
1.4 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/component"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newLogsCmd() *cobra.Command {
var stack string
var follow bool
logsCmd := &cobra.Command{
Use: "logs",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
stackName, err := explicitOrCurrent(stack, backend)
if err != nil {
return err
}
sinceTime := time.Unix(0, 0)
highestTimeSeen := time.Unix(0, 0)
for {
logs, err := backend.GetLogs(stackName, component.LogQuery{})
if err != nil {
return err
}
for _, logEntry := range logs {
eventTime := time.Unix(0, logEntry.Timestamp*1000000)
if eventTime.After(sinceTime) {
fmt.Printf("[%v] %v\n", eventTime, logEntry.Message)
}
if eventTime.After(highestTimeSeen) {
highestTimeSeen = eventTime
}
}
if !follow {
return nil
}
sinceTime = highestTimeSeen
time.Sleep(time.Second)
}
}),
}
logsCmd.PersistentFlags().StringVarP(
&stack, "stack", "s", "",
"List configuration for a different stack than the currently selected stack")
logsCmd.PersistentFlags().BoolVarP(
&follow, "follow", "f", false,
"Follow the log stream in real time (like tail -f)")
return logsCmd
}