-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #886 from HusterWan/zr/add-logs-interface
feature: add pouch logs cli command
- Loading branch information
Showing
10 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var validDrivers = map[string]bool{ | ||
"json-file": true, | ||
"journald": true, | ||
} | ||
|
||
// logsDescription is used to describe logs command in detail and auto generate command doc. | ||
var logsDescription = "" | ||
|
||
// LogsCommand use to implement 'logs' command, it is used to print a container's logs | ||
type LogsCommand struct { | ||
baseCommand | ||
details bool | ||
follow bool | ||
since string | ||
tail string | ||
timestamps bool | ||
} | ||
|
||
// Init initialize logs command. | ||
func (lc *LogsCommand) Init(c *Cli) { | ||
lc.cli = c | ||
lc.cmd = &cobra.Command{ | ||
Use: "logs [OPTIONS] CONTAINER", | ||
Short: "Print a container's logs", | ||
Long: logsDescription, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return lc.runLogs(args) | ||
}, | ||
Example: logsExample(), | ||
} | ||
lc.addFlags() | ||
} | ||
|
||
// addFlags adds flags for specific command. | ||
func (lc *LogsCommand) addFlags() { | ||
flagSet := lc.cmd.Flags() | ||
flagSet.BoolVarP(&lc.details, "details", "", false, "Show extra provided to logs") | ||
flagSet.BoolVarP(&lc.follow, "follow", "f", false, "Follow log output") | ||
flagSet.StringVarP(&lc.since, "since", "", "", "Show logs since timestamp") | ||
flagSet.StringVarP(&lc.tail, "tail", "", "all", "Number of lines to show from the end of the logs default \"all\"") | ||
flagSet.BoolVarP(&lc.timestamps, "timestamps", "t", false, "Show timestamps") | ||
} | ||
|
||
// runLogs is the entry of LogsCommand command. | ||
func (lc *LogsCommand) runLogs(args []string) error { | ||
// TODO | ||
|
||
containerName := args[0] | ||
|
||
ctx := context.Background() | ||
apiClient := lc.cli.Client() | ||
|
||
c, err := apiClient.ContainerGet(ctx, containerName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !validDrivers[c.HostConfig.LogConfig.Type] { | ||
return fmt.Errorf("\"logs\" command is supported only for \"json-file\" and \"journald\" logging drivers (got; %s)", c.HostConfig.LogConfig.Type) | ||
} | ||
|
||
opts := types.ContainerLogsOptions{ | ||
ShowStdout: true, | ||
ShowStderr: true, | ||
Since: lc.since, | ||
Timestamps: lc.timestamps, | ||
Follow: lc.follow, | ||
Tail: lc.tail, | ||
Details: lc.details, | ||
} | ||
|
||
resp, err := apiClient.ContainerLogs(ctx, containerName, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Close() | ||
|
||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(resp) | ||
|
||
fmt.Printf(buf.String()) | ||
|
||
return nil | ||
} | ||
|
||
// logsExample shows examples in logs command, and is used in auto-generated cli docs. | ||
func logsExample() string { | ||
return `` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.