From e0c0d3a072c1efa83f7266345d0f0eed59c873c6 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sat, 17 Aug 2019 08:51:42 +0000 Subject: [PATCH] Refactor --- config/config.go | 14 ++++++++++++++ kernel.go | 2 +- log.go | 16 ++++++++-------- main.go | 8 ++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 8f91aa4..5ab5cb2 100644 --- a/config/config.go +++ b/config/config.go @@ -14,18 +14,21 @@ import ( "github.com/naoina/toml" ) +// KernelMask defines the kernel type KernelMask struct { DistroType DistroType DistroRelease string // 18.04/7.4.1708/9.1 ReleaseMask string } +// DockerName is returns stable name for docker container func (km KernelMask) DockerName() string { distro := strings.ToLower(km.DistroType.String()) release := strings.Replace(km.DistroRelease, ".", "__", -1) return fmt.Sprintf("out_of_tree_%s_%s", distro, release) } +// ArtifactType is the kernel module or exploit type ArtifactType int const ( @@ -37,6 +40,7 @@ func (at ArtifactType) String() string { return [...]string{"module", "exploit"}[at] } +// UnmarshalTOML is for support github.com/naoina/toml func (at *ArtifactType) UnmarshalTOML(data []byte) (err error) { stype := strings.Trim(string(data), `"`) stypelower := strings.ToLower(stype) @@ -50,6 +54,7 @@ func (at *ArtifactType) UnmarshalTOML(data []byte) (err error) { return } +// MarshalTOML is for support github.com/naoina/toml func (at ArtifactType) MarshalTOML() (data []byte, err error) { s := "" switch at { @@ -64,6 +69,7 @@ func (at ArtifactType) MarshalTOML() (data []byte, err error) { return } +// Artifact is for .out-of-tree.toml type Artifact struct { Name string Type ArtifactType @@ -89,6 +95,7 @@ func (ka Artifact) checkSupport(ki KernelInfo, km KernelMask) ( return } +// Supported returns true if given kernel is supported by artifact func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) { for _, km := range ka.SupportedKernels { supported, err = ka.checkSupport(ki, km) @@ -100,6 +107,7 @@ func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) { return } +// DistroType is enum with all supported distros type DistroType int const ( @@ -110,6 +118,7 @@ const ( var DistroTypeStrings = [...]string{"Ubuntu", "CentOS", "Debian"} +// NewDistroType is create new Distro object func NewDistroType(dType string) (dt DistroType, err error) { err = dt.UnmarshalTOML([]byte(dType)) return @@ -119,6 +128,7 @@ func (dt DistroType) String() string { return DistroTypeStrings[dt] } +// UnmarshalTOML is for support github.com/naoina/toml func (dt *DistroType) UnmarshalTOML(data []byte) (err error) { sDistro := strings.Trim(string(data), `"`) if strings.EqualFold(sDistro, "Ubuntu") { @@ -133,6 +143,7 @@ func (dt *DistroType) UnmarshalTOML(data []byte) (err error) { return } +// MarshalTOML is for support github.com/naoina/toml func (dt DistroType) MarshalTOML() (data []byte, err error) { s := "" switch dt { @@ -175,6 +186,7 @@ type KernelInfo struct { VmlinuxPath string } +// KernelConfig is the ~/.out-of-tree/kernels.toml configuration description type KernelConfig struct { Kernels []KernelInfo } @@ -190,6 +202,7 @@ func readFileAll(path string) (buf []byte, err error) { return } +// ReadKernelConfig is for read kernels.toml func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) { buf, err := readFileAll(path) if err != nil { @@ -204,6 +217,7 @@ func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) { return } +// ReadArtifactConfig is for read .out-of-tree.toml func ReadArtifactConfig(path string) (artifactCfg Artifact, err error) { buf, err := readFileAll(path) if err != nil { diff --git a/kernel.go b/kernel.go index d1c122f..b057b30 100644 --- a/kernel.go +++ b/kernel.go @@ -24,7 +24,7 @@ import ( "code.dumpstack.io/tools/out-of-tree/config" ) -var KERNELS_ALL int64 = math.MaxInt64 +const KERNELS_ALL int64 = math.MaxInt64 func kernelListHandler(kcfg config.KernelConfig) (err error) { if len(kcfg.Kernels) == 0 { diff --git a/log.go b/log.go index e07b74c..b5d03b2 100644 --- a/log.go +++ b/log.go @@ -88,7 +88,7 @@ func logHandler(db *sql.DB, path, tag string, num int, rate bool) (err error) { success := 0 for _, l := range les { if l.Test.Ok { - success += 1 + success++ } } @@ -179,21 +179,21 @@ func getStats(db *sql.DB, path, tag string) ( rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] - rs.All += 1 + rs.All++ if l.Build.Ok { - rs.BuildOK += 1 + rs.BuildOK++ } if l.Run.Ok { - rs.RunOK += 1 + rs.RunOK++ } if l.Test.Ok { - rs.TestOK += 1 + rs.TestOK++ } if l.KernelPanic { - rs.Panic += 1 + rs.Panic++ } if l.KilledByTimeout { - rs.Timeout += 1 + rs.Timeout++ } distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs @@ -202,7 +202,7 @@ func getStats(db *sql.DB, path, tag string) ( return } -func logJsonHandler(db *sql.DB, path, tag string) (err error) { +func logJSONHandler(db *sql.DB, path, tag string) (err error) { distros, err := getStats(db, path, tag) if err != nil { return diff --git a/main.go b/main.go index 633e615..fffe902 100644 --- a/main.go +++ b/main.go @@ -195,8 +195,8 @@ func main() { "Show all info for log entry with ID") logDumpID := logDumpCommand.Arg("ID", "").Required().Int() - logJsonCommand := logCommand.Command("json", "Generate json statistics") - logJsonTag := logJsonCommand.Flag("tag", "Filter tag").Required().String() + logJSONCommand := logCommand.Command("json", "Generate json statistics") + logJSONTag := logJSONCommand.Flag("tag", "Filter tag").Required().String() logMarkdownCommand := logCommand.Command("markdown", "Generate markdown statistics") logMarkdownTag := logMarkdownCommand.Flag("tag", "Filter tag").Required().String() @@ -283,8 +283,8 @@ func main() { err = logHandler(db, *path, *logTag, *logNum, *logRate) case logDumpCommand.FullCommand(): err = logDumpHandler(db, *logDumpID) - case logJsonCommand.FullCommand(): - err = logJsonHandler(db, *path, *logJsonTag) + case logJSONCommand.FullCommand(): + err = logJSONHandler(db, *path, *logJSONTag) case logMarkdownCommand.FullCommand(): err = logMarkdownHandler(db, *path, *logMarkdownTag) }