Skip to content

Commit

Permalink
fix: support multi --src + export file permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
theobori committed Oct 16, 2023
1 parent 1bb9060 commit 5cbdb51
Show file tree
Hide file tree
Showing 22 changed files with 537 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Go workspace file
go.work

dockerv
/dockerv
vendor

*.tar.gz
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,5 @@ dockerv move \
- [ ] Documentation + (commands help)
- [ ] Custom volume destination for single source volume packed
- [ ] `import` without dest
- [ ] Export output permissions
- [ ] Support Docker volume list for `DockerVolumePoint`
- [ ] Output every operation
- [ ] Export multiple source ?
- [x] Export output permissions
- [x] Support multi `--src`
8 changes: 4 additions & 4 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ import (

"github.com/docker/docker/api/types/volume"
"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
"github.com/theobori/dockerv/internal/docker"
)

// copyCmd represents the export command
var copyCmd = &cobra.Command{
Use: "copy",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dest, _ := cmd.Flags().GetString("dest")
force, _ := cmd.Flags().GetBool("force")

dvConfig.PointDestination = &dest
dvConfig.PointDest = &dest
dvConfig.Kind = dockerv.Copy

// Creates the destination if not exists
Expand Down
8 changes: 4 additions & 4 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import (
"os"

"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
)

// exportCmd represents the export command
var exportCmd = &cobra.Command{
Use: "export",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dvConfig.Force, _ = cmd.Flags().GetBool("force")

dest, _ := cmd.Flags().GetString("dest")

dvConfig.PointDestination = &dest
dvConfig.PointDest = &dest
dvConfig.Kind = dockerv.Export

if err := dockerVExecute(); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ import (
"os"

"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
)

// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dvConfig.Force, _ = cmd.Flags().GetBool("force")

dest, _ := cmd.Flags().GetString("dest")

dvConfig.PointDestination = &dest
dvConfig.PointDest = &dest
dvConfig.Kind = dockerv.Import

if err := dockerVExecute(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"os"

"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dvConfig.State, _ = cmd.Flags().GetBool("state")

dvConfig.Kind = dockerv.List
Expand Down
8 changes: 4 additions & 4 deletions cmd/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ import (

"github.com/docker/docker/api/types/volume"
"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
"github.com/theobori/dockerv/internal/docker"
dockerv "github.com/theobori/dockerv/internal/dockerv"
)

// moveCmd represents the export command
var moveCmd = &cobra.Command{
Use: "move",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dest, _ := cmd.Flags().GetString("dest")
force, _ := cmd.Flags().GetBool("force")

dvConfig.PointDestination = &dest
dvConfig.PointDest = &dest
dvConfig.Kind = dockerv.Move

// Creates the destination if not exists
Expand Down
6 changes: 3 additions & 3 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"os"

"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
)

// removeCmd represents the remove command
var removeCmd = &cobra.Command{
Use: "remove",
Run: func(cmd *cobra.Command, args []string) {
dvConfig.PointSource, _ = cmd.Flags().GetString("src")
Run: func(cmd *cobra.Command, _ []string) {
dvConfig.PointSource, _ = cmd.Flags().GetStringSlice("src")
dvConfig.Force, _ = cmd.Flags().GetBool("force")

dvConfig.Kind = dockerv.Remove
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

"github.com/docker/docker/client"
"github.com/spf13/cobra"
dockerv "github.com/theobori/dockerv/internal"
dockerv "github.com/theobori/dockerv/internal/dockerv"
"github.com/theobori/dockerv/internal/point"
)

Expand All @@ -52,7 +52,7 @@ A point can be:
- A Docker compose file
- A directory
`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {

},
}
Expand Down Expand Up @@ -80,9 +80,9 @@ func Execute() {
}

func init() {
rootCmd.PersistentFlags().String(
rootCmd.PersistentFlags().StringSlice(
"src",
".",
[]string{},
"The source point",
)
rootCmd.MarkPersistentFlagRequired("src")
Expand Down
43 changes: 36 additions & 7 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func DockerVolumeExists(ctx context.Context, cli *client.Client, name string) bo
return err == nil
}

func DockerGetVolumesExist(ctx context.Context, cli *client.Client, names []string) []string {
ret := []string{}

for _, name := range names {
if DockerVolumeExists(ctx, cli, name) {
ret = append(ret, name)
}
}

return ret
}

func containerStartAutoRemove(ctx context.Context, cli *client.Client, id string) error {
var err error

Expand Down Expand Up @@ -74,13 +86,25 @@ func containerStartAutoRemove(ctx context.Context, cli *client.Client, id string
return nil
}

func ExecContainer(ctx context.Context, cli *client.Client, command *strslice.StrSlice, mounts *[]mount.Mount) error {
func ExecContainer(
ctx context.Context,
cli *client.Client,
command *strslice.StrSlice,
mounts *[]mount.Mount,
user string,
) error {
config := container.Config{
Cmd: *command,
Image: DockerImage,
}

if user != "" {
config.User = user
}

resp, err := cli.ContainerCreate(
ctx,
&container.Config{
Cmd: *command,
Image: DockerImage,
},
&config,
&container.HostConfig{
Mounts: *mounts,
}, nil, nil, "",
Expand All @@ -97,7 +121,12 @@ func ExecContainer(ctx context.Context, cli *client.Client, command *strslice.St
return nil
}

func DockerVolumeCopy(ctx context.Context, cli *client.Client, vSrc string, vDest string) error {
func DockerVolumeCopy(
ctx context.Context,
cli *client.Client,
vSrc string,
vDest string,
) error {
return ExecContainer(
ctx,
cli,
Expand All @@ -115,6 +144,6 @@ func DockerVolumeCopy(ctx context.Context, cli *client.Client, vSrc string, vDes
Source: vDest,
Target: "/dest",
},
},
}, "",
)
}
47 changes: 47 additions & 0 deletions internal/dockerv/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2023 Théo Bori <nagi@tilde.team>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package dockerv

import "github.com/theobori/dockerv/internal/point"

func (dv *DockerV) copy(vSrc *[]string) error {
if len(dv.source) != 1 {
return point.ErrOperation
}

pSrc := dv.source[0]
ok := true

ok = ok && (*pSrc).Metadata().Kind(dv.cli) == point.DockerVolume
ok = ok && (*dv.destination).Metadata().Kind(dv.cli) == point.DockerVolume

if !ok {
return point.ErrOperation
}

if err := (*dv.destination).From(vSrc); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 5cbdb51

Please sign in to comment.