1
0

refactor: move commands to cmd/

This commit is contained in:
dump_stack() 2024-02-17 22:38:43 +00:00
parent 1b3e23d188
commit 4e92950929
Signed by: dump_stack
GPG Key ID: C9905BA72B5E02BB
13 changed files with 66 additions and 60 deletions

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"fmt" "fmt"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"database/sql" "database/sql"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"context" "context"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"fmt" "fmt"

15
cmd/globals.go Normal file
View File

@ -0,0 +1,15 @@
package cmd
import (
"net/url"
"code.dumpstack.io/tools/out-of-tree/config"
)
type Globals struct {
Config config.OutOfTree `help:"path to out-of-tree configuration" default:"~/.out-of-tree/out-of-tree.toml"`
WorkDir string `help:"path to work directory" default:"./" type:"path"`
CacheURL url.URL
}

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"errors" "errors"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"errors" "errors"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"database/sql" "database/sql"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"fmt" "fmt"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"bufio" "bufio"
@ -30,6 +30,22 @@ import (
"code.dumpstack.io/tools/out-of-tree/qemu" "code.dumpstack.io/tools/out-of-tree/qemu"
) )
type LevelWriter struct {
io.Writer
Level zerolog.Level
}
func (lw *LevelWriter) WriteLevel(l zerolog.Level, p []byte) (n int, err error) {
if l >= lw.Level {
return lw.Writer.Write(p)
}
return len(p), nil
}
var ConsoleWriter, FileWriter LevelWriter
var LogLevel zerolog.Level
type PewCmd struct { type PewCmd struct {
Max int64 `help:"test no more than X kernels" default:"100500"` Max int64 `help:"test no more than X kernels" default:"100500"`
Runs int64 `help:"runs per each kernel" default:"1"` Runs int64 `help:"runs per each kernel" default:"1"`
@ -649,8 +665,8 @@ func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup,
defer f.Close() defer f.Close()
slog := zerolog.New(zerolog.MultiLevelWriter( slog := zerolog.New(zerolog.MultiLevelWriter(
&consoleWriter, &ConsoleWriter,
&fileWriter, &FileWriter,
&zerolog.ConsoleWriter{ &zerolog.ConsoleWriter{
Out: f, Out: f,
FieldsExclude: []string{ FieldsExclude: []string{
@ -662,7 +678,7 @@ func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup,
}, },
)) ))
switch loglevel { switch LogLevel {
case zerolog.TraceLevel, zerolog.DebugLevel: case zerolog.TraceLevel, zerolog.DebugLevel:
slog = slog.With().Caller().Logger() slog = slog.With().Caller().Logger()
} }

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a AGPLv3 license // Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file. // (or later) that can be found in the LICENSE file.
package main package cmd
import ( import (
"crypto/sha1" "crypto/sha1"

67
main.go
View File

@ -6,9 +6,7 @@ package main
import ( import (
"fmt" "fmt"
"io"
"math/rand" "math/rand"
"net/url"
"os" "os"
"os/exec" "os/exec"
"runtime/debug" "runtime/debug"
@ -28,31 +26,24 @@ import (
_ "code.dumpstack.io/tools/out-of-tree/distro/ubuntu" _ "code.dumpstack.io/tools/out-of-tree/distro/ubuntu"
"code.dumpstack.io/tools/out-of-tree/cache" "code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/cmd"
"code.dumpstack.io/tools/out-of-tree/config" "code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/container" "code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/fs"
) )
type Globals struct {
Config config.OutOfTree `help:"path to out-of-tree configuration" default:"~/.out-of-tree/out-of-tree.toml"`
WorkDir string `help:"path to work directory" default:"./" type:"path"`
CacheURL url.URL
}
type CLI struct { type CLI struct {
Globals cmd.Globals
Pew PewCmd `cmd:"" help:"build, run, and test module/exploit"` Pew cmd.PewCmd `cmd:"" help:"build, run, and test module/exploit"`
Kernel KernelCmd `cmd:"" help:"manipulate kernels"` Kernel cmd.KernelCmd `cmd:"" help:"manipulate kernels"`
Debug DebugCmd `cmd:"" help:"debug environment"` Debug cmd.DebugCmd `cmd:"" help:"debug environment"`
Log LogCmd `cmd:"" help:"query logs"` Log cmd.LogCmd `cmd:"" help:"query logs"`
Pack PackCmd `cmd:"" help:"exploit pack test"` Pack cmd.PackCmd `cmd:"" help:"exploit pack test"`
Gen GenCmd `cmd:"" help:"generate .out-of-tree.toml skeleton"` Gen cmd.GenCmd `cmd:"" help:"generate .out-of-tree.toml skeleton"`
Image ImageCmd `cmd:"" help:"manage images"` Image cmd.ImageCmd `cmd:"" help:"manage images"`
Container ContainerCmd `cmd:"" help:"manage containers"` Container cmd.ContainerCmd `cmd:"" help:"manage containers"`
Distro DistroCmd `cmd:"" help:"distro-related helpers"` Distro cmd.DistroCmd `cmd:"" help:"distro-related helpers"`
Version VersionFlag `name:"version" help:"print version information and quit"` Version VersionFlag `name:"version" help:"print version information and quit"`
@ -92,22 +83,6 @@ func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
return nil return nil
} }
type LevelWriter struct {
io.Writer
Level zerolog.Level
}
func (lw *LevelWriter) WriteLevel(l zerolog.Level, p []byte) (n int, err error) {
if l >= lw.Level {
return lw.Writer.Write(p)
}
return len(p), nil
}
var consoleWriter, fileWriter LevelWriter
var loglevel zerolog.Level
func main() { func main() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -126,34 +101,34 @@ func main() {
switch cli.LogLevel { switch cli.LogLevel {
case "trace": case "trace":
loglevel = zerolog.TraceLevel cmd.LogLevel = zerolog.TraceLevel
case "debug": case "debug":
loglevel = zerolog.DebugLevel cmd.LogLevel = zerolog.DebugLevel
case "info": case "info":
loglevel = zerolog.InfoLevel cmd.LogLevel = zerolog.InfoLevel
case "warn": case "warn":
loglevel = zerolog.WarnLevel cmd.LogLevel = zerolog.WarnLevel
case "error": case "error":
loglevel = zerolog.ErrorLevel cmd.LogLevel = zerolog.ErrorLevel
} }
consoleWriter = LevelWriter{Writer: zerolog.NewConsoleWriter( cmd.ConsoleWriter = cmd.LevelWriter{Writer: zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) { func(w *zerolog.ConsoleWriter) {
w.Out = os.Stderr w.Out = os.Stderr
}, },
), ),
Level: loglevel, Level: cmd.LogLevel,
} }
fileWriter = LevelWriter{Writer: &lumberjack.Logger{ cmd.FileWriter = cmd.LevelWriter{Writer: &lumberjack.Logger{
Filename: config.File("logs/out-of-tree.log"), Filename: config.File("logs/out-of-tree.log"),
}, },
Level: zerolog.TraceLevel, Level: zerolog.TraceLevel,
} }
log.Logger = log.Output(zerolog.MultiLevelWriter( log.Logger = log.Output(zerolog.MultiLevelWriter(
&consoleWriter, &cmd.ConsoleWriter,
&fileWriter, &cmd.FileWriter,
)) ))
log.Trace().Msg("start out-of-tree") log.Trace().Msg("start out-of-tree")