diff --git a/pkg/addon/addon.go b/pkg/addon/addon.go index c6620857c..9e20b16ee 100644 --- a/pkg/addon/addon.go +++ b/pkg/addon/addon.go @@ -196,6 +196,10 @@ func GetPatternFromItem(it Item, r AsyncReader, rootPath string) string { if strings.HasPrefix(relativePath, strings.Join([]string{rootPath, p.Value}, "/")) { return p.Value } + if strings.HasPrefix(relativePath, filepath.Join(rootPath, p.Value)) { + // for enable addon by load dir, compatible with linux or windows os + return p.Value + } } return "" } @@ -358,7 +362,7 @@ func readResFile(a *InstallPackage, reader AsyncReader, readPath string) error { if filename == "parameter.cue" { return nil } - file := ElementFile{Data: b, Name: path.Base(readPath)} + file := ElementFile{Data: b, Name: filepath.Base(readPath)} switch filepath.Ext(filename) { case ".cue": a.CUETemplates = append(a.CUETemplates, file) @@ -376,7 +380,7 @@ func readDefSchemaFile(a *InstallPackage, reader AsyncReader, readPath string) e if err != nil { return err } - a.DefSchemas = append(a.DefSchemas, ElementFile{Data: b, Name: path.Base(readPath)}) + a.DefSchemas = append(a.DefSchemas, ElementFile{Data: b, Name: filepath.Base(readPath)}) return nil } @@ -387,7 +391,7 @@ func readDefFile(a *UIData, reader AsyncReader, readPath string) error { return err } filename := path.Base(readPath) - file := ElementFile{Data: b, Name: path.Base(readPath)} + file := ElementFile{Data: b, Name: filepath.Base(readPath)} switch filepath.Ext(filename) { case ".cue": a.CUEDefinitions = append(a.CUEDefinitions, file) diff --git a/pkg/addon/helper.go b/pkg/addon/helper.go index a8c15a293..0493cef48 100644 --- a/pkg/addon/helper.go +++ b/pkg/addon/helper.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "fmt" + "path/filepath" "k8s.io/client-go/discovery" "k8s.io/klog/v2" @@ -92,7 +93,11 @@ func DisableAddon(ctx context.Context, cli client.Client, name string, config *r // EnableAddonByLocalDir enable an addon from local dir func EnableAddonByLocalDir(ctx context.Context, name string, dir string, cli client.Client, dc *discovery.DiscoveryClient, applicator apply.Applicator, config *rest.Config, args map[string]interface{}) error { - r := localReader{dir: dir, name: name} + absDir, err := filepath.Abs(dir) + if err != nil { + return err + } + r := localReader{dir: absDir, name: name} metas, err := r.ListAddonMeta() if err != nil { return err diff --git a/pkg/addon/reader_local.go b/pkg/addon/reader_local.go index 3facda102..d09b824eb 100644 --- a/pkg/addon/reader_local.go +++ b/pkg/addon/reader_local.go @@ -37,8 +37,10 @@ func (l localReader) ListAddonMeta() (map[string]SourceMeta, error) { } func (l localReader) ReadFile(path string) (string, error) { - file := strings.TrimPrefix(path, l.name+"/") - b, err := ioutil.ReadFile(filepath.Clean(filepath.Join(l.dir, file))) + path = strings.TrimPrefix(path, l.name+"/") + // for windows + path = strings.TrimPrefix(path, l.name+"\\") + b, err := ioutil.ReadFile(filepath.Clean(filepath.Join(l.dir, path))) if err != nil { return "", err } @@ -46,7 +48,8 @@ func (l localReader) ReadFile(path string) (string, error) { } func (l localReader) RelativePath(item Item) string { - return filepath.Join(l.name, strings.TrimPrefix(item.GetPath()+"/", l.dir)) + file := strings.TrimPrefix(item.GetPath(), filepath.Clean(l.dir)) + return filepath.Join(l.name, file) } func recursiveFetchFiles(path string, metas *SourceMeta) error { @@ -60,7 +63,7 @@ func recursiveFetchFiles(path string, metas *SourceMeta) error { return err } } else { - metas.Items = append(metas.Items, OSSItem{tp: "file", path: fmt.Sprintf("%s/%s", path, file.Name()), name: file.Name()}) + metas.Items = append(metas.Items, OSSItem{tp: "file", path: filepath.Join(path, file.Name()), name: file.Name()}) } } return nil