-
Notifications
You must be signed in to change notification settings - Fork 32
/
upload.go
88 lines (75 loc) · 1.95 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package stage
import (
"context"
"fmt"
"github.com/godarch/darch/pkg/cmd/darch/commands"
"github.com/godarch/darch/pkg/reference"
"github.com/godarch/darch/pkg/repository"
"github.com/godarch/darch/pkg/staging"
"github.com/godarch/darch/pkg/workspace"
"github.com/urfave/cli"
)
var uploadCommand = cli.Command{
Name: "upload",
Usage: "upload local image to stage",
ArgsUsage: "<image[:tag]>",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force",
Usage: "overwrite existing image with the given name",
},
},
Action: func(clicontext *cli.Context) error {
var (
imageName = clicontext.Args().First()
force = clicontext.Bool("force")
)
err := commands.CheckForRoot()
if err != nil {
return err
}
imageRef, err := reference.ParseImage(imageName)
if err != nil {
return err
}
repo, err := repository.NewSession(repository.DefaultContainerdSocketLocation)
if err != nil {
return err
}
defer repo.Close()
stagingSession, err := staging.NewSession()
if err != nil {
return err
}
// If the user isn't forcing this upload, let's do a quick check to see if it is already uploaded.
if !force {
isStaged, err := stagingSession.IsStaged(imageRef)
if err != nil {
return err
}
if isStaged {
return fmt.Errorf("image already exists on stage, --force to overwrite")
}
}
ws, err := workspace.NewWorkspace(staging.DefaultStagingDirectoryTmp)
if err != nil {
return err
}
defer ws.Destroy()
err = repo.ExtractImage(context.Background(), imageRef, ws.Path)
if err != nil {
return err
}
err = stagingSession.UploadDirectoryWithMove(ws.Path, imageRef, force)
if err != nil {
return err
}
ws.MarkDestroyed() // prevent defered Destroy() from working, since we moved the directory to where it should be.
// Run hooks for the new image.
err = stagingSession.RunHooksForImage(imageRef)
if err != nil {
return err
}
return stagingSession.SyncBootloader()
},
}