Merge pull request #115 from github/jg/logs-flags

ghcs logs: codespace flag, deprecate argument
This commit is contained in:
Jose Garcia 2021-09-09 10:07:41 -04:00 committed by GitHub
commit e87ca729f1

View file

@ -14,22 +14,34 @@ import (
)
func newLogsCmd() *cobra.Command {
var tail bool
var (
codespace string
tail bool
follow bool
)
log := output.NewLogger(os.Stdout, os.Stderr, false)
logsCmd := &cobra.Command{
Use: "logs [<codespace>]",
Use: "logs",
Short: "Access codespace logs",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var codespaceName string
if len(args) > 0 {
codespaceName = args[0]
log.Errorln("<codespace> argument is deprecated. Use --codespace instead.")
codespace = args[0]
}
return logs(context.Background(), tail, codespaceName)
if tail {
log.Errorln("--tail flag is deprecated. Use --follow instead.")
follow = true
}
return logs(context.Background(), log, codespace, follow)
},
}
logsCmd.Flags().BoolVarP(&tail, "tail", "t", false, "Tail the logs")
logsCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
logsCmd.Flags().BoolVarP(&tail, "tail", "t", false, "Tail the logs (deprecated, use --follow)")
logsCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Tail and follow the logs")
return logsCmd
}
@ -38,13 +50,12 @@ func init() {
rootCmd.AddCommand(newLogsCmd())
}
func logs(ctx context.Context, tail bool, codespaceName string) error {
func logs(ctx context.Context, log *output.Logger, codespaceName string, follow bool) error {
// Ensure all child tasks (port forwarding, remote exec) terminate before return.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
log := output.NewLogger(os.Stdout, os.Stderr, false)
user, err := apiClient.GetUser(ctx)
if err != nil {
@ -75,7 +86,7 @@ func logs(ctx context.Context, tail bool, codespaceName string) error {
}
cmdType := "cat"
if tail {
if follow {
cmdType = "tail -f"
}