-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from bmeg/feature/scanner
Sifter scanning commands
- Loading branch information
Showing
9 changed files
with
425 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,5 @@ linters: | |
- goimports | ||
- misspell | ||
- typecheck | ||
- golint | ||
- gosimple | ||
- govet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package graphplan | ||
|
||
import ( | ||
"log" | ||
"path/filepath" | ||
|
||
"github.com/bmeg/sifter/graphplan" | ||
"github.com/bmeg/sifter/playbook" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var outScriptDir = "" | ||
var outDataDir = "./" | ||
|
||
// Cmd is the declaration of the command line | ||
var Cmd = &cobra.Command{ | ||
Use: "graph-plan", | ||
Short: "Scan directory to plan operations", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
scriptPath, _ := filepath.Abs(args[0]) | ||
|
||
/* | ||
if outScriptDir != "" { | ||
baseDir, _ = filepath.Abs(outScriptDir) | ||
} else if len(args) > 1 { | ||
return fmt.Errorf("for multiple input directories, based dir must be defined") | ||
} | ||
_ = baseDir | ||
*/ | ||
outScriptDir, _ = filepath.Abs(outScriptDir) | ||
outDataDir, _ = filepath.Abs(outDataDir) | ||
|
||
outDataDir, _ = filepath.Rel(outScriptDir, outDataDir) | ||
|
||
pb := playbook.Playbook{} | ||
|
||
if sifterErr := playbook.ParseFile(scriptPath, &pb); sifterErr == nil { | ||
if len(pb.Pipelines) > 0 || len(pb.Inputs) > 0 { | ||
err := graphplan.NewGraphBuild( | ||
&pb, outScriptDir, outDataDir, | ||
) | ||
if err != nil { | ||
log.Printf("Error: %s\n", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
flags := Cmd.Flags() | ||
flags.StringVarP(&outScriptDir, "dir", "C", outScriptDir, "Change Directory for script base") | ||
flags.StringVarP(&outDataDir, "out", "o", outDataDir, "Change output Directory") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package scan | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bmeg/sifter/playbook" | ||
"github.com/bmeg/sifter/task" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var jsonOut = false | ||
var objectsOnly = false | ||
var baseDir = "" | ||
|
||
type Entry struct { | ||
ObjectType string `json:"objectType"` | ||
SifterFile string `json:"sifterFile"` | ||
Outfile string `json:"outFile"` | ||
} | ||
|
||
var ObjectCommand = &cobra.Command{ | ||
Use: "objects", | ||
Short: "Scan for outputs", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
scanDir := args[0] | ||
|
||
outputs := []Entry{} | ||
|
||
PathWalker(scanDir, func(pb *playbook.Playbook) { | ||
for pname, p := range pb.Pipelines { | ||
emitName := "" | ||
for _, s := range p { | ||
if s.Emit != nil { | ||
emitName = s.Emit.Name | ||
} | ||
} | ||
if emitName != "" { | ||
for _, s := range p { | ||
outdir := pb.GetDefaultOutDir() | ||
outname := fmt.Sprintf("%s.%s.%s.json.gz", pb.Name, pname, emitName) | ||
outpath := filepath.Join(outdir, outname) | ||
o := Entry{SifterFile: pb.GetPath(), Outfile: outpath} | ||
if s.ObjectValidate != nil { | ||
//outpath, _ = filepath.Rel(baseDir, outpath) | ||
//fmt.Printf("%s\t%s\n", s.ObjectValidate.Title, outpath) | ||
o.ObjectType = s.ObjectValidate.Title | ||
} | ||
if objectsOnly { | ||
if o.ObjectType != "" { | ||
outputs = append(outputs, o) | ||
} | ||
} else { | ||
outputs = append(outputs, o) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
if jsonOut { | ||
j := json.NewEncoder(os.Stdout) | ||
j.SetIndent("", " ") | ||
j.Encode(outputs) | ||
} else { | ||
for _, i := range outputs { | ||
fmt.Printf("%s\t%s\n", i.ObjectType, i.Outfile) | ||
} | ||
} | ||
|
||
return nil | ||
|
||
}, | ||
} | ||
|
||
type ScriptEntry struct { | ||
Name string `json:"name"` | ||
Path string `json:"path"` | ||
Inputs []string `json:"inputs"` | ||
Outputs []string `json:"outputs"` | ||
} | ||
|
||
func removeDuplicates(s []string) []string { | ||
t := map[string]bool{} | ||
|
||
for _, i := range s { | ||
t[i] = true | ||
} | ||
out := []string{} | ||
for k := range t { | ||
out = append(out, k) | ||
} | ||
return out | ||
} | ||
|
||
func relPathArray(basedir string, paths []string) []string { | ||
out := []string{} | ||
for _, i := range paths { | ||
if o, err := filepath.Rel(baseDir, i); err == nil { | ||
out = append(out, o) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
var ScriptCommand = &cobra.Command{ | ||
Use: "scripts", | ||
Short: "Scan for scripts", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
scanDir := args[0] | ||
|
||
scripts := []ScriptEntry{} | ||
|
||
if baseDir == "" { | ||
baseDir, _ = os.Getwd() | ||
} | ||
baseDir, _ = filepath.Abs(baseDir) | ||
//fmt.Printf("basedir: %s\n", baseDir) | ||
|
||
userInputs := map[string]string{} | ||
|
||
PathWalker(scanDir, func(pb *playbook.Playbook) { | ||
path := pb.GetPath() | ||
scriptDir := filepath.Dir(path) | ||
|
||
config, _ := pb.PrepConfig(userInputs, baseDir) | ||
|
||
task := task.NewTask(pb.Name, scriptDir, baseDir, pb.GetDefaultOutDir(), config) | ||
sourcePath, _ := filepath.Abs(path) | ||
|
||
cmdPath, _ := filepath.Rel(baseDir, sourcePath) | ||
|
||
inputs := []string{} | ||
outputs := []string{} | ||
for _, p := range pb.GetConfigFields() { | ||
if p.IsDir() || p.IsFile() { | ||
inputs = append(inputs, config[p.Name]) | ||
} | ||
} | ||
//inputs = append(inputs, sourcePath) | ||
|
||
sinks, _ := pb.GetOutputs(task) | ||
for _, v := range sinks { | ||
outputs = append(outputs, v...) | ||
} | ||
|
||
emitters, _ := pb.GetEmitters(task) | ||
for _, v := range emitters { | ||
outputs = append(outputs, v) | ||
} | ||
|
||
//for _, e := range pb.Inputs { | ||
//} | ||
|
||
s := ScriptEntry{ | ||
Path: cmdPath, | ||
Name: pb.Name, | ||
Outputs: relPathArray(baseDir, removeDuplicates(outputs)), | ||
Inputs: relPathArray(baseDir, removeDuplicates(inputs)), | ||
} | ||
scripts = append(scripts, s) | ||
}) | ||
|
||
if jsonOut { | ||
e := json.NewEncoder(os.Stdout) | ||
e.SetIndent("", " ") | ||
e.Encode(scripts) | ||
} else { | ||
for _, i := range scripts { | ||
fmt.Printf("%s\n", i) | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
// Cmd is the declaration of the command line | ||
var Cmd = &cobra.Command{ | ||
Use: "scan", | ||
Short: "Scan for scripts or objects", | ||
} | ||
|
||
func init() { | ||
Cmd.AddCommand(ObjectCommand) | ||
Cmd.AddCommand(ScriptCommand) | ||
|
||
objFlags := ObjectCommand.Flags() | ||
objFlags.BoolVarP(&objectsOnly, "objects", "s", objectsOnly, "Objects Only") | ||
objFlags.BoolVarP(&jsonOut, "json", "j", jsonOut, "Output JSON") | ||
|
||
scriptFlags := ScriptCommand.Flags() | ||
scriptFlags.StringVarP(&baseDir, "base", "b", baseDir, "Base Dir") | ||
scriptFlags.BoolVarP(&jsonOut, "json", "j", jsonOut, "Output JSON") | ||
|
||
} | ||
|
||
func PathWalker(baseDir string, userFunc func(*playbook.Playbook)) { | ||
filepath.Walk(baseDir, | ||
func(path string, info fs.FileInfo, err error) error { | ||
if strings.HasSuffix(path, ".yaml") { | ||
pb := playbook.Playbook{} | ||
if parseErr := playbook.ParseFile(path, &pb); parseErr == nil { | ||
userFunc(&pb) | ||
} | ||
} | ||
return nil | ||
}) | ||
} |
Oops, something went wrong.