Exemple #1
0
// MyDockerID returns id of the current container the process is running within, if any
func MyDockerID() (string, error) {
	if _, err := os.Stat("/proc/self/cgroup"); os.IsNotExist(err) {
		return "", nil
	}
	output, exitStatus, err := util.ExecPipe(&util.Cmd{
		Args: []string{"/bin/bash", "-c", `cat /proc/self/cgroup | grep "docker" | sed s/\\//\\n/g | tail -1`},
	})
	if err != nil {
		return "", err
	}
	if exitStatus != 0 {
		return "", fmt.Errorf("Failed to obtain docker id due error: %s", output)
	}

	return strings.Trim(output, "\n"), nil
}
Exemple #2
0
func doGitCmd(dir string, args []string) (out string, err error) {
	cmd := &util.Cmd{
		Args: append([]string{"/usr/bin/git"}, args...),
		Dir:  dir,
	}

	out, exitCode, err := util.ExecPipe(cmd)
	if err != nil {
		return "", err
	}
	if exitCode != 0 {
		if strings.Contains(out, "Not a git repository") {
			return "", &ErrNotGitRepo{cmd, dir}
		}
		return "", fmt.Errorf("Cmd `%s` exited with code: %d, output: %s", cmd, exitCode, out)
	}

	return strings.Trim(out, "\n"), nil
}