当前位置: 首页>>代码示例>>Golang>>正文


Golang archive.Untar函数代码示例

本文整理汇总了Golang中github.com/docker/docker/pkg/archive.Untar函数的典型用法代码示例。如果您正苦于以下问题:Golang Untar函数的具体用法?Golang Untar怎么用?Golang Untar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Untar函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ImageBuild

func (c *DaemonClient) ImageBuild(ctx context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
	if c.imageName != "" {
		if len(options.Tags) != 1 || options.Tags[0] != c.imageName {
			return types.ImageBuildResponse{}, fmt.Errorf("expected image %q, got %v", c.imageName, options.Tags)
		}
	}
	if c.contextDir != "" {
		tmp, err := ioutil.TempDir("", "image-build-test")
		if err != nil {
			return types.ImageBuildResponse{}, err
		}
		if err := archive.Untar(context, tmp, nil); err != nil {
			return types.ImageBuildResponse{}, err
		}
		changes, err := archive.ChangesDirs(tmp, c.contextDir)
		if err != nil {
			return types.ImageBuildResponse{}, err
		}
		if len(changes) != c.changes {
			return types.ImageBuildResponse{}, fmt.Errorf("expected %d changes, got %v", c.changes, changes)
		}
		b, err := json.Marshal(c.message)
		if err != nil {
			return types.ImageBuildResponse{}, err
		}
		return types.ImageBuildResponse{
			Body: ioutil.NopCloser(bytes.NewReader(b)),
		}, nil
	}
	return c.NopClient.ImageBuild(ctx, context, options)
}
开发者ID:haj,项目名称:kompose,代码行数:31,代码来源:builder_test.go

示例2: getContextFromReader

// getContextFromReader will read the contents of the given reader as either a
// Dockerfile or tar archive to be extracted to a temporary directory used as
// the context directory. Returns the absolute path to the temporary context
// directory, the relative path of the dockerfile in that context directory,
// and a non-nil error on success.
func getContextFromReader(r io.Reader, dockerfileName string) (absContextDir, relDockerfile string, err error) {
	buf := bufio.NewReader(r)

	magic, err := buf.Peek(archive.HeaderSize)
	if err != nil && err != io.EOF {
		return "", "", fmt.Errorf("failed to peek context header from STDIN: %v", err)
	}

	if absContextDir, err = ioutil.TempDir("", "docker-build-context-"); err != nil {
		return "", "", fmt.Errorf("unbale to create temporary context directory: %v", err)
	}

	defer func(d string) {
		if err != nil {
			os.RemoveAll(d)
		}
	}(absContextDir)

	if !archive.IsArchive(magic) { // Input should be read as a Dockerfile.
		// -f option has no meaning when we're reading it from stdin,
		// so just use our default Dockerfile name
		relDockerfile = api.DefaultDockerfileName

		return absContextDir, relDockerfile, writeToFile(buf, filepath.Join(absContextDir, relDockerfile))
	}

	if err := archive.Untar(buf, absContextDir, nil); err != nil {
		return "", "", fmt.Errorf("unable to extract stdin to temporary context directory: %v", err)
	}

	return getDockerfileRelPath(absContextDir, dockerfileName)
}
开发者ID:Distrotech,项目名称:docker,代码行数:37,代码来源:build.go

示例3: checkTarCorrect

func checkTarCorrect(tar archive.Archive, expectedFiles, unexpectedFiles []string, t *testing.T) {
	err := archive.Untar(tar, "/tmp/tar", nil)
	asserErrNil(err, t)
	defer os.RemoveAll("/tmp/tar")
	filesShouldExist(true, expectedFiles, "/tmp/tar", t)
	filesShouldExist(false, unexpectedFiles, "/tmp/tar", t)
}
开发者ID:HowardMei,项目名称:krgo,代码行数:7,代码来源:git_test.go

示例4: UntarToDest

// UntarToDest writes to user destination the streamed tarball in input
func UntarToDest(api *api.ScalewayAPI, sourceStream *io.ReadCloser, destination string) error {
	// destination is a server address + path (scp-like uri)
	if strings.Index(destination, ":") > -1 {
		log.Debugf("Streaming using ssh and untaring remotely")
		serverParts := strings.Split(destination, ":")
		if len(serverParts) != 2 {
			return fmt.Errorf("invalid destination uri, see 'scw cp -h' for usage")
		}

		serverID := api.GetServerID(serverParts[0])

		server, err := api.GetServer(serverID)
		if err != nil {
			return err
		}

		// remoteCommand is executed on the remote server
		// it streams a tarball raw content
		remoteCommand := []string{"tar"}
		remoteCommand = append(remoteCommand, "-C", serverParts[1])
		if os.Getenv("DEBUG") == "1" {
			remoteCommand = append(remoteCommand, "-v")
		}
		remoteCommand = append(remoteCommand, "-xf", "-")

		// execCmd contains the ssh connection + the remoteCommand
		execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, false, remoteCommand))
		log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
		spawnDst := exec.Command("ssh", execCmd...)

		untarInputStream, err := spawnDst.StdinPipe()
		if err != nil {
			return err
		}
		defer untarInputStream.Close()

		// spawnDst.Stderr = os.Stderr
		// spawnDst.Stdout = os.Stdout

		err = spawnDst.Start()
		if err != nil {
			return err
		}

		_, err = io.Copy(untarInputStream, *sourceStream)
		return err
	}

	// destination is stdout
	if destination == "-" { // stdout
		log.Debugf("Writing sourceStream(%v) to os.Stdout(%v)", sourceStream, os.Stdout)
		_, err := io.Copy(os.Stdout, *sourceStream)
		return err
	}

	// destination is a path on localhost
	log.Debugf("Untaring to local path: %s", destination)
	err := archive.Untar(*sourceStream, destination, &archive.TarOptions{NoLchown: true})
	return err
}
开发者ID:pkgr,项目名称:scaleway-cli,代码行数:61,代码来源:cp.go

示例5: themePullCommand

func themePullCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}
	// directory
	directory, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}
	stderr := os.Stderr

	if len(ctx.Args()) != 1 {
		fmt.Fprintln(stderr, "please input theme id with format: provider/name:version")
		os.Exit(1)
	}

	provider, name, version, err := parse_theme(ctx.Args()[0])
	if err != nil {
		fmt.Fprintln(stderr, "package format: provider/name:version, please try again")
		os.Exit(1)
	}

	// auth config
	config, err := LoadConfigFile(directory)
	if err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}

	host := ctx.String("Host")
	port := ctx.Int("Port")
	client := NewClient(directory, host, port)

	var pkg Package
	pkg.Provider = provider
	pkg.Name = name
	pkg.Version = version

	if err := client.ThemePull(config.Auth.Token, &pkg); err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}

	bar := pb.New(int(pkg.ArchiveLen)).SetUnits(pb.U_BYTES)
	bar.Prefix(fmt.Sprintf("%s/%s:%s ", pkg.Provider, pkg.Name, pkg.Version))
	bar.Start()
	// create multi writer
	rd := pb.NewPbReader(pkg.ArchiveReader, bar)

	if err := archive.Untar(rd, directory, nil); err != nil {
		fmt.Fprintln(stderr, err.Error())
		os.Exit(1)
	}
	bar.FinishPrint(fmt.Sprintf("%s/%s:%s pulled succussfully.", pkg.Provider, pkg.Name, pkg.Version))
}
开发者ID:h2object,项目名称:h2object,代码行数:58,代码来源:theme.go

示例6: untar

func untar(value []byte, dirPath string) error {
	return archive.Untar(
		bytes.NewReader(value),
		dirPath,
		&archive.TarOptions{
			NoLchown: true,
		},
	)
}
开发者ID:sr,项目名称:protoeasy,代码行数:9,代码来源:util.go

示例7: copyForExport

func copyForExport(docker docker.Docker, v *Volume) (io.Reader, error) {
	bindSpec := v.HostPath + ":/.dockervolume"

	vJson, err := json.MarshalIndent(v, "", "	")
	if err != nil {
		return nil, fmt.Errorf("Could not export volume data")
	}
	jsonStr := string(vJson)

	// Since we're using busybox's tar, it does not support appending files
	// Instead we'll handle adding in Dockerfile/config.json manually
	cmd := fmt.Sprintf(
		"mkdir -p /volumeData && cp -r /.dockervolume /volumeData/data && echo '%s' > /volumeData/Dockerfile && echo '%s' > /volumeData/config.json; cd /volumeData && tar -cf volume.tar .",
		ExportDockerfile,
		jsonStr,
	)
	containerConfig := map[string]interface{}{
		"Image": "busybox",
		"Cmd":   []string{"/bin/sh", "-c", cmd},
		"Volumes": map[string]struct{}{
			"/.dockervolume": struct{}{},
		},
		"HostConfig": map[string]interface{}{
			"Binds": []string{bindSpec},
		},
	}

	containerId, err := docker.RunContainer(containerConfig)
	if err != nil {
		return nil, fmt.Errorf("%s - %s", containerId, err)
	}
	defer docker.RemoveContainer(containerId, true, true)

	// Wait for the container to exit, signaling that our archive is ready
	if err := docker.ContainerWait(containerId); err != nil {
		return nil, fmt.Errorf("Could not get archive: %s", err)
	}

	// This is a tar of a tar, we only want the inner tar, so do some more stuff
	tmpArch, err := docker.Copy(containerId, "/volumeData/volume.tar")
	if err != nil {
		return nil, fmt.Errorf("Could not get archive: %s", err)
	}

	id := GenerateRandomID()
	tmpDir, err := ioutil.TempDir("", id)
	if err != nil {
		return nil, fmt.Errorf("Could not create temp dir: %s", err)
	}
	defer os.RemoveAll(tmpDir)
	// extract the tar to a temp dir so we can get the inner-tar
	if err := archive.Untar(tmpArch, tmpDir, &archive.TarOptions{Compression: archive.Uncompressed, NoLchown: true}); err != nil {
		return nil, fmt.Errorf("Could not untar archive: %s", err)
	}
	// Get the inner-tar and output to stdout
	return os.Open(tmpDir + "/volume.tar")
}
开发者ID:ankushagarwal,项目名称:docker-volumes,代码行数:57,代码来源:export.go

示例8: Decompress

func (c *tarArchiver) Decompress(reader io.Reader, dirPath string) error {
	return archive.Untar(
		reader,
		dirPath,
		&archive.TarOptions{
			NoLchown: true,
		},
	)
}
开发者ID:peter-edge,项目名称:pkg-go,代码行数:9,代码来源:tar_compressor.go

示例9: ExtractTarGz

func ExtractTarGz(in io.Reader, dest string, uid int, gid int) (err error) {
	return archive.Untar(in, dest, &archive.TarOptions{
		Compression: archive.Gzip,
		NoLchown:    false,
		ChownOpts: &archive.TarChownOptions{
			UID: uid,
			GID: gid,
		},
		ExcludePatterns: []string{"dev/"}, // prevent operation not permitted
	})
}
开发者ID:varung,项目名称:droot,代码行数:11,代码来源:util.go

示例10: unpackBodyTarball

func unpackBodyTarball(req io.ReadCloser) (tmpdir string, err error) {
	tmpdir, err = ioutil.TempDir("", "go-dockerclient-test")
	if err != nil {
		return
	}
	err = archive.Untar(req, tmpdir, &archive.TarOptions{
		Compression: archive.Uncompressed,
		NoLchown:    true,
	})
	return
}
开发者ID:rawlingsj,项目名称:gofabric8,代码行数:11,代码来源:build_test.go

示例11: ExtractTar

func ExtractTar(in io.Reader, dest string) (err error) {
	//func ExtractTarGz(in io.Reader, dest string, uid int, gid int) (err error) {

	//    ChownOpts: &archive.TarChownOptions{
	//			UID: uid,
	//			GID: gid,
	//		},

	return archive.Untar(in, dest, &archive.TarOptions{
		NoLchown:        false,
		ExcludePatterns: []string{"dev/"}, // prevent operation not permitted
	})
}
开发者ID:mudler,项目名称:artemide,代码行数:13,代码来源:archive.go

示例12: Restore

//Restore restores the contents of dir from the cache
func (c Cache) Restore(dir, task string) {
	path := c.path(dir, task)
	cacheReader, err := c.Store.Reader(c.path(dir, task))
	if cacheReader != nil {
		err = archive.Untar(cacheReader, dir, &archive.TarOptions{})
		c.log.Info("Restoring cache for", dir, "from", path)
		if err != nil {
			c.log.Error("Error restoring cache for", dir, "from", path, err.Error())
		}
	} else {
		c.log.Info("No Cache for", dir, "to restore")
	}
}
开发者ID:assemblyline,项目名称:spanner,代码行数:14,代码来源:cache.go

示例13: extractVolConfigJson

func extractVolConfigJson(imgId string, docker docker.Docker) (string, error) {
	extractVolInfoConfig := map[string]interface{}{
		"Image": imgId,
		"Cmd":   []string{"/bin/sh", "-c", "true"},
	}
	cid1, err := docker.RunContainer(extractVolInfoConfig)
	if err != nil {
		return "", fmt.Errorf("Could not extract volume config: ", err)
	}
	defer docker.RemoveContainer(cid1, true, true)
	docker.ContainerWait(cid1)

	tmpArch, err := docker.Copy(cid1, "/.volData/config.json")
	if err != nil {
		return "", fmt.Errorf("Could not extract volume config: ", err)
	}

	// Setup tmp dir for extracting the downloaded archive
	id := GenerateRandomID()
	tmpDir, err := ioutil.TempDir("", id)
	if err != nil {
		return "", fmt.Errorf("Could not create temp dir: ", err)
	}
	defer os.RemoveAll(tmpDir)

	// extract the tar to a temp dir so we can get the inner-tar
	if err := archive.Untar(tmpArch, tmpDir, &archive.TarOptions{Compression: archive.Uncompressed, NoLchown: true}); err != nil {
		return "", fmt.Errorf("Could not untar archive", err)
	}
	// Get the inner-tar and output to stdout
	configFile, err := os.Open(tmpDir + "/config.json")
	if err != nil {
		return "", fmt.Errorf("Could not open config.json: ", err)
	}
	var volConfig Volume
	if err := json.NewDecoder(configFile).Decode(&volConfig); err != nil {
		fmt.Errorf("Could not read config.json: ", err)
	}

	return volConfig.VolPath, nil
}
开发者ID:ankushagarwal,项目名称:docker-volumes,代码行数:41,代码来源:import.go

示例14: CmdCp

// CmdCp copies files/folders from a path on the container to a directory on the host running the command.
//
// If HOSTDIR is '-', the data is written as a tar file to STDOUT.
//
// Usage: docker cp CONTAINER:PATH HOSTDIR
func (cli *DockerCli) CmdCp(args ...string) error {
	cmd := cli.Subcmd("cp", "CONTAINER:PATH HOSTDIR|-", "Copy files/folders from a PATH on the container to a HOSTDIR on the host\nrunning the command. Use '-' to write the data\nas a tar file to STDOUT.", true)
	cmd.Require(flag.Exact, 2)

	cmd.ParseFlags(args, true)

	var copyData engine.Env
	info := strings.Split(cmd.Arg(0), ":")

	if len(info) != 2 {
		return fmt.Errorf("Error: Path not specified")
	}

	copyData.Set("Resource", info[1])
	copyData.Set("HostPath", cmd.Arg(1))

	stream, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", copyData, nil)
	if stream != nil {
		defer stream.Close()
	}
	if statusCode == 404 {
		return fmt.Errorf("No such container: %v", info[0])
	}
	if err != nil {
		return err
	}

	if statusCode == 200 {
		dest := copyData.Get("HostPath")

		if dest == "-" {
			_, err = io.Copy(cli.out, stream)
		} else {
			err = archive.Untar(stream, dest, &archive.TarOptions{NoLchown: true})
		}
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:jbbarth,项目名称:docker,代码行数:46,代码来源:cp.go

示例15: readContext

func (b *Builder) readContext(context io.Reader) error {
	tmpdirPath, err := ioutil.TempDir("", "docker-build")
	if err != nil {
		return err
	}

	decompressedStream, err := archive.DecompressStream(context)
	if err != nil {
		return err
	}

	if b.context, err = tarsum.NewTarSum(decompressedStream, true, tarsum.Version0); err != nil {
		return err
	}
	if err := archive.Untar(b.context, tmpdirPath, nil); err != nil {
		return err
	}

	b.contextPath = tmpdirPath
	return nil
}
开发者ID:baoruxing,项目名称:docker,代码行数:21,代码来源:internals.go


注:本文中的github.com/docker/docker/pkg/archive.Untar函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。