-
Notifications
You must be signed in to change notification settings - Fork 178
/
pullbox.go
125 lines (105 loc) · 2.83 KB
/
pullbox.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package pullbox
import (
"context"
"io/fs"
"os"
"path/filepath"
"runtime/trace"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/usererr"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/pullbox/git"
"go.jetpack.io/devbox/internal/pullbox/s3"
"go.jetpack.io/devbox/internal/pullbox/tar"
"go.jetpack.io/devbox/internal/ux"
)
type devboxProject interface {
ProjectDir() string
}
type pullbox struct {
devboxProject
devopt.PullboxOpts
}
func New(devbox devboxProject, opts devopt.PullboxOpts) *pullbox {
return &pullbox{devbox, opts}
}
// Pull
// This can be rewritten to be more readable and less repetitive. Possibly
// something like:
// puller := getPullerForURL(url)
// return puller.Pull()
func (p *pullbox) Pull(ctx context.Context) error {
defer trace.StartRegion(ctx, "Pull").End()
var err error
notEmpty, err := profileIsNotEmpty(p.ProjectDir())
if err != nil {
return err
} else if notEmpty && !p.Overwrite {
return fs.ErrExist
}
if p.URL != "" {
ux.Finfo(os.Stderr, "Pulling global config from %s\n", p.URL)
} else {
ux.Finfo(os.Stderr, "Pulling global config\n")
}
var tmpDir string
if p.URL == "" {
if p.Credentials.IDToken == "" {
return usererr.New("Not logged in")
}
profile := "default" // TODO: make this editable
if tmpDir, err = s3.PullToTmp(ctx, &p.Credentials, profile); err != nil {
return err
}
return p.copyToProfile(tmpDir)
}
if git.IsRepoURL(p.URL) {
if tmpDir, err = git.CloneToTmp(p.URL); err != nil {
return err
}
// Remove the .git directory, we don't want to keep state
if err := os.RemoveAll(filepath.Join(tmpDir, ".git")); err != nil {
return errors.WithStack(err)
}
return p.copyToProfile(tmpDir)
}
if p.IsTextDevboxConfig() {
return p.pullTextDevboxConfig(ctx)
}
if isArchive, err := urlIsArchive(p.URL); err != nil {
return err
} else if isArchive {
data, err := download(p.URL)
if err != nil {
return err
}
if tmpDir, err = tar.Extract(data); err != nil {
return err
}
return p.copyToProfile(tmpDir)
}
return usererr.New("Could not determine how to pull %s", p.URL)
}
func (p *pullbox) Push(ctx context.Context) error {
if p.URL != "" {
ux.Finfo(os.Stderr, "Pushing global config to %s\n", p.URL)
} else {
ux.Finfo(os.Stderr, "Pushing global config\n")
}
if p.URL == "" {
profile := "default" // TODO: make this editable
if p.Credentials.IDToken == "" {
return usererr.New("Not logged in")
}
ux.Finfo(
os.Stderr,
"Logged in as %s, pushing to to devbox cloud (profile: %s)\n",
p.Credentials.Email,
profile,
)
return s3.Push(ctx, &p.Credentials, p.ProjectDir(), profile)
}
return git.Push(ctx, p.ProjectDir(), p.URL)
}