-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend version command to help troubleshoot porter #642
Changes from 7 commits
ecbdbcd
bf5f635
822be22
829863c
51f0069
665e909
6390e72
591631c
266a307
b5f7fcf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,111 @@ | ||
package porter | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/deislabs/porter/pkg" | ||
"github.com/deislabs/porter/pkg/context" | ||
"github.com/deislabs/porter/pkg/mixin" | ||
"github.com/deislabs/porter/pkg/porter/version" | ||
"github.com/deislabs/porter/pkg/printer" | ||
"github.com/pkg/errors" | ||
"runtime" | ||
"text/template" | ||
) | ||
|
||
func (p *Porter) PrintVersion(opts version.Options) error { | ||
type VersionOpts struct { | ||
version.Options | ||
System bool | ||
} | ||
|
||
type SystemInfo struct { | ||
OS string | ||
Arch string | ||
} | ||
|
||
type Mixins []mixin.Metadata | ||
|
||
type SystemDebugInfo struct { | ||
Version mixin.Metadata `json:"version"` | ||
SysInfo SystemInfo `json:"system"` | ||
Mixins Mixins `json:"mixins"` | ||
} | ||
|
||
func (mixins Mixins) PrintMixinsTable() string { | ||
buffer := &bytes.Buffer{} | ||
printMixinRow := | ||
func(v interface{}) []interface{} { | ||
m, ok := v.(mixin.Metadata) | ||
if !ok { | ||
return nil | ||
} | ||
return []interface{}{m.Name, m.VersionInfo.Version, m.VersionInfo.Author} | ||
} | ||
err := printer.PrintTable(buffer, mixins, printMixinRow, "Name", "Version", "Author") | ||
if err != nil { | ||
return "" | ||
} | ||
return buffer.String() | ||
} | ||
|
||
func (p *Porter) PrintVersion(opts VersionOpts) error { | ||
metadata := mixin.Metadata{ | ||
Name: "porter", | ||
VersionInfo: mixin.VersionInfo{ | ||
Version: pkg.Version, | ||
Commit: pkg.Commit, | ||
}, | ||
} | ||
return version.PrintVersion(p.Context, opts, metadata) | ||
|
||
if opts.System { | ||
return p.PrintDebugInfo(p.Context, opts, metadata) | ||
} | ||
|
||
return version.PrintVersion(p.Context, opts.Options, metadata) | ||
} | ||
|
||
func getSystemInfo() *SystemInfo { | ||
return &SystemInfo{ | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
} | ||
} | ||
|
||
func (p *Porter) PrintDebugInfo(ctx *context.Context, opts VersionOpts, versionMetadata mixin.Metadata) error { | ||
opts.RawFormat = string(printer.FormatPlaintext) | ||
mixins, err := p.ListMixins() | ||
sysInfo := getSystemInfo() | ||
if err != nil { | ||
_ = errors.Wrap(err, "Failed to get list of mixins") | ||
} | ||
sysDebugInfo := SystemDebugInfo{ | ||
Version: versionMetadata, | ||
SysInfo: *sysInfo, | ||
Mixins: mixins, | ||
} | ||
|
||
switch opts.Format { | ||
case printer.FormatJson: | ||
return printer.PrintJson(ctx.Out, sysDebugInfo) | ||
case printer.FormatPlaintext: | ||
plaintextTmpl := `{{.Version.Name}} {{.Version.VersionInfo.Version}} ({{.Version.VersionInfo.Commit}}) | ||
|
||
System | ||
------- | ||
os: {{.SysInfo.OS}} | ||
arch: {{.SysInfo.Arch}} | ||
{{if .Mixins}} | ||
Mixins | ||
------- | ||
{{.Mixins.PrintMixinsTable}}{{end}} | ||
` | ||
tmpl, err := template.New("systemDebugInfo").Parse(plaintextTmpl) | ||
if err != nil { | ||
_ = errors.Wrap(err, "Failed to print system debug information") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, if we can't parse the template, then we should return the error, and not ignore it with If the template isn't parsed, then continuing on to call Execute on the template (which is nil when err is populated) will cause a nil reference exception. |
||
} | ||
err = tmpl.Execute(ctx.Out, sysDebugInfo) | ||
return err | ||
default: | ||
return fmt.Errorf("unsupported format: %s", opts.Format) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of ignoring the error (like it's doing now), or returning it (which would make it very hard to use this command for troubleshooting problems 😅 ), we can have it print a debug message and then keep going:
The contributing guide has a section on how to handle debug logging:
https://github.com/deislabs/porter/blob/master/CONTRIBUTING.md#logging
You can check
p.Debug
and then print top.Err
following the example in the guide, then have the rest of the command continue. That way even if we can't print any mixin data, at least we are able to print the data we do have.