Skip to content

Commit

Permalink
Refactor command interface a bit, only cache the context for commands…
Browse files Browse the repository at this point in the history
… that use it.
  • Loading branch information
dlorenc committed Oct 2, 2018
1 parent e57778e commit 9bd6f5b
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 155 deletions.
6 changes: 3 additions & 3 deletions pkg/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

type AddCommand struct {
BaseCommand
cmd *instructions.AddCommand
buildcontext string
snapshotFiles []string
Expand Down Expand Up @@ -110,7 +111,6 @@ func (a *AddCommand) String() string {
return a.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (a *AddCommand) CacheCommand() bool {
return false
func (a *AddCommand) UsesContext() bool {
return true
}
11 changes: 1 addition & 10 deletions pkg/commands/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

type ArgCommand struct {
BaseCommand
cmd *instructions.ArgCommand
}

Expand All @@ -46,17 +47,7 @@ func (r *ArgCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
return nil
}

// FilesToSnapshot returns an empty array since this command only touches metadata.
func (r *ArgCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (r *ArgCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *ArgCommand) CacheCommand() bool {
return false
}
34 changes: 34 additions & 0 deletions pkg/commands/base_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

type BaseCommand struct {
cache bool
usesContext bool
}

func (b *BaseCommand) CacheCommand() bool {
return b.cache
}

func (b *BaseCommand) UsesContext() bool {
return b.usesContext
}

func (b *BaseCommand) FilesToSnapshot() []string {
return []string{}
}
11 changes: 1 addition & 10 deletions pkg/commands/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

type CmdCommand struct {
BaseCommand
cmd *instructions.CmdCommand
}

Expand All @@ -52,17 +53,7 @@ func (c *CmdCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
return nil
}

// FilesToSnapshot returns an empty array since this is a metadata command
func (c *CmdCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (c *CmdCommand) String() string {
return c.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (c *CmdCommand) CacheCommand() bool {
return false
}
2 changes: 1 addition & 1 deletion pkg/commands/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestExecuteCmd(t *testing.T) {

for _, test := range cmdTests {
cmd := CmdCommand{
&instructions.CmdCommand{
cmd: &instructions.CmdCommand{
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
PrependShell: test.prependShell,
CmdLine: test.cmdLine,
Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type DockerCommand interface {
// Return true if this command should be true
// Currently only true for RUN
CacheCommand() bool

// Return true if this command depends on the build context.
UsesContext() bool
}

func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

type CopyCommand struct {
BaseCommand
cmd *instructions.CopyCommand
buildcontext string
snapshotFiles []string
Expand Down Expand Up @@ -103,7 +104,6 @@ func (c *CopyCommand) String() string {
return c.cmd.String()
}

// CacheCommand returns true since this command should be cached
func (c *CopyCommand) CacheCommand() bool {
return false
func (c *CopyCommand) UsesContext() bool {
return true
}
11 changes: 1 addition & 10 deletions pkg/commands/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

type EntrypointCommand struct {
BaseCommand
cmd *instructions.EntrypointCommand
}

Expand All @@ -50,17 +51,7 @@ func (e *EntrypointCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerf
return nil
}

// FilesToSnapshot returns an empty array since this is a metadata command
func (e *EntrypointCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (e *EntrypointCommand) String() string {
return e.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (e *EntrypointCommand) CacheCommand() bool {
return false
}
2 changes: 1 addition & 1 deletion pkg/commands/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestEntrypointExecuteCmd(t *testing.T) {

for _, test := range entrypointTests {
cmd := EntrypointCommand{
&instructions.EntrypointCommand{
cmd: &instructions.EntrypointCommand{
ShellDependantCmdLine: instructions.ShellDependantCmdLine{
PrependShell: test.prependShell,
CmdLine: test.cmdLine,
Expand Down
11 changes: 1 addition & 10 deletions pkg/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

type EnvCommand struct {
BaseCommand
cmd *instructions.EnvCommand
}

Expand All @@ -34,17 +35,7 @@ func (e *EnvCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
return util.UpdateConfigEnv(newEnvs, config, replacementEnvs)
}

// We know that no files have changed, so return an empty array
func (e *EnvCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (e *EnvCommand) String() string {
return e.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (e *EnvCommand) CacheCommand() bool {
return false
}
2 changes: 1 addition & 1 deletion pkg/commands/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_EnvExecute(t *testing.T) {
}

envCmd := &EnvCommand{
&instructions.EnvCommand{
cmd: &instructions.EnvCommand{
Env: []instructions.KeyValuePair{
{
Key: "path",
Expand Down
10 changes: 1 addition & 9 deletions pkg/commands/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

type ExposeCommand struct {
BaseCommand
cmd *instructions.ExposeCommand
}

Expand Down Expand Up @@ -72,15 +73,6 @@ func validProtocol(protocol string) bool {
return false
}

func (r *ExposeCommand) FilesToSnapshot() []string {
return []string{}
}

func (r *ExposeCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *ExposeCommand) CacheCommand() bool {
return false
}
4 changes: 2 additions & 2 deletions pkg/commands/expose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestUpdateExposedPorts(t *testing.T) {
}

exposeCmd := &ExposeCommand{
&instructions.ExposeCommand{
cmd: &instructions.ExposeCommand{
Ports: ports,
},
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestInvalidProtocol(t *testing.T) {
}

exposeCmd := &ExposeCommand{
&instructions.ExposeCommand{
cmd: &instructions.ExposeCommand{
Ports: ports,
},
}
Expand Down
11 changes: 1 addition & 10 deletions pkg/commands/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

type HealthCheckCommand struct {
BaseCommand
cmd *instructions.HealthCheckCommand
}

Expand All @@ -34,17 +35,7 @@ func (h *HealthCheckCommand) ExecuteCommand(config *v1.Config, buildArgs *docker
return nil
}

// FilesToSnapshot returns an empty array since this is a metadata command
func (h *HealthCheckCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (h *HealthCheckCommand) String() string {
return h.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (h *HealthCheckCommand) CacheCommand() bool {
return false
}
11 changes: 1 addition & 10 deletions pkg/commands/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

type LabelCommand struct {
BaseCommand
cmd *instructions.LabelCommand
}

Expand Down Expand Up @@ -64,17 +65,7 @@ func updateLabels(labels []instructions.KeyValuePair, config *v1.Config, buildAr

}

// No files have changed, this command only touches metadata.
func (r *LabelCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (r *LabelCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *LabelCommand) CacheCommand() bool {
return false
}
11 changes: 1 addition & 10 deletions pkg/commands/onbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

type OnBuildCommand struct {
BaseCommand
cmd *instructions.OnbuildCommand
}

Expand All @@ -39,17 +40,7 @@ func (o *OnBuildCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile
return nil
}

// FilesToSnapshot returns that no files have changed, this command only touches metadata.
func (o *OnBuildCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (o *OnBuildCommand) String() string {
return o.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (o *OnBuildCommand) CacheCommand() bool {
return false
}
2 changes: 1 addition & 1 deletion pkg/commands/onbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestExecuteOnbuild(t *testing.T) {
}

onbuildCmd := &OnBuildCommand{
&instructions.OnbuildCommand{
cmd: &instructions.OnbuildCommand{
Expression: test.expression,
},
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
)

type RunCommand struct {
BaseCommand
cmd *instructions.RunCommand
}

Expand Down Expand Up @@ -142,12 +143,6 @@ func addDefaultHOME(u string, envs []string) []string {
return append(envs, home)
}

// FilesToSnapshot returns nil for this command because we don't know which files
// have changed, so we snapshot the entire system.
func (r *RunCommand) FilesToSnapshot() []string {
return nil
}

// String returns some information about the command for the image config
func (r *RunCommand) String() string {
return r.cmd.String()
Expand Down
11 changes: 1 addition & 10 deletions pkg/commands/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

type ShellCommand struct {
BaseCommand
cmd *instructions.ShellCommand
}

Expand All @@ -32,17 +33,7 @@ func (s *ShellCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.B
return nil
}

// FilesToSnapshot returns an empty array since this is a metadata command
func (s *ShellCommand) FilesToSnapshot() []string {
return []string{}
}

// String returns some information about the command for the image config history
func (s *ShellCommand) String() string {
return s.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (s *ShellCommand) CacheCommand() bool {
return false
}
2 changes: 1 addition & 1 deletion pkg/commands/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestShellExecuteCmd(t *testing.T) {

for _, test := range shellTests {
cmd := ShellCommand{
&instructions.ShellCommand{
cmd: &instructions.ShellCommand{
Shell: test.cmdLine,
},
}
Expand Down
Loading

0 comments on commit 9bd6f5b

Please sign in to comment.