-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
95 lines (85 loc) · 2.17 KB
/
files.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
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package pullbox
import (
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/synopkg/devbox/internal/cmdutil"
"github.com/synopkg/devbox/internal/devconfig"
"github.com/synopkg/devbox/internal/fileutil"
)
func (p *pullbox) copyToProfile(src string) error {
srcFileInfo, err := os.Stat(src)
if err != nil {
return errors.WithStack(err)
}
var srcFiles []fs.FileInfo
if srcFileInfo.IsDir() {
entries, err := os.ReadDir(src)
if err != nil {
return errors.WithStack(err)
}
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return errors.WithStack(err)
}
srcFiles = append(srcFiles, info)
}
} else {
srcFiles = []fs.FileInfo{srcFileInfo}
}
if err := fileutil.ClearDir(p.ProjectDir()); err != nil {
return err
}
for _, srcFile := range srcFiles {
srcPath := src
if srcFileInfo.IsDir() {
srcPath = filepath.Join(src, srcFile.Name())
}
cmd := cmdutil.CommandTTY("cp", "-rf", srcPath, p.ProjectDir())
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func profileIsNotEmpty(path string) (bool, error) {
entries, err := os.ReadDir(path)
if err != nil {
return false, errors.WithStack(err)
}
for _, entry := range entries {
if entry.Name() != devconfig.DefaultName ||
isModifiedConfig(filepath.Join(path, entry.Name())) {
return true, nil
}
}
return false, nil
}
func isModifiedConfig(path string) bool {
if filepath.Base(path) == devconfig.DefaultName {
cfg, err := devconfig.Load(path)
if err != nil {
return false
}
return !cfg.Equals(devconfig.DefaultConfig())
}
return false
}
// urlIsArchive checks if a file URL points to an archive file
func urlIsArchive(url string) (bool, error) {
response, err := http.Head(url)
if err != nil {
return false, err
}
defer response.Body.Close()
contentType := response.Header.Get("Content-Type")
return strings.Contains(contentType, "tar") ||
strings.Contains(contentType, "zip") ||
strings.Contains(contentType, "octet-stream"), nil
}