Skip to content

Commit

Permalink
Merge branch 'master' of github.com:superfly/flyctl
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan committed May 4, 2021
2 parents 27bbfad + a397c27 commit 1d9b462
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 53 deletions.
20 changes: 20 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,26 @@ type LogEntry struct {
Meta struct {
Instance string
Region string
Event struct {
Provider string
}
HTTP struct {
Request struct {
ID string
Method string
Version string
}
Response struct {
StatusCode int `json:"status_code"`
}
}
Error struct {
Code int
Message string
}
URL struct {
Full string
}
}
}

Expand Down
51 changes: 43 additions & 8 deletions cmd/presenters/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ func (lp *LogPresenter) printEntry(w io.Writer, asJSON bool, entry api.LogEntry)
fmt.Fprintln(w, string(outBuf))
return
}

fmt.Fprintf(w, "%s ", aurora.Faint(entry.Timestamp))

if !lp.HideAllocID {
fmt.Fprintf(w, "%s ", entry.Instance)
if entry.Meta.Event.Provider != "" {
if entry.Instance != "" {
fmt.Fprintf(w, "%s[%s]", entry.Meta.Event.Provider, entry.Instance)
} else {
fmt.Fprint(w, entry.Meta.Event.Provider)
}
} else if entry.Instance != "" {
fmt.Fprintf(w, "%s", entry.Instance)
}
fmt.Fprint(w, " ")
}

if !lp.HideRegion {
Expand All @@ -44,13 +52,39 @@ func (lp *LogPresenter) printEntry(w io.Writer, asJSON bool, entry api.LogEntry)

fmt.Fprintf(w, "[%s] ", aurora.Colorize(entry.Level, levelColor(entry.Level)))

if lp.RemoveNewlines {
newLineReplacer.WriteString(w, entry.Message)
} else {
w.Write([]byte(entry.Message))
printFieldIfPresent(w, "error.code", entry.Meta.Error.Code)
hadErrorMsg := printFieldIfPresent(w, "error.message", entry.Meta.Error.Message)
printFieldIfPresent(w, "request.method", entry.Meta.HTTP.Request.Method)
printFieldIfPresent(w, "request.url", entry.Meta.URL.Full)
printFieldIfPresent(w, "request.id", entry.Meta.HTTP.Request.ID)
printFieldIfPresent(w, "response.status", entry.Meta.HTTP.Response.StatusCode)

if !hadErrorMsg {
if lp.RemoveNewlines {
_, _ = newLineReplacer.WriteString(w, entry.Message)
} else {
_, _ = w.Write([]byte(entry.Message))
}
}

_, _ = w.Write(newline)
}

func printFieldIfPresent(w io.Writer, name string, value interface{}) bool {
switch v := value.(type) {
case string:
if v != "" {
fmt.Fprintf(w, `%s"%s" `, aurora.Faint(name+"="), v)
return true
}
case int:
if v > 0 {
fmt.Fprintf(w, "%s%d ", aurora.Faint(name+"="), v)
return true
}
}

w.Write(newline)
return false
}

func levelColor(level string) aurora.Color {
Expand All @@ -59,8 +93,9 @@ func levelColor(level string) aurora.Color {
return aurora.CyanFg
case "info":
return aurora.BlueFg
case "warn":
case "warning":
return aurora.MagentaFg
return aurora.YellowFg
}
return aurora.RedFg
}
7 changes: 7 additions & 0 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func newSSHCommand(client *client.Client) *Command {
requireAppName)
console.Args = cobra.MaximumNArgs(1)

console.AddStringFlag(StringFlagOpts{
Name: "command",
Shorthand: "C",
Default: "",
Description: "command to run on SSH session",
})

console.AddBoolFlag(BoolFlagOpts{
Name: "select",
Shorthand: "s",
Expand Down
4 changes: 3 additions & 1 deletion cmd/ssh_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func runSSHConsole(ctx *cmdctx.CmdContext) error {
Org: &app.Organization,
Tunnel: tunnel,
App: ctx.AppConfig.AppName,
Cmd: ctx.Config.GetString("command"),
}, addr)
}

Expand Down Expand Up @@ -205,6 +206,7 @@ type SSHParams struct {
Org *api.Organization
App string
Tunnel *wg.Tunnel
Cmd string
}

func sshConnect(p *SSHParams, addr string) error {
Expand Down Expand Up @@ -254,7 +256,7 @@ func sshConnect(p *SSHParams, addr string) error {
Mode: "xterm",
}

if err := sshClient.Shell(context.Background(), term); err != nil {
if err := sshClient.Shell(context.Background(), term, p.Cmd); err != nil {
return fmt.Errorf("SSH shell: %w", err)
}

Expand Down

0 comments on commit 1d9b462

Please sign in to comment.