Skip to content

Commit

Permalink
Fix deepsource error
Browse files Browse the repository at this point in the history
  • Loading branch information
nightfury1204 committed Mar 15, 2023
1 parent 48b8ff5 commit 0cc3fb7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 98 deletions.
8 changes: 4 additions & 4 deletions pkg/build/build_buildkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestBuild(t *testing.T) {
Manifest: "convox2.yml",
}

os.Setenv("PROVIDER", "do")
t.Setenv("PROVIDER", "do")
testBuild(t, opts, bkEngine, func(b *build.Build, p *structs.MockProvider, e *exec.MockInterface, out *bytes.Buffer) {
p.On("BuildGet", "app1", "build1").Return(fxBuildStarted(), nil).Once()

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestBuildDevelopment(t *testing.T) {
Push: "registry.test.com",
}

os.Setenv("PROVIDER", "do")
t.Setenv("PROVIDER", "do")
testBuild(t, opts, bkEngine, func(b *build.Build, p *structs.MockProvider, e *exec.MockInterface, out *bytes.Buffer) {
p.On("BuildGet", "app1", "build1").Return(fxBuildStarted(), nil).Once()

Expand Down Expand Up @@ -180,7 +180,7 @@ func TestBuildOptions(t *testing.T) {
Manifest: "convox2.yml",
}

os.Setenv("PROVIDER", "do")
t.Setenv("PROVIDER", "do")
testBuild(t, opts, bkEngine, func(b *build.Build, p *structs.MockProvider, e *exec.MockInterface, out *bytes.Buffer) {
p.On("BuildGet", "app1", "build1").Return(fxBuildStarted(), nil).Once()

Expand Down Expand Up @@ -253,7 +253,7 @@ func TestLogin(t *testing.T) {
}

defer os.Remove(tmp)
os.Setenv("HOME", tmp)
t.Setenv("HOME", tmp)

r, err := sdk.NewFromEnv()
if err != nil {
Expand Down
35 changes: 18 additions & 17 deletions pkg/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func (bk *BuildKit) Build(bb *Build, dir string) error {
Tag string
}

builds := []build{}
var builds []build

for _, s := range m.Services {
for i := range m.Services {
b := build{
Build: s.Build,
Image: s.Image,
Build: m.Services[i].Build,
Image: m.Services[i].Image,
}

if bb.Push != "" {
b.Tag = fmt.Sprintf("%s:%s.%s", bb.Push, s.Name, bb.Id)
b.Tag = fmt.Sprintf("%s:%s.%s", bb.Push, m.Services[i].Name, bb.Id)
}

builds = append(builds, b)
Expand Down Expand Up @@ -131,19 +131,19 @@ func (*BuildKit) Login(bb *Build) error {
}

func (*BuildKit) cacheProvider(provider string) bool {
return provider != "" && strings.Contains("do az", provider)
return provider != "" && strings.Contains("do az", provider) // skipcq
}

func (*BuildKit) buildArgs(development bool, dockerfile string, env map[string]string) ([]string, error) {
fd, err := os.Open(dockerfile)
if err != nil {
return nil, err
}
defer fd.Close()
defer fd.Close() // skipcq

s := bufio.NewScanner(fd)

args := []string{}
var args []string

for s.Scan() {
fields := strings.Fields(strings.TrimSpace(s.Text()))
Expand Down Expand Up @@ -192,26 +192,27 @@ func (*BuildKit) entrypoint(bb *Build, tag string) []string {
return inspect.Config.Entrypoint
}

// skipcq
func (bk *BuildKit) build(bb *Build, path, dockerfile, tag string, env map[string]string) error {
if path == "" {
return fmt.Errorf("must have path to build")
}

args := []string{"build"}
args = append(args, "--frontend", "dockerfile.v0")
args = append(args, "--local", fmt.Sprintf("context=%s", path))
args = append(args, "--local", fmt.Sprintf("dockerfile=%s", path))
args = append(args, "--opt", fmt.Sprintf("filename=%s", dockerfile))
args = append(args, "--output", fmt.Sprintf("type=image,name=%s,push=true", tag))
args = append(args, "--frontend", "dockerfile.v0") // skipcq
args = append(args, "--local", fmt.Sprintf("context=%s", path)) // skipcq
args = append(args, "--local", fmt.Sprintf("dockerfile=%s", path)) // skipcq
args = append(args, "--opt", fmt.Sprintf("filename=%s", dockerfile)) // skipcq
args = append(args, "--output", fmt.Sprintf("type=image,name=%s,push=true", tag)) // skipcq

if bk.cacheProvider(os.Getenv("PROVIDER")) {
reg := strings.Split(tag, ":")[0]
args = append(args, "--export-cache", fmt.Sprintf("type=registry,ref=%s:buildcache", reg))
args = append(args, "--import-cache", fmt.Sprintf("type=registry,ref=%s:buildcache", reg))
args = append(args, "--export-cache", fmt.Sprintf("type=registry,ref=%s:buildcache", reg)) // skipcq
args = append(args, "--import-cache", fmt.Sprintf("type=registry,ref=%s:buildcache", reg)) // skipcq
} else {
// keep a local cache for services using the same Dockerfile
args = append(args, "--export-cache", "type=local,dest=/var/lib/buildkit")
args = append(args, "--import-cache", "type=local,src=/var/lib/buildkit")
args = append(args, "--export-cache", "type=local,dest=/var/lib/buildkit") // skipcq
args = append(args, "--import-cache", "type=local,src=/var/lib/buildkit") // skipcq
}

df := filepath.Join(path, dockerfile)
Expand Down
11 changes: 6 additions & 5 deletions pkg/build/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (d *Docker) Build(bb *Build, dir string) error {
}
}

tagfroms := []string{}
var tagfroms []string

for from := range tags {
tagfroms = append(tagfroms, from)
Expand All @@ -117,7 +117,7 @@ func (d *Docker) Build(bb *Build, dir string) error {
}
}

pushfroms := []string{}
var pushfroms []string

for from := range pushes {
pushfroms = append(pushfroms, from)
Expand Down Expand Up @@ -165,7 +165,8 @@ func (*Docker) Login(bb *Build) error {
return nil
}

func (d *Docker) build(bb *Build, path, dockerfile, tag string, env map[string]string) error {
// skipcq
func (*Docker) build(bb *Build, path, dockerfile, tag string, env map[string]string) error {
if path == "" {
return fmt.Errorf("must have path to build")
}
Expand Down Expand Up @@ -230,11 +231,11 @@ func (bb *Build) buildArgs(dockerfile string, env map[string]string) ([]string,
if err != nil {
return nil, err
}
defer fd.Close()
defer fd.Close() // skipcq

s := bufio.NewScanner(fd)

args := []string{}
var args []string

for s.Scan() {
fields := strings.Fields(strings.TrimSpace(s.Text()))
Expand Down
22 changes: 11 additions & 11 deletions pkg/start/gen2.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type buildSource struct {
Remote string
}

func (s *Start) Start2(ctx context.Context, w io.Writer, opts Options2) error {
func (*Start) Start2(ctx context.Context, w io.Writer, opts Options2) error {
select {
case <-ctx.Done():
return nil
Expand Down Expand Up @@ -99,8 +99,8 @@ func (s *Start) Start2(ctx context.Context, w io.Writer, opts Options2) error {
services := map[string]bool{}

if opts.Services == nil {
for _, s := range m.Services {
services[s.Name] = true
for i := range m.Services {
services[m.Services[i].Name] = true
}
} else {
for _, s := range opts.Services {
Expand Down Expand Up @@ -156,13 +156,13 @@ func (s *Start) Start2(ctx context.Context, w io.Writer, opts Options2) error {
return errors.WithStack(err)
}

for _, s := range m.Services {
if !services[s.Name] {
for i := range m.Services {
if !services[m.Services[i].Name] {
continue
}

if s.Build.Path != "" {
go opts.watchChanges(ctx, pw, m, s.Name, wd, errch)
if m.Services[i].Build.Path != "" {
go opts.watchChanges(ctx, pw, m, m.Services[i].Name, wd, errch)
}
}

Expand Down Expand Up @@ -373,7 +373,7 @@ func (opts Options2) handleAdds(pid, remote string, adds []changes.Change) error
return errors.WithStack(err)
}

defer fd.Close()
defer fd.Close() // skipcq

if _, err := io.Copy(tw, fd); err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -500,7 +500,7 @@ func (opts Options2) watchPath(ctx context.Context, pw prefix.Writer, service, r
})

tick := time.Tick(1000 * time.Millisecond)
chgs := []changes.Change{}
var chgs []changes.Change

for {
select {
Expand Down Expand Up @@ -602,7 +602,7 @@ func buildSources(m *manifest.Manifest, root, service string) ([]buildSource, er
return nil, errors.WithStack(err)
}

bs := []buildSource{}
var bs []buildSource
env := map[string]string{}
wd := ""

Expand Down Expand Up @@ -714,7 +714,7 @@ lines:
}
}

bss := []buildSource{}
var bss []buildSource

for i := range bs {
contained := false
Expand Down
11 changes: 7 additions & 4 deletions provider/k8s/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Provider) buildImage(provider string) string {
}

func (*Provider) buildPrivileged(provider string) bool {
return strings.Contains("do gcp aws local", provider)
return strings.Contains("do gcp aws local", provider) // skipcq
}

func (p *Provider) BuildCreate(app, url string, opts structs.BuildCreateOptions) (*structs.Build, error) {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (p *Provider) BuildExport(app, id string, w io.Writer) error {
return errors.WithStack(err)
}

services := []string{}
var services []string

r, err := p.ReleaseGet(app, build.Release)
if err != nil {
Expand All @@ -158,8 +158,8 @@ func (p *Provider) BuildExport(app, id string, w io.Writer) error {
return errors.WithStack(err)
}

for _, s := range m.Services {
services = append(services, s.Name)
for i := range m.Services {
services = append(services, m.Services[i].Name)
}

if len(services) < 1 {
Expand Down Expand Up @@ -241,6 +241,8 @@ func (p *Provider) BuildExport(app, id string, w io.Writer) error {
if err != nil {
return err
}

// skipcq
if _, err := io.Copy(tw, data); err != nil {
return err
}
Expand Down Expand Up @@ -309,6 +311,7 @@ func (p *Provider) BuildImport(app string, r io.Reader) (*structs.Build, error)
return nil, errors.Errorf("failed to untar image - %s", err.Error())
}

// skipcq
_, err = io.Copy(f, tr)
if err != nil {
errors.Errorf("failed to write image - %s", err.Error())
Expand Down
Loading

0 comments on commit 0cc3fb7

Please sign in to comment.