Commit 3d5adcd1 authored by Jessica Yuen's avatar Jessica Yuen
Browse files

Realize AbsPath as simple strings


Signed-off-by: default avatarJessica Yuen <im.jessicayuen@gmail.com>
parent f198fb5e
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -104,7 +103,6 @@ var applyCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
c.ClientPool, c.Discovery, err = restClientPool(cmd, &env)
if err != nil {
......@@ -120,14 +118,14 @@ var applyCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
return err
}
return c.Run(objs, wd)
return c.Run(objs, cwd)
},
Long: `
The ` + "`apply`" + `command uses local manifest(s) to update (and optionally create)
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -66,7 +65,6 @@ var deleteCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
c.ClientPool, c.Discovery, err = restClientPool(cmd, &env)
if err != nil {
......@@ -82,7 +80,7 @@ var deleteCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -61,7 +61,6 @@ var diffCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
componentNames, err := flags.GetStringArray(flagComponent)
if err != nil {
......@@ -83,7 +82,7 @@ var diffCmd = &cobra.Command{
return err
}
c, err := initDiffCmd(appFs, cmd, wd, env1, env2, componentNames, diffStrategy)
c, err := initDiffCmd(appFs, cmd, cwd, env1, env2, componentNames, diffStrategy)
if err != nil {
return err
}
......@@ -141,7 +140,7 @@ ks diff dev -c redis
`,
}
func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd metadata.AbsPath, envFq1, envFq2 *string, files []string, diffStrategy string) (kubecfg.DiffCmd, error) {
func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd string, envFq1, envFq2 *string, files []string, diffStrategy string) (kubecfg.DiffCmd, error) {
const (
remote = "remote"
local = "local"
......@@ -186,7 +185,7 @@ func initDiffCmd(fs afero.Fs, cmd *cobra.Command, wd metadata.AbsPath, envFq1, e
}
// initDiffSingleEnv sets up configurations for diffing using one environment
func initDiffSingleEnv(fs afero.Fs, env, diffStrategy string, files []string, cmd *cobra.Command, wd metadata.AbsPath) (kubecfg.DiffCmd, error) {
func initDiffSingleEnv(fs afero.Fs, env, diffStrategy string, files []string, cmd *cobra.Command, wd string) (kubecfg.DiffCmd, error) {
c := kubecfg.DiffRemoteCmd{}
c.DiffStrategy = diffStrategy
c.Client = &kubecfg.Client{}
......
......@@ -125,9 +125,8 @@ var envAddCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -209,9 +208,8 @@ var envRmCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -258,9 +256,8 @@ var envListCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......@@ -301,9 +298,8 @@ var envSetCmd = &cobra.Command{
if err != nil {
return err
}
appRoot := metadata.AbsPath(appDir)
manager, err := metadata.Find(appRoot)
manager, err := metadata.Find(appDir)
if err != nil {
return err
}
......
......@@ -21,7 +21,6 @@ import (
"os"
"path/filepath"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
......@@ -62,13 +61,11 @@ var initCmd = &cobra.Command{
return err
}
path, err := genKsRoot(appName, wd, initDir)
appRoot, err := genKsRoot(appName, wd, initDir)
if err != nil {
return err
}
appRoot := metadata.AbsPath(path)
specFlag, err := flags.GetString(flagAPISpec)
if err != nil {
return err
......
......@@ -23,7 +23,6 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/metadata/parts"
str "github.com/ksonnet/ksonnet/strings"
"github.com/ksonnet/ksonnet/utils"
"github.com/spf13/cobra"
)
......@@ -102,9 +101,8 @@ var pkgInstallCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -166,9 +164,8 @@ var pkgDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -239,9 +236,8 @@ var pkgListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......
......@@ -27,7 +27,6 @@ import (
"github.com/ksonnet/ksonnet/prototype/snippet"
"github.com/ksonnet/ksonnet/prototype/snippet/jsonnet"
str "github.com/ksonnet/ksonnet/strings"
"github.com/ksonnet/ksonnet/utils"
"github.com/spf13/cobra"
)
......@@ -88,9 +87,8 @@ var prototypeListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -144,10 +142,9 @@ var prototypeDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
extProtos := prototype.SpecificationSchemas{}
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err == nil {
extProtos, err = manager.GetAllPrototypes()
if err != nil {
......@@ -256,10 +253,9 @@ var prototypePreviewCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
extProtos := prototype.SpecificationSchemas{}
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err == nil {
extProtos, err = manager.GetAllPrototypes()
if err != nil {
......@@ -357,7 +353,7 @@ var prototypeUseCmd = &cobra.Command{
if err != nil {
return err
}
manager, err := metadata.Find(metadata.AbsPath(cwd))
manager, err := metadata.Find(cwd)
if err != nil {
return fmt.Errorf("Command can only be run in a ksonnet application directory:\n\n%v", err)
}
......
......@@ -8,7 +8,6 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
str "github.com/ksonnet/ksonnet/strings"
"github.com/ksonnet/ksonnet/utils"
"github.com/spf13/cobra"
)
......@@ -77,9 +76,8 @@ var registryListCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -137,9 +135,8 @@ var registryDescribeCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
manager, err := metadata.Find(wd)
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
......
......@@ -333,9 +333,8 @@ func overrideCluster(envName string, clientConfig clientcmd.ClientConfig, overri
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
metadataManager, err := metadata.Find(wd)
metadataManager, err := metadata.Find(cwd)
if err != nil {
return err
}
......@@ -393,7 +392,7 @@ type cmdObjExpanderConfig struct {
cmd *cobra.Command
env string
components []string
cwd metadata.AbsPath
cwd string
}
// cmdObjExpander finds and expands templates for the family of commands of
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -97,13 +96,12 @@ ks show dev -c redis -c nginx-server
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
te := newCmdObjExpander(cmdObjExpanderConfig{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
)
......@@ -54,7 +53,6 @@ var validateCmd = &cobra.Command{
if err != nil {
return err
}
wd := metadata.AbsPath(cwd)
componentNames, err := flags.GetStringArray(flagComponent)
if err != nil {
......@@ -70,7 +68,7 @@ var validateCmd = &cobra.Command{
cmd: cmd,
env: env,
components: componentNames,
cwd: wd,
cwd: cwd,
})
objs, err := te.Expand()
if err != nil {
......
......@@ -24,12 +24,11 @@ func parseClusterSpec(specFlag string, fs afero.Fs) (ClusterSpec, error) {
case "version":
return &clusterSpecVersion{k8sVersion: split[1]}, nil
case "file":
abs, err := filepath.Abs(split[1])
p, err := filepath.Abs(split[1])
if err != nil {
return nil, err
}
absPath := AbsPath(abs)
return &clusterSpecFile{specPath: absPath, fs: fs}, nil
return &clusterSpecFile{specPath: p, fs: fs}, nil
case "url":
return &clusterSpecLive{apiServerURL: split[1]}, nil
default:
......@@ -38,7 +37,7 @@ func parseClusterSpec(specFlag string, fs afero.Fs) (ClusterSpec, error) {
}
type clusterSpecFile struct {
specPath AbsPath
specPath string
fs afero.Fs
}
......
......@@ -23,13 +23,14 @@ import (
param "github.com/ksonnet/ksonnet/metadata/params"
"github.com/ksonnet/ksonnet/prototype"
str "github.com/ksonnet/ksonnet/strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
func (m *manager) ComponentPaths() (AbsPaths, error) {
paths := AbsPaths{}
err := afero.Walk(m.appFS, string(m.componentsPath), func(p string, info os.FileInfo, err error) error {
func (m *manager) ComponentPaths() ([]string, error) {
paths := []string{}
err := afero.Walk(m.appFS, m.componentsPath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
......@@ -67,7 +68,7 @@ func (m *manager) CreateComponent(name string, text string, params param.Params,
return fmt.Errorf("Component name '%s' is not valid; must not contain punctuation, spaces, or begin or end with a slash", name)
}
componentPath := string(appendToAbsPath(m.componentsPath, name))
componentPath := str.AppendToPath(m.componentsPath, name)
switch templateType {
case prototype.YAML:
componentPath = componentPath + ".yaml"
......@@ -105,7 +106,7 @@ func (m *manager) DeleteComponent(name string) error {
}
// Build the new component/params.libsonnet file.
componentParamsFile, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
componentParamsFile, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -122,8 +123,8 @@ func (m *manager) DeleteComponent(name string) error {
return err
}
for _, env := range envs {
path := appendToAbsPath(m.environmentsPath, env.Name, paramsFileName)
envParamsFile, err := afero.ReadFile(m.appFS, string(path))
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
envParamsFile, err := afero.ReadFile(m.appFS, path)
if err != nil {
return err
}
......@@ -141,16 +142,16 @@ func (m *manager) DeleteComponent(name string) error {
// Remove the references in component/params.libsonnet.
log.Debugf("... deleting references in %s", m.componentParamsPath)
err = afero.WriteFile(m.appFS, string(m.componentParamsPath), []byte(componentJsonnet), defaultFilePermissions)
err = afero.WriteFile(m.appFS, m.componentParamsPath, []byte(componentJsonnet), defaultFilePermissions)
if err != nil {
return err
}
// Remove the component references in each environment's
// environment/<env>/params.libsonnet.
for _, env := range envs {
path := appendToAbsPath(m.environmentsPath, env.Name, paramsFileName)
path := str.AppendToPath(m.environmentsPath, env.Name, paramsFileName)
log.Debugf("... deleting references in %s", path)
err = afero.WriteFile(m.appFS, string(path), []byte(envJsonnets[env.Name]), defaultFilePermissions)
err = afero.WriteFile(m.appFS, path, []byte(envJsonnets[env.Name]), defaultFilePermissions)
if err != nil {
return err
}
......@@ -172,7 +173,7 @@ func (m *manager) DeleteComponent(name string) error {
}
func (m *manager) GetComponentParams(component string) (param.Params, error) {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return nil, err
}
......@@ -181,7 +182,7 @@ func (m *manager) GetComponentParams(component string) (param.Params, error) {
}
func (m *manager) GetAllComponentParams() (map[string]param.Params, error) {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return nil, err
}
......@@ -190,7 +191,7 @@ func (m *manager) GetAllComponentParams() (map[string]param.Params, error) {
}
func (m *manager) SetComponentParams(component string, params param.Params) error {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -200,7 +201,7 @@ func (m *manager) SetComponentParams(component string, params param.Params) erro
return err
}
return afero.WriteFile(m.appFS, string(m.componentParamsPath), []byte(jsonnet), defaultFilePermissions)
return afero.WriteFile(m.appFS, m.componentParamsPath, []byte(jsonnet), defaultFilePermissions)
}
func (m *manager) findComponentPath(name string) (string, error) {
......@@ -232,7 +233,7 @@ func (m *manager) findComponentPath(name string) (string, error) {
}
func (m *manager) writeComponentParams(componentName string, params param.Params) error {
text, err := afero.ReadFile(m.appFS, string(m.componentParamsPath))
text, err := afero.ReadFile(m.appFS, m.componentParamsPath)
if err != nil {
return err
}
......@@ -242,7 +243,7 @@ func (m *manager) writeComponentParams(componentName string, params param.Params
return err
}
return afero.WriteFile(m.appFS, string(m.componentParamsPath), []byte(appended), defaultFilePermissions)
return afero.WriteFile(m.appFS, m.componentParamsPath, []byte(appended), defaultFilePermissions)
}
func genComponentParamsContent() []byte {
......
......@@ -21,6 +21,8 @@ import (
"sort"
"strings"
"testing"
str "github.com/ksonnet/ksonnet/strings"
)
const (
......@@ -36,7 +38,7 @@ func populateComponentPaths(t *testing.T) *manager {
t.Fatalf("Failed to parse cluster spec: %v", err)
}
appPath := AbsPath(componentsPath)
appPath := componentsPath
reg := newMockRegistryManager("incubator")
m, err := initManager("componentPaths", appPath, spec, &mockAPIServer, &mockNamespace, reg, testFS)
if err != nil {
......@@ -44,29 +46,29 @@ func populateComponentPaths(t *testing.T) *manager {
}
// Create empty app file.
components := appendToAbsPath(appPath, componentsDir)
appFile1 := appendToAbsPath(components, componentFile1)
f1, err := testFS.OpenFile(string(appFile1), os.O_RDONLY|os.O_CREATE, 0777)
components := str.AppendToPath(appPath, componentsDir)
appFile1 := str.AppendToPath(components, componentFile1)
f1, err := testFS.OpenFile(appFile1, os.O_RDONLY|os.O_CREATE, 0777)
if err != nil {
t.Fatalf("Failed to touch app file '%s'\n%v", appFile1, err)
}
f1.Close()
// Create empty file in a nested directory.
appSubdir := appendToAbsPath(components, componentSubdir)
err = testFS.MkdirAll(string(appSubdir), os.ModePerm)
appSubdir := str.AppendToPath(components, componentSubdir)
err = testFS.MkdirAll(appSubdir, os.ModePerm)
if err != nil {
t.Fatalf("Failed to create directory '%s'\n%v", appSubdir, err)
}
appFile2 := appendToAbsPath(appSubdir, componentFile2)
f2, err := testFS.OpenFile(string(appFile2), os.O_RDONLY|os.O_CREATE, 0777)
appFile2 := str.AppendToPath(appSubdir, componentFile2)
f2, err := testFS.OpenFile(appFile2, os.O_RDONLY|os.O_CREATE, 0777)
if err != nil {
t.Fatalf("Failed to touch app file '%s'\n%v", appFile1, err)
}
f2.Close()
// Create a directory that won't be listed in the call to `ComponentPaths`.
unlistedDir := string(appendToAbsPath(components, "doNotListMe"))
unlistedDir := str.AppendToPath(components, "doNotListMe")
err = testFS.MkdirAll(unlistedDir, os.ModePerm)
if err != nil {
t.Fatalf("Failed to create directory '%s'\n%v", unlistedDir, err)
......
......@@ -22,6 +22,7 @@ import (
"path/filepath"
"github.com/ksonnet/ksonnet/metadata/app"
str "github.com/ksonnet/ksonnet/strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
......@@ -92,14 +93,14 @@ func (m *manager) createEnvironment(name, server, namespace string, extensionsLi
log.Infof("Creating environment '%s' with namespace '%s', pointing at server at address '%s'", name, namespace, server)
envPath := appendToAbsPath(m.environmentsPath, name)
err = m.appFS.MkdirAll(string(envPath), defaultFolderPermissions)
envPath := str.AppendToPath(m.environmentsPath, name)
err = m.appFS.MkdirAll(envPath, defaultFolderPermissions)
if err != nil {
return err
}
metadataPath := appendToAbsPath(envPath, metadataDirName)
err = m.appFS.MkdirAll(string(metadataPath), defaultFolderPermissions)
metadataPath := str.AppendToPath(envPath, metadataDirName)
err = m.appFS.MkdirAll(metadataPath, defaultFolderPermissions)
if err != nil {
return err
}
......@@ -107,40 +108,40 @@ func (m *manager) createEnvironment(name, server, namespace string, extensionsLi
log.Infof("Generating environment metadata at path '%s'", envPath)
metadata := []struct {
path AbsPath
path string
data []byte
}{
{
// schema file
appendToAbsPath(metadataPath, schemaFilename),
str.AppendToPath(metadataPath, schemaFilename),
specData,
},
{
// k8s file
appendToAbsPath(metadataPath, k8sLibFilename),
str.AppendToPath(metadataPath, k8sLibFilename),
k8sLibData,
},
{
// extensions file
appendToAbsPath(metadataPath, extensionsLibFilename),
str.AppendToPath(metadataPath, extensionsLibFilename),
extensionsLibData,
},
{
// environment base override file
appendToAbsPath(envPath, envFileName),
str.AppendToPath(envPath, envFileName),
m.generateOverrideData(),
},
{
// params file
appendToAbsPath(envPath, paramsFileName),
str.AppendToPath(envPath, paramsFileName),
m.generateParamsData(),
},
}
for _, a := range metadata {
fileName := path.Base(string(a.path))
fileName := path.Base(a.path)
log.Debugf("Generating '%s', length: %d", fileName, len(a.data))
if err = afero.WriteFile(m.appFS, string(a.path), a.data, defaultFilePermissions); err != nil {
if err = afero.WriteFile(m.appFS, a.path, a.data, defaultFilePermissions); err != nil {
log.Debugf("Failed to write '%s'", fileName)
return err
}
......@@ -177,12 +178,12 @@ func (m *manager) DeleteEnvironment(name string) error {
return fmt.Errorf("Environment '%s' does not exist", name)
}
envPath :=