Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support accepted kitfile names #42

Merged
merged 2 commits into from
Feb 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cmd/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/build/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions pkg/lib/filesystem/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filesystem
import (
"fmt"
"io/fs"
"kitops/pkg/lib/constants"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -48,3 +49,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 filepath.Join(contextDir, fileName)
}
}
return filepath.Join(contextDir, constants.DefaultKitFileName)
}