Skip to content

Commit

Permalink
fix: dont affect original go build command (#224)
Browse files Browse the repository at this point in the history
* fix: dont affect original go build command

* update readme and usage
  • Loading branch information
y1yang0 authored Dec 13, 2024
1 parent fec9612 commit 32af429
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 225 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The configuration for the tool can be set by the following command:

```bash
$ otel set -verbose # print verbose logs
$ otel set -log=/path/to/file.log # set log file
$ otel set -debug # enable debug mode
$ otel set -debug -verbose -rule=custom.json # set multiple configs
$ otel set -disabledefault -rule=custom.json # disable default rules, use custom rules only
Expand Down
7 changes: 7 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ This guide provides a detailed overview of configuring and using the otel tool e
## Configuration
The primary method of configuring the tool is through the `otel set` command. This command allows you to specify various settings tailored to your needs:

Logging: Set a custom log file to store logs generated by the tool.
```bash
$ otel set -log=/path/to/file.log
```
The default log file is `.otel-build/preprocess/debug.log`. You can
log to stdout by setting `-log=/dev/stdout`.

Verbose Logging: Enable verbose logging to receive detailed output from the tool, which is helpful for troubleshooting and understanding the tool's processes.
```bash
$ otel set -verbose
Expand Down
2 changes: 1 addition & 1 deletion test/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestFlags(t *testing.T) {
RunGoBuild(t, "")
}

func TestFlagConfigOverwriteNo(t *testing.T) {
func TestFlagEnvOverwrite(t *testing.T) {
UseApp(AppName)

RunSet(t, "-verbose=false")
Expand Down
23 changes: 8 additions & 15 deletions test/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func runCmd(args []string) *exec.Cmd {
}

func ReadInstrumentLog(t *testing.T, fileName string) string {
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, fileName)
path := filepath.Join(shared.TempBuildDir, util.PInstrument, fileName)
content, err := util.ReadFile(path)
if err != nil {
t.Fatal(err)
Expand All @@ -64,7 +64,7 @@ func ReadInstrumentLog(t *testing.T, fileName string) string {
}

func ReadPreprocessLog(t *testing.T, fileName string) string {
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, fileName)
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, fileName)
content, err := util.ReadFile(path)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -102,7 +102,6 @@ func RunSet(t *testing.T, args ...string) {

func RunGoBuild(t *testing.T, args ...string) {
util.Assert(pwd != "", "pwd is empty")
RunSet(t, "-debuglog")
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path}, args...))
err := cmd.Run()
Expand All @@ -113,17 +112,14 @@ func RunGoBuild(t *testing.T, args ...string) {
t.Log("\n\n\n")
t.Log(stderr)
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
log2 := ReadInstrumentLog(t, shared.DebugLogFile)
text := fmt.Sprintf("failed to run instrument: %v\n", err)
text += fmt.Sprintf("preprocess: %v\n", log1)
text += fmt.Sprintf("instrument: %v\n", log2)
text += fmt.Sprintf("text: %v\n", log1)
t.Fatal(text)
}
}

func RunGoBuildWithEnv(t *testing.T, envs []string, args ...string) {
util.Assert(pwd != "", "pwd is empty")
RunSet(t, "-debuglog")
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path}, args...))
cmd.Env = append(cmd.Env, envs...)
Expand All @@ -135,17 +131,14 @@ func RunGoBuildWithEnv(t *testing.T, envs []string, args ...string) {
t.Log("\n\n\n")
t.Log(stderr)
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
log2 := ReadInstrumentLog(t, shared.DebugLogFile)
text := fmt.Sprintf("failed to run instrument: %v\n", err)
text += fmt.Sprintf("preprocess: %v\n", log1)
text += fmt.Sprintf("instrument: %v\n", log2)
text += fmt.Sprintf("text: %v\n", log1)
t.Fatal(text)
}
}

func RunGoBuildFallible(t *testing.T, args ...string) {
util.Assert(pwd != "", "pwd is empty")
RunSet(t, "-debuglog")
path := filepath.Join(filepath.Dir(pwd), getExecName())
cmd := runCmd(append([]string{path}, args...))
err := cmd.Run()
Expand Down Expand Up @@ -219,25 +212,25 @@ func ExpectStderrContains(t *testing.T, expect string) {
}

func ExpectInstrumentContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, log)
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
content := readLog(t, path)
ExpectContains(t, content, rule)
}

func ExpectInstrumentNotContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, log)
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
content := readLog(t, path)
ExpectNotContains(t, content, rule)
}

func ExpectPreprocessContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
content := readLog(t, path)
ExpectContains(t, content, rule)
}

func ExpectPreprocessNotContains(t *testing.T, log string, rule string) {
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
content := readLog(t, path)
ExpectNotContains(t, content, rule)
}
Expand Down
44 changes: 20 additions & 24 deletions tool/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -49,26 +48,25 @@ Command:
`

func printUsage() {
usage = strings.ReplaceAll(usage, "{}", config.GetToolName())
usage = strings.ReplaceAll(usage, "{}", util.GetToolName())
fmt.Print(usage)
}

func initLogs(names ...string) error {
for _, name := range names {
path := shared.GetTempBuildDirWith(name)
logPath := filepath.Join(path, shared.DebugLogFile)
_, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("failed to create log file: %w", err)
}
func initLog() error {
name := util.PPreprocess
path := shared.GetTempBuildDirWith(name)
logPath := filepath.Join(path, shared.DebugLogFile)
_, err := os.Create(logPath)
if err != nil {
return fmt.Errorf("failed to create log file: %w", err)
}
return nil
}

func initTempDir() error {
// All temp directories are prepared before, instrument phase should not
// create any new directories.
if shared.GetRunPhase() == shared.PInstrument {
if util.GetRunPhase() == util.PInstrument {
return nil
}

Expand All @@ -83,14 +81,14 @@ func initTempDir() error {
// we always recreate the preprocess and instrument directories, but only
// create the configure directory if it does not exist. This is because
// the configure directory can be used across multiple runs.
exist, _ := util.PathExists(shared.GetTempBuildDirWith(shared.PConfigure))
exist, _ := util.PathExists(shared.GetTempBuildDirWith(util.PConfigure))
if !exist {
err := os.MkdirAll(shared.GetTempBuildDirWith(shared.PConfigure), 0777)
err := os.MkdirAll(shared.GetTempBuildDirWith(util.PConfigure), 0777)
if err != nil {
return fmt.Errorf("failed to make log directory: %w", err)
}
}
for _, subdir := range []string{shared.PPreprocess, shared.PInstrument} {
for _, subdir := range []string{util.PPreprocess, util.PInstrument} {
exist, _ = util.PathExists(shared.GetTempBuildDirWith(subdir))
if exist {
err := os.RemoveAll(shared.GetTempBuildDirWith(subdir))
Expand All @@ -114,35 +112,33 @@ func initEnv() error {
switch {
case os.Args[1] == SubcommandSet:
// otel set?
shared.SetRunPhase(shared.PConfigure)
util.SetRunPhase(util.PConfigure)
case strings.HasSuffix(os.Args[1], SubcommandGo):
// otel go build?
shared.SetRunPhase(shared.PPreprocess)
util.SetRunPhase(util.PPreprocess)
case os.Args[1] == SubcommandRemix:
// otel remix?
shared.SetRunPhase(shared.PInstrument)
util.SetRunPhase(util.PInstrument)
default:
// do nothing
}

log.SetPrefix("[" + shared.GetRunPhase().String() + "] ")

// Create temp build directory
err := initTempDir()
if err != nil {
return fmt.Errorf("failed to init temp dir: %w", err)
}

// Create log files under temp build directory
if shared.InPreprocess() {
err := initLogs(shared.PPreprocess, shared.PInstrument)
if util.InPreprocess() {
err := initLog()
if err != nil {
return fmt.Errorf("failed to init logs: %w", err)
}
}

// Prepare shared configuration
if shared.InPreprocess() || shared.InInstrument() {
if util.InPreprocess() || util.InInstrument() {
err = config.InitConfig()
if err != nil {
return fmt.Errorf("failed to init config: %w", err)
Expand All @@ -159,7 +155,7 @@ func main() {

err := initEnv()
if err != nil {
log.Printf("failed to init env: %v", err)
util.LogFatal("failed to init env: %v", err)
os.Exit(1)
}

Expand All @@ -177,7 +173,7 @@ func main() {
printUsage()
}
if err != nil {
log.Printf("failed to run command %s: %v", subcmd, err)
util.LogFatal("failed to run command %s: %v", subcmd, err)
os.Exit(1)
}
}
Loading

0 comments on commit 32af429

Please sign in to comment.