Skip to content

Commit

Permalink
linter fixes open-telemetry#4
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski committed Jul 23, 2023
1 parent a41c431 commit b2d53c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
2 changes: 0 additions & 2 deletions instrgen/driver/instrgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestInstrumentation(t *testing.T) {
rewriter := rewriters.BasicRewriter{
FilePattern: k, Replace: "yes", Pkg: "main", Fun: "main"}
analyzePackage(rewriter, "main", filePaths, nil, "", args)

}
fmt.Println(cwd)

Expand Down Expand Up @@ -86,5 +85,4 @@ func TestInstrumentation(t *testing.T) {
panic("not all files were compared")
}
}

}
47 changes: 18 additions & 29 deletions instrgen/driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -31,19 +30,20 @@ import (
"go.opentelemetry.io/contrib/instrgen/rewriters"
)

func usage() error {
func usage() {
fmt.Println("\nusage driver --command [file pattern] replace entrypoint")
fmt.Println("\tcommand:")
fmt.Println("\t\tinject (injects open telemetry calls into project code)")
fmt.Println("\t\tprune (prune open telemetry calls")
return nil
}

// Entry point function.
type EntryPoint struct {
Pkg string
FunName string
}

// Command passed to the compiler toolchain.
type InstrgenCmd struct {
ProjectPath string
FilePattern string
Expand All @@ -64,7 +64,6 @@ func isDirectory(path string) (bool, error) {
func executeCommand(command string, projectPath string, packagePattern string, replaceSource string, entryPoint string) error {
isDir, err := isDirectory(projectPath)
if !isDir {
_ = usage()
return errors.New("[path to go project] argument must be directory")
}
if err != nil {
Expand All @@ -79,13 +78,13 @@ func executeCommand(command string, projectPath string, packagePattern string, r
file, _ := json.MarshalIndent(data, "", " ")
err = os.WriteFile("instrgen_cmd.json", file, 0644)
if err != nil {
log.Fatal(err)
fmt.Println(err)
return nil
}
cmd := exec.Command("go", "build", "-work", "-a", "-toolexec", "driver")
fmt.Println("invoke : " + cmd.String())
if err := cmd.Run(); err != nil {
log.Fatal(err)
fmt.Println(err)
}
return nil
case "--prune":
Expand All @@ -97,7 +96,7 @@ func executeCommand(command string, projectPath string, packagePattern string, r
cmd := exec.Command("go", "build", "-work", "-a", "-toolexec", "driver")
fmt.Println("invoke : " + cmd.String())
if err := cmd.Run(); err != nil {
log.Fatal(err)
fmt.Println(err)
}
return nil
default:
Expand All @@ -107,7 +106,6 @@ func executeCommand(command string, projectPath string, packagePattern string, r

func checkArgs(args []string) error {
if len(args) < 4 {
_ = usage()
return errors.New("wrong arguments")
}
return nil
Expand Down Expand Up @@ -149,15 +147,15 @@ func createFile(name string) (*os.File, error) {

func analyzePackage(rewriter alib.PackageRewriter, pkg string, filePaths map[string]int, trace *os.File, destPath string, args []string) []string {
fset := token.NewFileSet()
trace.WriteString(rewriter.Id() + " pkg:" + pkg + ":" + destPath)
trace.WriteString("\n")
_, err := trace.WriteString(rewriter.Id() + " pkg:" + pkg + ":" + destPath + "\n")
if err != nil {
return nil
}
extraFilesWritten := false
for filePath, index := range filePaths {
file, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)

if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
// TODO handle that
continue
}
if rewriter.Inject(pkg, filePath) {
Expand All @@ -167,50 +165,39 @@ func analyzePackage(rewriter alib.PackageRewriter, pkg string, filePaths map[str
var out *os.File
out, err = createFile(fset.File(file.Pos()).Name() + "tmp")
if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
continue
}
err = printer.Fprint(out, fset, file)
if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
continue
}
oldFileName := fset.File(file.Pos()).Name() + "tmp"
newFileName := fset.File(file.Pos()).Name()
err = os.Rename(oldFileName, newFileName)
if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
continue
}
} else {
filename := filepath.Base(filePath)
out, err := createFile(destPath + "/" + filename)
if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
continue
}
err = printer.Fprint(out, fset, file)
if err != nil {
trace.WriteString(err.Error())
trace.WriteString("\n")
continue
}
args[index] = destPath + "/" + filename
}
if !extraFilesWritten {
files := rewriter.WriteExtraFiles(pkg, destPath)
if files != nil && len(files) > 0 {
if len(files) > 0 {
args = append(args, files...)
}
extraFilesWritten = true
}
}
}

return args
}

Expand Down Expand Up @@ -268,7 +255,8 @@ func executeCommandProxy(cmdName string) {
}
err = executeCommand(os.Args[1], ".", os.Args[2], replace, os.Args[4])
if err != nil {
log.Fatal(err)
fmt.Println(err)
return
}
}

Expand All @@ -289,7 +277,6 @@ func main() {
case "--rootfunctions":
executeCommandProxy(cmdName)
return

}
if len(args) > 0 {
executePass(args[0:])
Expand All @@ -300,13 +287,15 @@ func main() {
}
content, err := os.ReadFile("./instrgen_cmd.json")
if err != nil {
log.Fatal("Error when opening file: ", err)
fmt.Println("Error when opening file: ", err)
return
}

var instrgenCfg InstrgenCmd
err = json.Unmarshal(content, &instrgenCfg)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
fmt.Println("Error during Unmarshal(): ", err)
return
}

var rewriterS []alib.PackageRewriter
Expand Down

0 comments on commit b2d53c0

Please sign in to comment.