From 79a894a695d4603af09e9cf4f8ae13dab2767eb6 Mon Sep 17 00:00:00 2001 From: Gorkem Ercan Date: Sat, 24 Feb 2024 15:08:40 -0500 Subject: [PATCH 1/2] Support accepted kitfile names Adds a method to find the accepted variations of the default kit file name on the context directory. Starts using it on build command. Updates the name of the default kit file const. --- pkg/cmd/build/cmd.go | 4 ++-- pkg/cmd/build/cmd_test.go | 2 +- pkg/cmd/export/export.go | 2 +- pkg/lib/constants/consts.go | 4 ++-- pkg/lib/filesystem/paths.go | 16 ++++++++++++++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/build/cmd.go b/pkg/cmd/build/cmd.go index 516664ed..2e4c15c7 100644 --- a/pkg/cmd/build/cmd.go +++ b/pkg/cmd/build/cmd.go @@ -6,10 +6,10 @@ package build import ( "context" "fmt" - "path" "strings" "kitops/pkg/lib/constants" + "kitops/pkg/lib/filesystem" "kitops/pkg/lib/storage" "kitops/pkg/output" @@ -73,7 +73,7 @@ func (opts *buildOptions) complete(ctx context.Context, flags *buildFlags, args opts.modelFile = flags.modelFile if opts.modelFile == "" { - opts.modelFile = path.Join(opts.contextDir, constants.DefaultModelFileName) + opts.modelFile = filesystem.FindKitfileInPath(opts.contextDir) } configHome, ok := ctx.Value(constants.ConfigKey{}).(string) diff --git a/pkg/cmd/build/cmd_test.go b/pkg/cmd/build/cmd_test.go index db510025..96c390ad 100644 --- a/pkg/cmd/build/cmd_test.go +++ b/pkg/cmd/build/cmd_test.go @@ -28,7 +28,7 @@ func TestBuildOptions_Complete(t *testing.T) { assert.NoError(t, err) assert.Equal(t, args[0], options.contextDir) - assert.Equal(t, filepath.Join(args[0], constants.DefaultModelFileName), options.modelFile) + assert.Equal(t, filepath.Join(args[0], constants.DefaultKitFileName), options.modelFile) } func TestBuildOptions_RunBuild(t *testing.T) { diff --git a/pkg/cmd/export/export.go b/pkg/cmd/export/export.go index 22f45438..52bc8b90 100644 --- a/pkg/cmd/export/export.go +++ b/pkg/cmd/export/export.go @@ -81,7 +81,7 @@ func exportModel(ctx context.Context, store oras.Target, ref *registry.Reference } func exportConfig(config *artifact.KitFile, exportDir string, overwrite bool) error { - configPath := path.Join(exportDir, constants.DefaultModelFileName) + configPath := path.Join(exportDir, constants.DefaultKitFileName) if fi, exists := filesystem.PathExists(configPath); exists { if !overwrite { return fmt.Errorf("failed to export config: path %s already exists", configPath) diff --git a/pkg/lib/constants/consts.go b/pkg/lib/constants/consts.go index 51d171a7..5a1b18ae 100644 --- a/pkg/lib/constants/consts.go +++ b/pkg/lib/constants/consts.go @@ -3,8 +3,8 @@ package constants type ConfigKey struct{} const ( - DefaultModelFileName = "Kitfile" - DefaultConfigSubdir = ".kitops" + DefaultKitFileName = "Kitfile" + DefaultConfigSubdir = ".kitops" // Media type for the model layer ModelLayerMediaType = "application/vnd.kitops.modelkit.model.v1.tar+gzip" diff --git a/pkg/lib/filesystem/paths.go b/pkg/lib/filesystem/paths.go index 852043ba..7840b29b 100644 --- a/pkg/lib/filesystem/paths.go +++ b/pkg/lib/filesystem/paths.go @@ -3,7 +3,9 @@ package filesystem import ( "fmt" "io/fs" + "kitops/pkg/lib/constants" "os" + "path" "path/filepath" "strings" ) @@ -48,3 +50,17 @@ func PathExists(path string) (fs.FileInfo, bool) { } return fi, true } + +// Searches for a kit file in the given context directory. +// It checks for accepted kitfile names and returns the path of the first found kitfile. +// If no kitfile is found, it returns the path (contextDir + kitfile) +// of the default kitfile. +func FindKitfileInPath(contextDir string) string { + var defaultKitFileNames = []string{"Kitfile", "kitfile", ".kitfile"} + for _, fileName := range defaultKitFileNames { + if _, exists := PathExists(filepath.Join(contextDir, fileName)); exists { + return path.Join(contextDir, fileName) + } + } + return path.Join(contextDir, constants.DefaultKitFileName) +} From 9c4404cef538fbfb4f9d853dc698748f06ca6885 Mon Sep 17 00:00:00 2001 From: Gorkem Ercan Date: Mon, 26 Feb 2024 10:31:05 -0500 Subject: [PATCH 2/2] fixup: use filepath --- pkg/lib/filesystem/paths.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/lib/filesystem/paths.go b/pkg/lib/filesystem/paths.go index 7840b29b..48774f40 100644 --- a/pkg/lib/filesystem/paths.go +++ b/pkg/lib/filesystem/paths.go @@ -5,7 +5,6 @@ import ( "io/fs" "kitops/pkg/lib/constants" "os" - "path" "path/filepath" "strings" ) @@ -59,8 +58,8 @@ func FindKitfileInPath(contextDir string) string { var defaultKitFileNames = []string{"Kitfile", "kitfile", ".kitfile"} for _, fileName := range defaultKitFileNames { if _, exists := PathExists(filepath.Join(contextDir, fileName)); exists { - return path.Join(contextDir, fileName) + return filepath.Join(contextDir, fileName) } } - return path.Join(contextDir, constants.DefaultKitFileName) + return filepath.Join(contextDir, constants.DefaultKitFileName) }