Skip to content

Commit

Permalink
fix: use RunE instead Run in cobra.Command struct (#27)
Browse files Browse the repository at this point in the history
* fix: use RunE instead Run in cobra.Command struct

Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>

* Update cmd/analyze.go

Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>

* Update cmd/hunt.go

Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>

* fix

Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>

---------

Signed-off-by: Alessio Greggi <ale_grey_91@hotmail.it>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
  • Loading branch information
alegrey91 and ccoVeille authored Aug 6, 2024
1 parent 91b2e5a commit baa3958
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
12 changes: 5 additions & 7 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,20 @@ var analyzeCmd = &cobra.Command{
Long: `
`,
Example: " harpoon analyze --exclude vendor/ /path/to/repo/",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if exclude != "" {
excludedPaths = strings.Split(exclude, ",")
}

file, err := os.Open("go.mod")
if err != nil {
fmt.Printf("failed to open %s: %v\n", "go.mod", err)
return
return fmt.Errorf("failed to open go.mod: %w", err)
}
defer file.Close()

moduleName, err := analyzer.GetModuleName(file)
if err != nil {
fmt.Println("module name not found in go.mod")
return
return fmt.Errorf("error module name not found in go.mod: %w", err)
}

symbolsList := metadata.NewSymbolsList()
Expand Down Expand Up @@ -129,12 +127,12 @@ var analyzeCmd = &cobra.Command{
// store to file
file, err = os.Create(".harpoon.yml")
if err != nil {
fmt.Printf("failed to create symbols list file")
return
return fmt.Errorf("failed to create symbols list file: %w", err)
}
mw := io.Writer(file)
fmt.Fprintln(mw, symbolsList.String())
fmt.Println("file .harpoon.yml is ready")
return nil
},
}

Expand Down
18 changes: 8 additions & 10 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ var buildCmd = &cobra.Command{
Long: `
`,
Example: " harpoon build",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
files, err := os.ReadDir(inputDirectory)
if err != nil {
fmt.Printf("error reading dir content: %v", err)
return
return fmt.Errorf("error reading dir content: %w", err)
}

syscalls := make([]string, 0)
Expand All @@ -50,8 +49,7 @@ var buildCmd = &cobra.Command{

file, err := os.Open(inputDirectory + "/" + fileObj.Name())
if err != nil {
fmt.Printf("error opening file %s: %v", file.Name(), err)
return
return fmt.Errorf("error opening file %s: %w", file.Name(), err)
}
defer file.Close()

Expand All @@ -71,26 +69,26 @@ var buildCmd = &cobra.Command{

profile, err := seccomp.BuildProfile(syscalls)
if err != nil {
fmt.Printf("error building seccomp profile: %v", err)
return fmt.Errorf("error building seccomp profile: %w", err)
}

if saveProfile {
profileFile, err := os.Create(profileName)
if err != nil {
fmt.Printf("error creating seccomp file %s: %v\n", profileFile.Name(), err)
return
return fmt.Errorf("error creating seccomp file %s: %w", profileFile.Name(), err)
}
defer profileFile.Close()

if err := profileFile.Chmod(0644); err != nil {
fmt.Printf("error setting permissions to %s: %v\n", profileFile.Name(), err)
return
return fmt.Errorf("error setting permissions to %s: %w", profileFile.Name(), err)
}
// write to file
fmt.Fprintln(profileFile, profile)
} else {
fmt.Println(profile)
}

return nil
},
}

Expand Down
17 changes: 9 additions & 8 deletions cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,22 @@ var huntCmd = &cobra.Command{
Long: `
`,
Example: " harpoon hunt --file .harpoon.yaml",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
file, err := os.Open(harpoonFile)
if err != nil {
fmt.Printf("failed to open %s: %v\n", harpoonFile, err)
return
return fmt.Errorf("failed to open %s: %w", harpoonFile, err)
}
defer file.Close()

byteValue, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Failed to read file: %s", err)
return fmt.Errorf("failed to read file %q: %w", file.Name(), err)
}

// Unmarshal the JSON data into the struct
var analysisReport meta.SymbolsList
if err := yaml.Unmarshal(byteValue, &analysisReport); err != nil {
fmt.Printf("Failed to unmarshal YAML: %v", err)
return fmt.Errorf("failed to unmarshal YAML in %q: %w", harpoonFile, err)
}
//fmt.Println(analysisReport)

Expand All @@ -73,17 +72,19 @@ var huntCmd = &cobra.Command{
for _, functionSymbol := range symbolsOrigins.Symbols {
syscalls, err := captor.Capture(functionSymbol, captureArgs, opts)
if err != nil {
fmt.Printf("error capturing syscall: %v", err)
os.Exit(1)
return fmt.Errorf("error capturing syscall: %w", err)
}

saveOpts := writer.WriteOptions{
Save: save,
Directory: directory,
}
writer.Write(syscalls, functionSymbol, saveOpts)
if err = writer.Write(syscalls, functionSymbol, saveOpts); err != nil {
return fmt.Errorf("error writing seccomp profile: %w", err)
}
}
}
return nil
},
}

Expand Down

0 comments on commit baa3958

Please sign in to comment.