Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Update filesystem dependency injection (#233)
Browse files Browse the repository at this point in the history
Updates `fs` package to have `Asset`/`DotShip` `BasePath` instances
  • Loading branch information
ebramanti authored Aug 2, 2018
1 parent 899938f commit 145b125
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 35 deletions.
19 changes: 0 additions & 19 deletions pkg/constants/const.go

This file was deleted.

19 changes: 19 additions & 0 deletions pkg/constants/filepaths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package constants

// InstallerPrefixPath is the path prefix of installed assets
const InstallerPrefixPath = "installer"

// ShipPath is the default folder path of Ship configuration
const ShipPath = ".ship"

// OverlaysPrefixPath is the path prefix of overlays
const OverlaysPrefixPath = "overlays/ship"

// StatePath is the default state file path
const StatePath = ".ship/state.json"

// KustomizeHelmPath is the path used to store helm chart contents
const KustomizeHelmPath = ".ship/kustomize/chart"

// TempHelmValuesPath is the folder path used to store the updated values.yaml
const TempHelmValuesPath = ".ship/kustomize/tmp"
5 changes: 5 additions & 0 deletions pkg/constants/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

// ShouldUseUpdate is the message printed to the user when they attempt
// to use "ship init" with a present state file on disk
const ShouldUseUpdate = `To build on your progress, run "ship update"`
42 changes: 41 additions & 1 deletion pkg/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
package fs

import (
"github.com/replicatedhq/ship/pkg/constants"
"github.com/spf13/afero"
"github.com/spf13/viper"
)

func FromViper(v *viper.Viper) afero.Afero {
// NewBaseFilesystem creates a new Afero OS filesystem
func NewBaseFilesystem() afero.Afero {
return afero.Afero{Fs: afero.NewOsFs()}
}

// FilesystemParams is a struct that contains Filesystem configuration
type FilesystemParams struct {
AssetsPath string
DotShipPath string
}

// NewFilesystemParams creates a new FilesystemParams config object
func NewFilesystemParams(v *viper.Viper) FilesystemParams {
assetsPath := ""
if v.GetBool("is-app") {
assetsPath = constants.InstallerPrefixPath
}

return FilesystemParams{
AssetsPath: assetsPath,
DotShipPath: constants.ShipPath,
}
}

// Filesystems is a struct that returns multiple filesystems for use
// in ship execution
type Filesystems struct {
DotShip afero.Afero
Assets afero.Afero
}

// NewFilesystems creates a new Filesystems struct for use in ship execution
func NewFilesystems(fsp FilesystemParams, baseFilesystem afero.Afero) Filesystems {
return Filesystems{
DotShip: afero.Afero{
Fs: afero.NewBasePathFs(baseFilesystem.Fs, fsp.DotShipPath),
},
Assets: afero.Afero{
Fs: afero.NewBasePathFs(baseFilesystem.Fs, fsp.AssetsPath),
},
}
}
4 changes: 2 additions & 2 deletions pkg/lifecycle/render/amazonElasticKubernetesService/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func (r *LocalRenderer) Execute(

var assetsPath string
if asset.Dest != "" {
assetsPath = path.Join(constants.InstallerPrefix, asset.Dest)
assetsPath = path.Join(constants.InstallerPrefixPath, asset.Dest)
} else {
assetsPath = path.Join(constants.InstallerPrefix, "amazon_elastic_kubernetes_service.tf")
assetsPath = path.Join(constants.InstallerPrefixPath, "amazon_elastic_kubernetes_service.tf")
}

// write the inline spec
Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/render/docker/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (p *DefaultStep) Execute(
return errors.Wrapf(err, "write directory to %s", dest)
}
} else {
dest = filepath.Join(constants.InstallerPrefix, dest)
dest = filepath.Join(constants.InstallerPrefixPath, dest)
}

pullURL, err := p.URLResolver.ResolvePullURL(asset, meta)
Expand Down
8 changes: 4 additions & 4 deletions pkg/lifecycle/render/planner/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *CLIPlanner) Build(assets []api.Asset, configGroups []libyaml.ConfigGrou
p.Daemon.SetProgress(daemon.JSONProgress("build", progress))

if asset.Inline != nil {
asset.Inline.Dest = filepath.Join(constants.InstallerPrefix, asset.Inline.Dest)
asset.Inline.Dest = filepath.Join(constants.InstallerPrefixPath, asset.Inline.Dest)
evaluatedWhen, err := p.evalAssetWhen(debug, builder, asset, asset.Inline.AssetShared.When)
if err != nil {
return nil, err
Expand All @@ -60,7 +60,7 @@ func (p *CLIPlanner) Build(assets []api.Asset, configGroups []libyaml.ConfigGrou
} else if asset.Docker != nil {
// TODO: Improve handling of docker scheme, this is done because config not parsed yet
if !strings.HasPrefix(asset.Docker.Dest, "docker://") {
asset.Docker.Dest = filepath.Join(constants.InstallerPrefix, asset.Docker.Dest)
asset.Docker.Dest = filepath.Join(constants.InstallerPrefixPath, asset.Docker.Dest)
}
evaluatedWhen, err := p.evalAssetWhen(debug, builder, asset, asset.Docker.AssetShared.When)
if err != nil {
Expand All @@ -72,7 +72,7 @@ func (p *CLIPlanner) Build(assets []api.Asset, configGroups []libyaml.ConfigGrou
plan = append(plan, p.dockerStep(*asset.Docker, meta, templateContext, configGroups))
}
} else if asset.Helm != nil {
asset.Helm.Dest = filepath.Join(constants.InstallerPrefix, asset.Helm.Dest)
asset.Helm.Dest = filepath.Join(constants.InstallerPrefixPath, asset.Helm.Dest)
evaluatedWhen, err := p.evalAssetWhen(debug, builder, asset, asset.Helm.AssetShared.When)
if err != nil {
return nil, err
Expand All @@ -83,7 +83,7 @@ func (p *CLIPlanner) Build(assets []api.Asset, configGroups []libyaml.ConfigGrou
plan = append(plan, p.helmStep(*asset.Helm, meta, templateContext, configGroups))
}
} else if asset.DockerLayer != nil {
asset.DockerLayer.Dest = filepath.Join(constants.InstallerPrefix, asset.DockerLayer.Dest)
asset.DockerLayer.Dest = filepath.Join(constants.InstallerPrefixPath, asset.DockerLayer.Dest)
evaluatedWhen, err := p.evalAssetWhen(debug, builder, asset, asset.DockerLayer.AssetShared.When)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/lifecycle/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func (r *Renderer) Execute(ctx context.Context, release *api.Release, step *api.

debug.Log("event", "backup.start")
r.Daemon.SetProgress(ProgressBackup)
err = r.backupIfPresent(constants.InstallerPrefix)
err = r.backupIfPresent(constants.InstallerPrefixPath)
if err != nil {
return errors.Wrapf(err, "backup existing install directory %s", constants.InstallerPrefix)
return errors.Wrapf(err, "backup existing install directory %s", constants.InstallerPrefixPath)
}

r.Daemon.SetProgress(ProgressExecute)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lifecycle/render/terraform/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func (r *LocalRenderer) Execute(

var assetsPath string
if asset.Dest != "" && path.Ext(asset.Dest) == ".tf" {
assetsPath = path.Join(constants.InstallerPrefix, asset.Dest)
assetsPath = path.Join(constants.InstallerPrefixPath, asset.Dest)
} else {
assetsPath = path.Join(constants.InstallerPrefix, "main.tf")
assetsPath = path.Join(constants.InstallerPrefixPath, "main.tf")
}

// write the inline spec
Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/render/terraform/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRenderer(t *testing.T) {
return false
}
return asset.Contents == test.asset.Inline &&
strings.HasPrefix(asset.Dest, constants.InstallerPrefix)
strings.HasPrefix(asset.Dest, constants.InstallerPrefixPath)
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/lifecycle/terraform/terraformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewTerraformer(
PlanConfirmer: planner,
Terraform: func(cmdPath string) *exec.Cmd {
cmd := exec.Command("terraform")
cmd.Dir = path.Join(constants.InstallerPrefix, cmdPath)
cmd.Dir = path.Join(constants.InstallerPrefixPath, cmdPath)
return cmd
},
Viper: viper,
Expand Down
4 changes: 3 additions & 1 deletion pkg/ship/dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func buildInjector() (*dig.Container, error) {
viper.GetViper,
logger.FromViper,
ui.FromViper,
fs.FromViper,
fs.NewBaseFilesystem,
fs.NewFilesystemParams,
fs.NewFilesystems,
daemon.WebUIFactoryFactory,
filetree.NewLoader,
templates.NewBuilderBuilder,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ship/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (s *Ship) buildRelease(helmChartMetadata api.HelmChartMetadata) *api.Releas
},
{
Kustomize: &api.Kustomize{
BasePath: path.Join(constants.InstallerPrefix, helmChartMetadata.Name),
BasePath: path.Join(constants.InstallerPrefixPath, helmChartMetadata.Name),
Dest: path.Join("overlays", "ship"),
},
},
Expand Down

0 comments on commit 145b125

Please sign in to comment.