Skip to content
Snippets Groups Projects
Commit a02fffb3 authored by bryanl's avatar bryanl
Browse files

Add support for custom directory location for ks app


* If custom dir is relative, it is created relative to CWD
* if custom dir is absolute, it is created at the specificed location
* if custom dir is not specified, a directory created at CWD/app-name is created

Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 9f2405a2
No related branches found
No related tags found
No related merge requests found
......@@ -16,9 +16,10 @@
package cmd
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
......@@ -26,6 +27,10 @@ import (
"github.com/spf13/cobra"
)
const (
flagInitDir = "dir"
)
func init() {
RootCmd.AddCommand(initCmd)
// TODO: We need to make this default to checking the `kubeconfig` file.
......@@ -33,6 +38,7 @@ func init() {
"Manually specify API version from OpenAPI schema, cluster, or Kubernetes version")
bindClientGoFlags(initCmd)
initCmd.Flags().String(flagInitDir, "", "Ksonnet application directory")
}
var initCmd = &cobra.Command{
......@@ -45,11 +51,22 @@ var initCmd = &cobra.Command{
}
appName := args[0]
appDir, err := os.Getwd()
wd, err := os.Getwd()
if err != nil {
return err
}
initDir, err := flags.GetString(flagInitDir)
if err != nil {
return err
}
path, err := genKsRoot(appName, wd, initDir)
if err != nil {
return err
}
appRoot := metadata.AbsPath(path.Join(appDir, appName))
appRoot := metadata.AbsPath(path)
specFlag, err := flags.GetString(flagAPISpec)
if err != nil {
......@@ -120,5 +137,29 @@ ks init app-name --api-spec=version:v1.7.1
# Initialize a ksonnet application, using the OpenAPI spec generated by a
# specific build of Kubernetes to generate 'ksonnet-lib'.
ks init app-name --api-spec=file:swagger.json`,
ks init app-name --api-spec=file:swagger.json
# Initialize a ksonnet application, using a custom location for the application
# directory.
ks init app-name --dir=custom-location`,
}
func genKsRoot(appName, ksDir, wd string) (string, error) {
if ksDir == "" {
return "", errors.New("invalid working directory")
}
if appName == "" && wd == "" {
return "", errors.New("invalid application name")
}
if wd != "" {
if filepath.IsAbs(wd) {
return wd, nil
}
return filepath.Abs(filepath.Join(ksDir, wd))
}
return filepath.Join(ksDir, appName), nil
}
package cmd
import "testing"
func Test_genKsRoot(t *testing.T) {
cases := []struct {
name string
appName string
ksDir string
wd string
expected string
isErr bool
}{
{name: "no wd", appName: "app", ksDir: "/root", expected: "/root/app"},
{name: "with abs wd", appName: "app", ksDir: "/root", wd: "/custom", expected: "/custom"},
{name: "with rel wd #1", appName: "app", ksDir: "/root", wd: "./custom", expected: "/root/custom"},
{name: "with rel wd #2", appName: "app", ksDir: "/root", wd: "custom", expected: "/root/custom"},
{name: "with rel wd #2", appName: "app", ksDir: "/root", wd: "../custom", expected: "/custom"},
{name: "missing ksDir", appName: "app", wd: "./custom", isErr: true},
{name: "missing appName and wd", ksDir: "/root", isErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := genKsRoot(tc.appName, tc.ksDir, tc.wd)
if tc.isErr {
if err == nil {
t.Errorf("genKsRoot expected error, but none was received")
}
} else {
if got != tc.expected {
t.Errorf("genKsRoot got %q; expected %q", got, tc.expected)
}
}
})
}
}
......@@ -36,7 +36,7 @@ of the current context specified in `$KUBECONFIG`.
```
ks init <app-name>
ks init <app-name> [flags]
```
### Examples
......@@ -65,6 +65,10 @@ ks init app-name --api-spec=version:v1.7.1
# Initialize a ksonnet application, using the OpenAPI spec generated by a
# specific build of Kubernetes to generate 'ksonnet-lib'.
ks init app-name --api-spec=file:swagger.json
# Initialize a ksonnet application, using a custom location for the application
# directory.
ks init app-name --dir=custom-location
```
### Options
......@@ -77,6 +81,7 @@ ks init app-name --api-spec=file:swagger.json
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
--context string The name of the kubeconfig context to use
--dir string Ksonnet application directory
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
--kubeconfig string Path to a kube config. Only required if out-of-cluster
-n, --namespace string If present, the namespace scope for this CLI request
......
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