Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #177 from dgonyeo/break-copy-in-two
Browse files Browse the repository at this point in the history
copy: split --to-dir flag into separate subcommand
  • Loading branch information
Derek Gonyeo committed Mar 22, 2016
2 parents 937c64c + 167122b commit d16190f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 39 deletions.
22 changes: 22 additions & 0 deletions Documentation/subcommands/copy-to-dir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# acbuild copy-to-dir

`acbuild copy-to-dir` will copy any number of files and directories from the
local filesystem into the ACI.

It takes at least two arguments, where all but the final argument are paths on
the host system. The final argument is a parent directory inside the ACI to
place the files and directories in.

If the target directory doesn't exist in the ACI, it will be implicitly created
(along with any necessary parent directories).

The following commands would do the same thing:

```bash
acbuild copy-to-dir apache.conf sites-available/00-default sites-available/myblog /etc/apache2
```

```bash
cp apache.conf sites-available/00-default sites-available/myblog ./.acbuild/currentaci/rootfs/etc/apache2
```

30 changes: 2 additions & 28 deletions Documentation/subcommands/copy.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# acbuild copy

`acbuild copy` will copy files and directories from the local filesystem into
`acbuild copy` will copy one file or directory from the local filesystem into
the ACI.

There are two modes of operation for `acbuild copy`, one for copying multiple
files/directories into a specified directory, and one for copying a single
file/directory to a specified path.

## Default Mode

By default, `acbuild copy` will copy one thing from the host to a specified
path. It takes exactly two arguments, the first of which is the path on the
It takes exactly two arguments, the first of which is the path on the
local system to copy from, and the second is the path inside the ACI to copy
to. If the target path's parent directory does not exist in the filesystem, it
will be implicitly created (along with any necessary parent directories).
Expand All @@ -24,22 +17,3 @@ acbuild copy ./nginx.conf /etc/nginx/nginx.conf
```bash
cp ./nginx.conf ./.acbuild/currentaci/rootfs/etc/nginx/nginx.conf
```

## Explicit Target Mode

When the `--to-dir` flag is used, `acbuild copy` takes any number of arguments
greater than or equal to 2. The last specified argument is the directory inside
the ACI to put the files in, and all other arguments are paths on the host to
copy. If the target directory doesn't exist in the ACI, it will be implicitly
created (along with any necessary parent directories).

The following commands would do the same thing:

```bash
acbuild copy --to-dir apache.conf sites-available/00-default sites-available/myblog /etc/apache2
```

```bash
cp apache.conf sites-available/00-default sites-available/myblog /etc/apache2
```

65 changes: 65 additions & 0 deletions acbuild/copy-to-dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2015 The appc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"

"github.com/appc/acbuild/Godeps/_workspace/src/github.com/spf13/cobra"
)

var (
toDir bool
cmdCopyToDir = &cobra.Command{
Use: "copy-to-dir PATH1_ON_HOST PATH2_ON_HOST ... PATH_IN_ACI",
Short: "Copy a file or directory into a directory in an ACI",
Example: "acbuild copy-to-dir build/bin/* /usr/bin",
Run: runWrapper(runCopyToDir),
}
)

func init() {
cmdAcbuild.AddCommand(cmdCopyToDir)
}

func runCopyToDir(cmd *cobra.Command, args []string) (exit int) {
if len(args) == 0 {
cmd.Usage()
return 1
}
if len(args) < 2 {
stderr("copy-to-dir: incorrect number of arguments")
return 1
}

if debug {
logMsg := "Copying "
for i := 0; i < len(args)-1; i++ {
logMsg += fmt.Sprintf("%s ", args[i])
}
logMsg += "to "
logMsg += fmt.Sprintf("%s", args[len(args)-1])
stderr(logMsg)
}

err := newACBuild().CopyToDir(args[:len(args)-1], args[len(args)-1])

if err != nil {
stderr("copy-to-dir: %v", err)
return getErrorCode(err)
}

return 0
}
11 changes: 2 additions & 9 deletions acbuild/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
)

var (
toDir bool
cmdCopy = &cobra.Command{
Use: "copy PATH_ON_HOST PATH_IN_ACI",
Short: "Copy a file or directory into an ACI",
Expand All @@ -30,15 +29,14 @@ var (

func init() {
cmdAcbuild.AddCommand(cmdCopy)
cmdCopy.Flags().BoolVar(&toDir, "to-dir", false, "copy multiple files/directories to the specified directory")
}

func runCopy(cmd *cobra.Command, args []string) (exit int) {
if len(args) == 0 {
cmd.Usage()
return 1
}
if (!toDir && len(args) != 2) || (toDir && len(args) < 2) {
if len(args) != 2 {
stderr("copy: incorrect number of arguments")
return 1
}
Expand All @@ -47,12 +45,7 @@ func runCopy(cmd *cobra.Command, args []string) (exit int) {
stderr("Copying host:%s to aci:%s", args[0], args[1])
}

var err error
if toDir {
err = newACBuild().CopyToDir(args[:len(args)-1], args[len(args)-1])
} else {
err = newACBuild().CopyToTarget(args[0], args[1])
}
err := newACBuild().CopyToTarget(args[0], args[1])

if err != nil {
stderr("copy: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions tests/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestCopyManyDirs(t *testing.T) {
}

// golang--
err = runACBuildNoHist(workingDir, append([]string{"copy", "--to-dir"}, append(froms, dest)...)...)
err = runACBuildNoHist(workingDir, append([]string{"copy-to-dir"}, append(froms, dest)...)...)
if err != nil {
t.Fatalf("%v\n", err)
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestCopyDirsAndFiles(t *testing.T) {
}

// golang--
err = runACBuildNoHist(workingDir, append([]string{"copy", "--to-dir"}, append(froms, dest)...)...)
err = runACBuildNoHist(workingDir, append([]string{"copy-to-dir"}, append(froms, dest)...)...)
if err != nil {
t.Fatalf("%v\n", err)
}
Expand Down

0 comments on commit d16190f

Please sign in to comment.