Skip to content

Commit

Permalink
Merge pull request #1574 from criblio/bug/cli-non-json-in-stdout
Browse files Browse the repository at this point in the history
Print cli command errors to stderr instead of stdout
  • Loading branch information
iapaddler authored Jul 6, 2023
2 parents ad00444 + 0a08e30 commit b688d4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
8 changes: 3 additions & 5 deletions cli/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ be set to sockets with unix:///var/run/mysock, tcp://hostname:port, udp://hostna
scope attach --rootdir /path/to/host/root/proc/<hostpid>/root 1000
scope attach --payloads 2000`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
internal.InitConfig()
rc.Rootdir, _ = cmd.Flags().GetString("rootdir")
inspectFlag, _ := cmd.Flags().GetBool("inspect")
Expand Down Expand Up @@ -81,11 +81,11 @@ be set to sockets with unix:///var/run/mysock, tcp://hostname:port, udp://hostna

procs, err := util.HandleInputArg(id, "", rc.Rootdir, true, true, true, false)
if err != nil {
return err
util.ErrAndExit("Attach failure: %v", err)
}

if len(procs) == 0 {
return errNoScopedProcs
util.ErrAndExit("Attach failure: no matching processes found")
}

pid := procs[0].Pid // we told HandleInputArg above that we wanted to choose only one proc
Expand Down Expand Up @@ -137,8 +137,6 @@ be set to sockets with unix:///var/run/mysock, tcp://hostname:port, udp://hostna
fmt.Println(string(cfg))
}
}

return nil
},
}

Expand Down
18 changes: 9 additions & 9 deletions cli/cmd/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var detachCmd = &cobra.Command{
scope detach --rootdir /path/to/host/root
scope detach --all --rootdir /path/to/host/root/proc/<hostpid>/root`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
internal.InitConfig()
all, _ := cmd.Flags().GetBool("all")
wait, _ := cmd.Flags().GetBool("wait")
Expand All @@ -50,25 +50,27 @@ var detachCmd = &cobra.Command{

procs, err := util.HandleInputArg(id, "", rc.Rootdir, !all, true, false, false)
if err != nil {
return err
util.ErrAndExit("Detach failure: %v", err)
}

if len(procs) == 0 {
return errNoScopedProcs
util.ErrAndExit("Detach failure: %v", errNoScopedProcs)
}
if len(procs) == 1 {
err = rc.Detach(procs[0].Pid)
if err = rc.Detach(procs[0].Pid); err != nil {
util.ErrAndExit("Detach failure: %v", err)
}
// Simple approach for now
// Later: check for detach then resume
if wait {
time.Sleep(11 * time.Second) // detach uses dynamic config (<10s)
}
return err
return
}
// len(procs) is > 1
if !util.Confirm(fmt.Sprintf("Are your sure you want to detach from all of these processes?")) {
fmt.Println("info: canceled")
return nil
return
}

errors := false
Expand All @@ -79,16 +81,14 @@ var detachCmd = &cobra.Command{
}
}
if errors {
return errDetachingMultiple
util.ErrAndExit("Detach failure: %v", errDetachingMultiple)
}

// Simple approach for now
// Later: check for detach then resume
if wait {
time.Sleep(11 * time.Second) // detach uses dynamic config (<10s)
}

return nil
},
}

Expand Down
12 changes: 5 additions & 7 deletions cli/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var inspectCmd = &cobra.Command{
scope inspect --all --rootdir /path/to/host/root
scope inspect --all --rootdir /path/to/host/root/proc/<hostpid>/root`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
internal.InitConfig()
all, _ := cmd.Flags().GetBool("all")
rootdir, _ := cmd.Flags().GetString("rootdir")
Expand Down Expand Up @@ -57,17 +57,17 @@ var inspectCmd = &cobra.Command{

procs, err := util.HandleInputArg(id, "", rootdir, !all, false, false, false)
if err != nil {
return err
util.ErrAndExit("Inspect failure: %v", err)
}

if len(procs) == 0 {
return errNoScopedProcs
util.ErrAndExit("Inspect failure: %v", errNoScopedProcs)
}
if len(procs) == 1 {
pidCtx.Pid = procs[0].Pid
iout, _, err := inspect.InspectProcess(*pidCtx)
if err != nil {
return err
util.ErrAndExit("Inspect failure: %v", err)
}
iouts = append(iouts, iout)
} else { // len(procs) > 1
Expand All @@ -82,7 +82,7 @@ var inspectCmd = &cobra.Command{
iouts = append(iouts, iout)
}
if errors {
return errInspectingMultiple
util.ErrAndExit("Inspect failure: %v", errInspectingMultiple)
}
}

Expand All @@ -109,8 +109,6 @@ var inspectCmd = &cobra.Command{
}
fmt.Println(string(cfgs))
}

return nil
},
}

Expand Down
19 changes: 11 additions & 8 deletions cli/cmd/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var rulesCmd = &cobra.Command{
scope rules --add firefox --rootdir /path/to/host/root
scope rules --remove chromium`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
internal.InitConfig()
rc.Rootdir, _ = cmd.Flags().GetString("rootdir")
jsonOut, _ := cmd.Flags().GetBool("json")
Expand Down Expand Up @@ -97,7 +97,7 @@ var rulesCmd = &cobra.Command{
// Retrieve an existing rules file
raw, rulesFile, err := rules.Retrieve(rc.Rootdir)
if err != nil {
return err
util.ErrAndExit("Rules failure: %v", err)
}

// In the case that no --add argument or --remove argument was provided
Expand All @@ -111,16 +111,15 @@ var rulesCmd = &cobra.Command{
// Marshal to json
jsonData, err := json.Marshal(rulesFile)
if err != nil {
util.Warn("Error marshaling JSON:%v", err)
return err
util.ErrAndExit("Error marshaling JSON: %v", err)
}
fmt.Println(string(jsonData))
} else {
content := string(raw)
fmt.Println(content)
}

return nil
return
}

// Below operations require root
Expand All @@ -132,12 +131,16 @@ var rulesCmd = &cobra.Command{

// Add a process to; or remove a process from the scope rules
if addProc != "" {
return rules.Add(rulesFile, addProc, procArg, sourceid, rc.Rootdir, rc, unixPath)
if err = rules.Add(rulesFile, addProc, procArg, sourceid, rc.Rootdir, rc, unixPath); err != nil {
util.ErrAndExit("Rules failure: %v", err)
}
} else if remProc != "" {
return rules.Remove(rulesFile, remProc, sourceid, rc.Rootdir, rc)
if err = rules.Remove(rulesFile, remProc, sourceid, rc.Rootdir, rc); err != nil {
util.ErrAndExit("Rules failure: %v", err)
}
}

return nil
return
},
}

Expand Down

0 comments on commit b688d4d

Please sign in to comment.