Skip to content
Snippets Groups Projects
Unverified Commit 4f222781 authored by bryanl's avatar bryanl
Browse files

remove stringsAppendToPath


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent bb346e6f
No related branches found
No related tags found
No related merge requests found
......@@ -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"
)
......@@ -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 {
......
......@@ -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"
)
......@@ -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 {
......@@ -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
}
......
......@@ -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
......@@ -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
......
......@@ -21,7 +21,6 @@ import (
"path"
"path/filepath"
str "github.com/ksonnet/ksonnet/strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
......@@ -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
......
......@@ -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"
)
......@@ -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
......
......@@ -19,11 +19,10 @@ 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 {
......@@ -31,5 +30,5 @@ func (m *manager) registryPath(regManager registry.Registry) string {
if filepath.IsAbs(path) {
return path
}
return str.AppendToPath(m.registriesPath, regManager.RegistrySpecFilePath())
return filepath.Join(m.registriesPath, regManager.RegistrySpecFilePath())
}
// Copyright 2017 The kubecfg authors
//
//
// 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 kubecfg
// ComponentRmCmd stores the information necessary to remove a component from
// the ksonnet application.
type ComponentRmCmd struct {
component string
}
// NewComponentRmCmd acts as a constructor for ComponentRmCmd.
func NewComponentRmCmd(component string) *ComponentRmCmd {
return &ComponentRmCmd{component: component}
}
// Run executes the removing of the component.
func (c *ComponentRmCmd) Run() error {
manager, err := manager()
if err != nil {
return err
}
return manager.DeleteComponent(c.component)
}
......@@ -18,7 +18,6 @@ package strings
import (
"bytes"
"fmt"
"path"
"strings"
"github.com/PuerkitoBio/purell"
......@@ -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...)
}
......@@ -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)
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment