Skip to content

Commit

Permalink
refactor: simplify code to return the fastpath first
Browse files Browse the repository at this point in the history
What I did: simplify some codes; and use return fast to eliminate some else block, since I think this will make codes ident less to be more readable.

Fixes clearcontainers#623

Signed-off-by: Allen Sun <[email protected]>
  • Loading branch information
allencloud committed Sep 25, 2017
1 parent 535be19 commit 1378b68
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 57 deletions.
25 changes: 7 additions & 18 deletions cc-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ func findAnchoredString(haystack, needle string) bool {
// Ensure the search string is anchored
pattern := regexp.MustCompile(`\b` + needle + `\b`)

matched := pattern.MatchString(haystack)

if matched {
return true
}

return false
return pattern.MatchString(haystack)
}

func getCPUFlags(cpuinfo string) string {
Expand All @@ -140,11 +134,7 @@ func haveKernelModule(module string) bool {
// Now, check if the module is unloaded, but available
cmd := exec.Command(modInfoCmd, module)
err := cmd.Run()
if err == nil {
return true
}

return false
return err == nil
}

func checkCPU(tag, cpuinfo string, attribs map[string]string) error {
Expand All @@ -154,11 +144,10 @@ func checkCPU(tag, cpuinfo string, attribs map[string]string) error {

for attrib, desc := range attribs {
found := findAnchoredString(cpuinfo, attrib)
if found {
ccLog.Infof("Found CPU %v %q (%s)", tag, desc, attrib)
} else {
if !found {
return fmt.Errorf("CPU does not have required %v: %q (%s)", tag, desc, attrib)
}
ccLog.Infof("Found CPU %v %q (%s)", tag, desc, attrib)
}

return nil
Expand Down Expand Up @@ -193,9 +182,7 @@ func checkKernelModules(modules map[string]kernelModule) error {

value = strings.TrimRight(value, "\n\r")

if value == expected {
ccLog.Infof("Kernel module %q parameter %q has correct value", details.desc, param)
} else {
if value != expected {
msg := fmt.Sprintf("kernel module %q parameter %q has value %q (expected %q)", details.desc, param, value, expected)

// this option is not required when
Expand All @@ -207,6 +194,8 @@ func checkKernelModules(modules map[string]kernelModule) error {

return errors.New(msg)
}

ccLog.Infof("Kernel module %q parameter %q has correct value", details.desc, param)
}
}

Expand Down
10 changes: 4 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,11 @@ func getDefaultConfigFile() (string, error) {

for _, file := range getDefaultConfigFilePaths() {
resolved, err := resolvePath(file)
if err != nil {
s := fmt.Sprintf("config file %q unresolvable: %v", file, err)
errs = append(errs, s)
continue
if err == nil {
return resolved, nil
}

return resolved, nil
s := fmt.Sprintf("config file %q unresolvable: %v", file, err)
errs = append(errs, s)
}

return "", errors.New(strings.Join(errs, ", "))
Expand Down
26 changes: 13 additions & 13 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ func execute(context *cli.Context) error {
return err
}

if !params.detach {
p, err := os.FindProcess(process.Pid)
if err != nil {
return err
}
if params.detach {
return nil
}

ps, err := p.Wait()
if err != nil {
return fmt.Errorf("Process state %s, container info %+v: %v",
ps.String(), status, err)
}
p, err := os.FindProcess(process.Pid)
if err != nil {
return err
}

// Exit code has to be forwarded in this case.
return cli.NewExitError("", ps.Sys().(syscall.WaitStatus).ExitStatus())
ps, err := p.Wait()
if err != nil {
return fmt.Errorf("Process state %s, container info %+v: %v",
ps.String(), status, err)
}

return nil
// Exit code has to be forwarded in this case.
return cli.NewExitError("", ps.Sys().(syscall.WaitStatus).ExitStatus())
}
40 changes: 20 additions & 20 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,30 @@ func run(containerID, bundle, console, consoleSocket, pidFile string, detach boo
return err
}

if !detach {
containers := pod.GetAllContainers()
if len(containers) == 0 {
return fmt.Errorf("There are no containers running in the pod: %s", pod.ID())
}
if detach {
return nil
}

p, err := os.FindProcess(containers[0].GetPid())
if err != nil {
return err
}
containers := pod.GetAllContainers()
if len(containers) == 0 {
return fmt.Errorf("There are no containers running in the pod: %s", pod.ID())
}

ps, err := p.Wait()
if err != nil {
return fmt.Errorf("Process state %s: %s", ps.String(), err)
}
p, err := os.FindProcess(containers[0].GetPid())
if err != nil {
return err
}

// delete container's resources
if err := delete(pod.ID(), true); err != nil {
return err
}
ps, err := p.Wait()
if err != nil {
return fmt.Errorf("Process state %s: %s", ps.String(), err)
}

//runtime should forward container exit code to the system
return cli.NewExitError("", ps.Sys().(syscall.WaitStatus).ExitStatus())
// delete container's resources
if err := delete(pod.ID(), true); err != nil {
return err
}

return nil
//runtime should forward container exit code to the system
return cli.NewExitError("", ps.Sys().(syscall.WaitStatus).ExitStatus())
}

0 comments on commit 1378b68

Please sign in to comment.