-
Notifications
You must be signed in to change notification settings - Fork 94
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 #28 from replicatedhq/v0.0.1
Adding version file, analyzer result format conversions
- Loading branch information
Showing
12 changed files
with
327 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cli | ||
|
||
import ( | ||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"path/filepath" | ||
) | ||
|
||
func writeVersionFile(path string) (string, error) { | ||
version := troubleshootv1beta1.SupportBundleVersion{ | ||
ApiVersion: "troubleshoot.replicated.com/v1beta1", | ||
Kind: "SupportBundle", | ||
LayoutVersion: "0.0.1", | ||
} | ||
b, err := yaml.Marshal(version) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
filename := filepath.Join(path, "version.yaml") | ||
err = ioutil.WriteFile(filename, b, 0644) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filename, nil | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package v1beta1 | ||
|
||
type SupportBundleVersion struct { | ||
ApiVersion string `json:"apiVersion" yaml:"apiVersion"` | ||
Kind string `json:"kind" yaml:"kind"` | ||
LayoutVersion string `json:"layoutVersion" yaml:"layoutVersion"` | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package convert | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type FuncError struct { | ||
Name string // Name of function. | ||
Err error // Pre-formatted error. | ||
} | ||
|
||
func (e FuncError) Error() string { | ||
return e.Err.Error() | ||
} | ||
|
||
// Panic will panic with a recoverable error. | ||
func Panic(name string, err error) error { | ||
panic(Error(name, err)) | ||
} | ||
|
||
// Error will wrap a template error in an ExecError causing template.Execute to recover. | ||
func Error(name string, err error) error { | ||
return FuncError{ | ||
Name: name, | ||
Err: err, | ||
} | ||
} | ||
|
||
func IsFuncError(err error) bool { | ||
_, ok := errors.Cause(err).(FuncError) | ||
return ok | ||
} |
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,107 @@ | ||
package convert | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
multierror "github.com/hashicorp/go-multierror" | ||
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze" | ||
) | ||
|
||
type Meta struct { | ||
Name string `json:"name,omitempty" yaml:"name,omitempty" hcl:"name,omitempty"` | ||
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty" hcl:"labels,omitempty"` | ||
} | ||
|
||
const ( | ||
SeverityError Severity = "error" | ||
SeverityWarn Severity = "warn" | ||
SeverityInfo Severity = "info" | ||
SeverityDebug Severity = "debug" | ||
) | ||
|
||
type Severity string | ||
|
||
type Insight struct { | ||
Meta `json:",inline" yaml:",inline" hcl:",inline"` | ||
|
||
Primary string `json:"primary" yaml:"primary" hcl:"primary"` | ||
Detail string `json:"detail" yaml:"detail" hcl:"detail"` | ||
Severity Severity `json:"severity,omitempty" yaml:"severity,omitempty" hcl:"severity,omitempty"` | ||
} | ||
|
||
type Result struct { | ||
Meta `json:",inline" yaml:",inline" hcl:",inline"` | ||
|
||
Insight *Insight `json:"insight" yaml:"insight" hcl:"insight"` | ||
Severity Severity `json:"severity" yaml:"severity" hcl:"severity"` | ||
AnalyzerSpec string `json:"analyzerSpec" yaml:"analyzerSpec" hcl:"analyzerSpec"` | ||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty" hcl:"variables,omitempty"` | ||
Error string `json:"error,omitempty" yaml:"error,omitempty" hcl:"error,omitempty"` | ||
} | ||
|
||
func (m *Insight) Render(data interface{}) (*Insight, error) { | ||
var multiErr *multierror.Error | ||
var err error | ||
built := &Insight{ | ||
Meta: m.Meta, | ||
Severity: m.Severity, | ||
} | ||
built.Primary, err = String(m.Primary, data) | ||
multiErr = multierror.Append(multiErr, errWrap(err, "build primary")) | ||
built.Detail, err = String(m.Detail, data) | ||
multiErr = multierror.Append(multiErr, errWrap(err, "build detail")) | ||
return built, multiErr.ErrorOrNil() | ||
} | ||
|
||
func errWrap(err error, text string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
return fmt.Errorf("%s: %v", text, err) | ||
} | ||
|
||
func FromAnalyzerResult(input []*analyze.AnalyzeResult) []*Result { | ||
reg := regexp.MustCompile("[^a-zA-Z0-9]+") | ||
|
||
result := make([]*Result, 0) | ||
for _, i := range input { | ||
name := reg.ReplaceAllString(strings.ToLower(i.Title), ".") | ||
r := &Result{ | ||
Meta: Meta{ | ||
Name: name, | ||
Labels: map[string]string{ | ||
"desiredPosition": "1", | ||
"iconKey": "gray_checkmark", | ||
}, | ||
}, | ||
Insight: &Insight{ | ||
Meta: Meta{ | ||
Name: name, | ||
Labels: map[string]string{ | ||
"iconKey": "gray_checkmark", | ||
}, | ||
}, | ||
Primary: i.Title, | ||
Detail: i.Message, | ||
}, | ||
AnalyzerSpec: "", | ||
Variables: map[string]interface{}{}, | ||
} | ||
if i.IsFail { | ||
r.Severity = SeverityError | ||
r.Insight.Severity = SeverityError | ||
r.Error = i.Message | ||
} else if i.IsWarn { | ||
r.Severity = SeverityWarn | ||
r.Insight.Severity = SeverityWarn | ||
} else if i.IsPass { | ||
r.Severity = SeverityDebug | ||
r.Insight.Severity = SeverityDebug | ||
} | ||
result = append(result, r) | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.