(ctx context.Context, dockerCli command.Cli, options CreateTransformerOptions)
| 41 | } |
| 42 | |
| 43 | func CreateTransformer(ctx context.Context, dockerCli command.Cli, options CreateTransformerOptions) error { |
| 44 | if options.From == "" { |
| 45 | options.From = DefaultTransformerImage |
| 46 | } |
| 47 | out, err := filepath.Abs(options.Dest) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | |
| 52 | if _, err := os.Stat(out); err == nil { |
| 53 | return fmt.Errorf("output folder %s already exists", out) |
| 54 | } |
| 55 | |
| 56 | tmpl := filepath.Join(out, "templates") |
| 57 | err = os.MkdirAll(tmpl, 0o744) |
| 58 | if err != nil && !os.IsExist(err) { |
| 59 | return fmt.Errorf("cannot create output folder: %w", err) |
| 60 | } |
| 61 | |
| 62 | if err := command.ValidateOutputPath(out); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | created, err := dockerCli.Client().ContainerCreate(ctx, client.ContainerCreateOptions{ |
| 67 | Image: options.From, |
| 68 | }) |
| 69 | |
| 70 | defer func() { |
| 71 | _, _ = dockerCli.Client().ContainerRemove(context.Background(), created.ID, client.ContainerRemoveOptions{ |
| 72 | Force: true, |
| 73 | }) |
| 74 | }() |
| 75 | |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | resp, err := dockerCli.Client().CopyFromContainer(ctx, created.ID, client.CopyFromContainerOptions{ |
| 80 | SourcePath: templatesPath, |
| 81 | }) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | defer func() { |
| 86 | _ = resp.Content.Close() |
| 87 | }() |
| 88 | |
| 89 | srcInfo := archive.CopyInfo{ |
| 90 | Path: templatesPath, |
| 91 | Exists: true, |
| 92 | IsDir: resp.Stat.Mode.IsDir(), |
| 93 | } |
| 94 | |
| 95 | preArchive := resp.Content |
| 96 | if srcInfo.RebaseName != "" { |
| 97 | _, srcBase := archive.SplitPathDirEntry(srcInfo.Path) |
| 98 | preArchive = archive.RebaseArchiveEntries(resp.Content, srcBase, srcInfo.RebaseName) |
| 99 | } |
| 100 |
no test coverage detected