Skip to content

Commit

Permalink
recusrive scan on local
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <jdolitsky@gmail.com>
  • Loading branch information
jdolitsky committed Jan 13, 2019
1 parent 0ee7fb5 commit 110c52e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/chart-scanner/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func backendFromConfig(conf *config.Config) storage.Backend {

func localBackendFromConfig(conf *config.Config) storage.Backend {
crashIfConfigMissingVars(conf, []string{"storage.local.rootdir"})
return storage.Backend(storage.NewLocalFilesystemBackend(
return storage.Backend(NewLocalFilesystemBackendWithDir(
conf.GetString("storage.local.rootdir"),
))
}
Expand Down
43 changes: 43 additions & 0 deletions cmd/chart-scanner/local_backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

// Needed in order to scan directories for local filesystem backend

import (
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/chartmuseum/storage"
)

type LocalFilesystemBackendWithDir struct {
*storage.LocalFilesystemBackend
}

func (b LocalFilesystemBackendWithDir) ListObjects(prefix string) ([]storage.Object, error) {
var objects []storage.Object
files, err := ioutil.ReadDir(path.Join(b.RootDirectory, prefix))
if err != nil {
if os.IsNotExist(err) { // OK if the directory doesnt exist yet
err = nil
}
return objects, err
}
for _, f := range files {
object := storage.Object{Path: f.Name(), Content: []byte{}, LastModified: f.ModTime()}
objects = append(objects, object)
}
return objects, nil
}

// NewLocalFilesystemBackendWithDir creates a new instance of LocalFilesystemBackendWithDir
func NewLocalFilesystemBackendWithDir(rootDirectory string) *LocalFilesystemBackendWithDir {
absPath, err := filepath.Abs(rootDirectory)
if err != nil {
panic(err)
}
localFsBackend := &storage.LocalFilesystemBackend{RootDirectory: absPath}
b := &LocalFilesystemBackendWithDir{localFsBackend}
return b
}
6 changes: 2 additions & 4 deletions cmd/chart-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
)

func main() {
log.SetOutput(os.Stdout)
app := cli.NewApp()
app.Name = "chart-scanner"
app.Version = fmt.Sprintf("%s (build %s)", Version, Revision)
Expand All @@ -43,8 +44,5 @@ func cliHandler(c *cli.Context) {
log.Fatal(err)
}
backend := backendFromConfig(conf)
err = scanBackend(backend)
if err != nil {
log.Fatal(err)
}
scan(backend, "")
}
22 changes: 16 additions & 6 deletions cmd/chart-scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ package main

import (
"log"
"path"
"strings"

"github.com/chartmuseum/storage"
)

func scanBackend(backend storage.Backend) error {
objects, err := backend.ListObjects("")
if err != nil {
return err
func scan(backend storage.Backend, prefix string){
objects, _ := backend.ListObjects(prefix)
for _, object := range objects {
fullPath := path.Join(prefix, object.Path)
isChartPackage := strings.HasSuffix(fullPath, ".tgz")
if isChartPackage {
validateChartPackage(fullPath)
} else {
scan(backend, fullPath)
}
}
log.Println(len(objects))
return nil
}

func validateChartPackage(filePath string) {
log.Printf("%s is valid\n", filePath)
}

0 comments on commit 110c52e

Please sign in to comment.