github.com/coreos/mantle@v0.13.0/system/ns/exec.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ns
    16  
    17  import (
    18  	"github.com/vishvananda/netns"
    19  
    20  	"github.com/coreos/mantle/system/exec"
    21  )
    22  
    23  type Cmd struct {
    24  	*exec.ExecCmd
    25  	NsHandle netns.NsHandle
    26  }
    27  
    28  func Command(ns netns.NsHandle, name string, arg ...string) *Cmd {
    29  	return &Cmd{
    30  		ExecCmd:  exec.Command(name, arg...),
    31  		NsHandle: ns,
    32  	}
    33  }
    34  
    35  func (cmd *Cmd) CombinedOutput() ([]byte, error) {
    36  	nsExit, err := Enter(cmd.NsHandle)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	defer nsExit()
    41  
    42  	return cmd.ExecCmd.CombinedOutput()
    43  }
    44  
    45  func (cmd *Cmd) Output() ([]byte, error) {
    46  	nsExit, err := Enter(cmd.NsHandle)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	defer nsExit()
    51  
    52  	return cmd.ExecCmd.Output()
    53  }
    54  
    55  func (cmd *Cmd) Run() error {
    56  	nsExit, err := Enter(cmd.NsHandle)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	defer nsExit()
    61  
    62  	return cmd.ExecCmd.Run()
    63  }
    64  
    65  func (cmd *Cmd) Start() error {
    66  	nsExit, err := Enter(cmd.NsHandle)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	defer nsExit()
    71  
    72  	return cmd.ExecCmd.Start()
    73  }