Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

remove stringsAppendToPath #404

Merged
merged 1 commit into from Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 0 additions & 85 deletions metadata/component.go
Expand Up @@ -23,9 +23,7 @@ import (
"github.com/ksonnet/ksonnet/metadata/app"
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/prototype"
str "github.com/ksonnet/ksonnet/strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -87,89 +85,6 @@ func (m *manager) CreateComponent(name string, text string, params param.Params,
return nil
}

// DeleteComponent removes the component file and all references.
// Write operations will happen at the end to minimal-ize failures that leave
// the directory structure in a half-finished state.
func (m *manager) DeleteComponent(name string) error {
ksApp, err := m.App()
if err != nil {
return err
}

componentPath, err := component.Path(ksApp, name)
if err != nil {
return err
}

ns, _ := component.ExtractNamespacedComponent(ksApp, name)

// Build the new component/params.libsonnet file.
componentParamsFile, err := afero.ReadFile(m.appFS, ns.ParamsPath())
if err != nil {
return err
}
componentJsonnet, err := param.DeleteComponent(name, string(componentParamsFile))
if err != nil {
return err
}

// Build the new environment/<env>/params.libsonnet files.
// environment name -> jsonnet
envJsonnets := make(map[string]string)
envs, err := ksApp.Environments()
if err != nil {
return err
}
for _, env := range envs {
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
envParamsFile, err := afero.ReadFile(m.appFS, path)
if err != nil {
return err
}
jsonnet, err := param.DeleteEnvironmentComponent(name, string(envParamsFile))
if err != nil {
return err
}
envJsonnets[env.Name] = jsonnet
}

//
// Delete the component references.
//
log.Infof("Removing component parameter references ...")

// Remove the references in component/params.libsonnet.
log.Debugf("... deleting references in %s", m.componentParamsPath)
err = afero.WriteFile(m.appFS, ns.ParamsPath(), []byte(componentJsonnet), app.DefaultFilePermissions)
if err != nil {
return err
}
// Remove the component references in each environment's
// environment/<env>/params.libsonnet.
for _, env := range envs {
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
log.Debugf("... deleting references in %s", path)
err = afero.WriteFile(m.appFS, path, []byte(envJsonnets[env.Name]), app.DefaultFilePermissions)
if err != nil {
return err
}
}

//
// Delete the component file in components/.
//
log.Infof("Deleting component '%s' at path '%s'", name, componentPath)
if err := m.appFS.Remove(componentPath); err != nil {
return err
}

// TODO: Remove,
// references in main.jsonnet.
// component references in other component files (feature does not yet exist).
log.Infof("Successfully deleted component '%s'", name)
return nil
}

func (m *manager) GetComponentParams(component string) (param.Params, error) {
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
Expand Down
19 changes: 5 additions & 14 deletions metadata/environment.go
Expand Up @@ -16,8 +16,9 @@
package metadata

import (
"path/filepath"

"github.com/ksonnet/ksonnet/pkg/env"
str "github.com/ksonnet/ksonnet/strings"

param "github.com/ksonnet/ksonnet/metadata/params"
)
Expand All @@ -33,16 +34,6 @@ var (
envCreate = env.Create
)

// func (m *manager) DeleteEnvironment(name string) error {
// a, err := m.App()
// if err != nil {
// return err
// }

// // TODO: move this to actions
// return env.Delete(a, name, false)
// }

func (m *manager) GetEnvironmentParams(name, nsName string) (map[string]param.Params, error) {
a, err := m.App()
if err != nil {
Expand Down Expand Up @@ -76,12 +67,12 @@ func (m *manager) EnvPaths(env string) (libPath, mainPath, paramsPath string, er
}

func (m *manager) makeEnvPaths(env string) (mainPath, paramsPath string) {
envPath := str.AppendToPath(m.environmentsPath, env)
envPath := filepath.Join(m.environmentsPath, env)

// main.jsonnet file
mainPath = str.AppendToPath(envPath, envFileName)
mainPath = filepath.Join(envPath, envFileName)
// params.libsonnet file
paramsPath = str.AppendToPath(envPath, componentParamsFile)
paramsPath = filepath.Join(envPath, componentParamsFile)

return
}
Expand Down
4 changes: 0 additions & 4 deletions metadata/interface.go
Expand Up @@ -39,7 +39,6 @@ type Manager interface {
ComponentPaths() ([]string, error)
GetAllComponents() ([]component.Component, error)
CreateComponent(name string, text string, params param.Params, templateType prototype.TemplateType) error
DeleteComponent(name string) error

// Params API.
SetComponentParams(component string, params param.Params) error
Expand All @@ -51,9 +50,6 @@ type Manager interface {
// i.e.: "nginx" => {"replicas" => 1, "name": "nginx"}
GetEnvironmentParams(name, nsName string) (map[string]param.Params, error)
SetEnvironmentParams(env, component string, params param.Params) error

// Environment API.
// DeleteEnvironment(name string) error
}

// Find will recursively search the current directory and its parents for a
Expand Down
3 changes: 1 addition & 2 deletions metadata/lib/lib.go
Expand Up @@ -21,7 +21,6 @@ import (
"path"
"path/filepath"

str "github.com/ksonnet/ksonnet/strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"

Expand Down Expand Up @@ -143,7 +142,7 @@ func (m *Manager) GenerateLibData(useVersionPath bool) error {
// GetLibPath returns the absolute path pointing to the directory with the
// metadata files for the provided k8sVersion.
func (m *Manager) GetLibPath(useVersionPath bool) (string, error) {
path := str.AppendToPath(m.libPath, m.K8sVersion)
path := filepath.Join(m.libPath, m.K8sVersion)
ok, err := afero.DirExists(m.fs, string(path))
if err != nil {
return "", err
Expand Down
23 changes: 11 additions & 12 deletions metadata/manager.go
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"

"github.com/ksonnet/ksonnet/metadata/app"
str "github.com/ksonnet/ksonnet/strings"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -116,27 +115,27 @@ func newManager(rootPath string, appFS afero.Fs) (*manager, error) {
if err != nil {
return nil, err
}
userRootPath := str.AppendToPath(usr.HomeDir, userKsonnetRootDir)
userRootPath := filepath.Join(usr.HomeDir, userKsonnetRootDir)

m := &manager{
appFS: appFS,

// Application paths.
rootPath: rootPath,
ksonnetPath: str.AppendToPath(rootPath, ksonnetDir),
registriesPath: str.AppendToPath(rootPath, registriesDir),
libPath: str.AppendToPath(rootPath, libDir),
componentsPath: str.AppendToPath(rootPath, componentsDir),
environmentsPath: str.AppendToPath(rootPath, environmentsDir),
vendorPath: str.AppendToPath(rootPath, vendorDir),
ksonnetPath: filepath.Join(rootPath, ksonnetDir),
registriesPath: filepath.Join(rootPath, registriesDir),
libPath: filepath.Join(rootPath, libDir),
componentsPath: filepath.Join(rootPath, componentsDir),
environmentsPath: filepath.Join(rootPath, environmentsDir),
vendorPath: filepath.Join(rootPath, vendorDir),

componentParamsPath: str.AppendToPath(rootPath, componentsDir, componentParamsFile),
baseLibsonnetPath: str.AppendToPath(rootPath, environmentsDir, baseLibsonnetFile),
appYAMLPath: str.AppendToPath(rootPath, appYAMLFile),
componentParamsPath: filepath.Join(rootPath, componentsDir, componentParamsFile),
baseLibsonnetPath: filepath.Join(rootPath, environmentsDir, baseLibsonnetFile),
appYAMLPath: filepath.Join(rootPath, appYAMLFile),

// User-level paths.
userKsonnetRootPath: userRootPath,
pkgSrcCachePath: str.AppendToPath(userRootPath, pkgSrcCacheDir),
pkgSrcCachePath: filepath.Join(userRootPath, pkgSrcCacheDir),
}

return m, nil
Expand Down
5 changes: 2 additions & 3 deletions metadata/registry.go
Expand Up @@ -19,17 +19,16 @@ import (
"path/filepath"

"github.com/ksonnet/ksonnet/pkg/registry"
str "github.com/ksonnet/ksonnet/strings"
)

func (m *manager) registryDir(regManager registry.Registry) string {
return str.AppendToPath(m.registriesPath, regManager.RegistrySpecDir())
return filepath.Join(m.registriesPath, regManager.RegistrySpecDir())
}

func (m *manager) registryPath(regManager registry.Registry) string {
path := regManager.RegistrySpecFilePath()
if filepath.IsAbs(path) {
return path
}
return str.AppendToPath(m.registriesPath, regManager.RegistrySpecFilePath())
return filepath.Join(m.registriesPath, regManager.RegistrySpecFilePath())
}
37 changes: 0 additions & 37 deletions pkg/kubecfg/component.go

This file was deleted.

7 changes: 0 additions & 7 deletions strings/strings.go
Expand Up @@ -18,7 +18,6 @@ package strings
import (
"bytes"
"fmt"
"path"
"strings"

"github.com/PuerkitoBio/purell"
Expand Down Expand Up @@ -163,9 +162,3 @@ func padRows(rows []Row) ([]FormattedRow, error) {

return result, nil
}

// AppendToPath appends one or more paths to the specified original path.
func AppendToPath(originalPath string, toAppend ...string) string {
paths := append([]string{originalPath}, toAppend...)
return path.Join(paths...)
}
29 changes: 0 additions & 29 deletions strings/strings_test.go
Expand Up @@ -232,32 +232,3 @@ Hi World
require.EqualValues(t, test.expected, padded)
}
}

func TestAppendToPath(t *testing.T) {
tests := []struct {
originalPath string
toAppend string
expected string
}{
{
originalPath: "host/path/",
toAppend: "appended",
expected: "host/path/appended",
},
{
originalPath: "host/path",
toAppend: "appended/",
expected: "host/path/appended",
},
{
originalPath: "host/path/",
toAppend: "//appended//",
expected: "host/path/appended",
},
}
for _, test := range tests {
result := AppendToPath(test.originalPath, test.toAppend)

require.EqualValues(t, test.expected, result)
}
}