Skip to content

Commit

Permalink
Merge pull request #24 from marema31/RECIPES
Browse files Browse the repository at this point in the history
Adding  recipes first iteration, the issue #25 does not impact the main features I want to achieve for a MVP
  • Loading branch information
marema31 authored Oct 31, 2019
2 parents 2dca1e7 + 91048fa commit 9e7a436
Show file tree
Hide file tree
Showing 72 changed files with 922 additions and 258 deletions.
15 changes: 12 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"fmt"

"github.com/marema31/kamino/recipe"
"github.com/marema31/kamino/step"
"github.com/spf13/cobra"
)

Expand All @@ -13,7 +15,8 @@ var (
Long: ``,
DisableFlagsInUseLine: true,
RunE: func(_ *cobra.Command, args []string) error {
return Apply(args)
cookbook := recipe.New(step.Factory{})
return Apply(cookbook, cfgFolder, names, types, args)
},
}
names = []string{}
Expand All @@ -27,7 +30,13 @@ func init() {
}

//Apply will run only the recipes with Apply type
func Apply(args []string) error {
fmt.Println("TODO: To be implemented")
func Apply(cookbook recipe.Cooker, cfgFolder string, names []string, types []string, args []string) error {
err := cookbook.Load(ctx, cfgFolder, args, names, types)
if err != nil {
return fmt.Errorf("error while loading the recipes: %v", err)
}
if cookbook.Do(ctx) {
return fmt.Errorf("a step had an error")
}
return nil
}
73 changes: 73 additions & 0 deletions cmd/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd_test

import (
"context"
"fmt"
"testing"

"github.com/marema31/kamino/cmd"
)

type mockedCookbook struct {
called bool
errorLoad error
doReturnValue bool
}

//Do manage the runnning of the cookbook
func (ck *mockedCookbook) Do(ctx context.Context) bool {
ck.called = true
return ck.doReturnValue
}

// Load the step file and returns the priority and a list of steper for this file
func (ck *mockedCookbook) Load(ctx context.Context, path string, recipes []string, stepNames []string, stepTypes []string) error {
ck.called = false
return ck.errorLoad
}

// Statistics return statistics on the cookbook
func (ck *mockedCookbook) Statistics() (map[string][]int, int) {
result := make(map[string][]int)
var total int
return result, total

}

func TestApplyOk(t *testing.T) {
ck := &mockedCookbook{}
err := cmd.Apply(ck, "testdata/good", []string{"recipe1ok", "recipe2ok"}, nil, nil)
if err != nil {
t.Errorf("Load should not returns an error, returned: %v", err)
}

if !ck.called {
t.Errorf("Do should be called")
}
}

func TestApplyLoadError(t *testing.T) {
ck := &mockedCookbook{}
ck.errorLoad = fmt.Errorf("fake error")
err := cmd.Apply(ck, "testdata/good", []string{"recipe1ok", "recipe2ok"}, nil, nil)
if err == nil {
t.Errorf("Load should returns an error")
}

if ck.called {
t.Errorf("Do should not be called")
}
}

func TestApplyDoError(t *testing.T) {
ck := &mockedCookbook{}
ck.doReturnValue = true
err := cmd.Apply(ck, "testdata/good", []string{"recipe1ok", "recipe2ok"}, nil, nil)
if err == nil {
t.Errorf("Load should returns an error")
}

if !ck.called {
t.Errorf("Do should be called")
}
}
8 changes: 3 additions & 5 deletions cmd/migrate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package migrate

import (
"context"
"fmt"

"github.com/spf13/cobra"
)

var admin, user bool
var ctx context.Context

//TODO:var ctx context.Context

//AddCommands adds all subcommands to RootCmd
func AddCommands(c context.Context, cmd *cobra.Command) {
ctx = c //Save context only to make it accessible to sub-command
//TODO: ctx = c //Save context only to make it accessible to sub-command
cmd.AddCommand(
NewMigrateCommand(),
)
//TODO: remove this line when ctx will be used elsewhere
fmt.Print(ctx)
}

//NewMigrateCommand declare the migration sub commands
Expand Down
30 changes: 13 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package cmd

import (
"context"
"fmt"
"os"
"time"

"github.com/marema31/kamino/cmd/migrate"

"github.com/spf13/cobra"
)

var (
ctx context.Context
cfgFolder string
dryRun bool
quiet bool
environment string
instances []string
ctx context.Context
cfgFolder string
dryRun bool
quiet bool
//TODO: use this for the subcommands
tags []string
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -42,12 +41,10 @@ var rootCmd = &cobra.Command{

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(c context.Context) {
ctx = c //Store the context for all sub-command definition
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
func Execute(c context.Context) error {
ctx = c //Store the context for all sub-command definition
time.Sleep(60 * time.Second) //TODO: remove after cancellation
return rootCmd.Execute()
}

func init() {
Expand All @@ -56,11 +53,10 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFolder, "config", "c", "", "config folder (default is $HOME/.kamino.d)")
rootCmd.PersistentFlags().StringVarP(&cfgFolder, "config", "c", "", "config folder")
rootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "list action only do not do them")
rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "do not print to screen")
rootCmd.PersistentFlags().StringVarP(&environment, "environment", "e", "", "database environment (by default the only existing environment)")
rootCmd.PersistentFlags().StringSliceVarP(&instances, "instances", "i", []string{}, "comma separated list of instance (default is all the instances)")
rootCmd.PersistentFlags().StringSliceVarP(&tags, "tags", "T", []string{}, "comma separated list of tags to filter the calculated impacted datasources")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand Down
10 changes: 5 additions & 5 deletions datasource/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func setupDatabaseTest() *Datasources {

func TestLoadMysqlCompleteEngine(t *testing.T) {
dss := setupDatabaseTest()
ds, err := dss.load("testdata/good/datasources", "mysqlcomplete")
ds, err := dss.load("testdata/good", "mysqlcomplete")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestLoadMysqlCompleteEngine(t *testing.T) {

func TestLoadMysqlMinimalEngine(t *testing.T) {
dss := setupDatabaseTest()
ds, err := dss.load("testdata/good/datasources", "mysqlminimal")
ds, err := dss.load("testdata/good", "mysqlminimal")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestLoadMysqlMinimalEngine(t *testing.T) {

func TestLoadPostgresCompleteEngine(t *testing.T) {
dss := setupDatabaseTest()
ds, err := dss.load("testdata/good/datasources", "postgrescomplete")
ds, err := dss.load("testdata/good", "postgrescomplete")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestLoadPostgresCompleteEngine(t *testing.T) {

func TestLoadPostgresMinimalEngine(t *testing.T) {
dss := setupDatabaseTest()
ds, err := dss.load("testdata/good/datasources", "postgresminimal")
ds, err := dss.load("testdata/good", "postgresminimal")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestLoadPostgresMinimalEngine(t *testing.T) {

func TestLoadNoDatabase(t *testing.T) {
dss := setupDatabaseTest()
_, err := dss.load("testdata/fail/datasources", "nodatabase")
_, err := dss.load("testdata/fail", "nodatabase")
if err == nil {
t.Errorf("Load should returns an error")
}
Expand Down
6 changes: 5 additions & 1 deletion datasource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import (
)

// load a dile type datasource from the viper configuration
func loadFileDatasource(filename string, v *viper.Viper, engine Engine) (Datasource, error) {
func loadFileDatasource(recipePath string, filename string, v *viper.Viper, engine Engine) (Datasource, error) {
var ds Datasource
ds.dstype = File
ds.engine = engine
ds.name = filename
ds.inline = v.GetString("inline")
ds.filePath = v.GetString("file")
if ds.filePath != "" && ds.filePath != "-" && ds.filePath[0] != '/' {
ds.filePath = filepath.Join(recipePath, ds.filePath)
}

ds.url = v.GetString("URL")
if ds.filePath == "" && ds.url == "" && ds.inline == "" {
return Datasource{}, fmt.Errorf("the datasource %s does not provide the file path or URL", ds.name)
Expand Down
28 changes: 14 additions & 14 deletions datasource/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func setupFileTest() *Datasources {
}
func TestLoadCsvEngine(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "csv")
ds, err := dss.load("testdata/good", "csv")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -24,7 +24,7 @@ func TestLoadCsvEngine(t *testing.T) {
if ds.engine != CSV {
t.Errorf("Should be recognized as CSV datasource but was recognized as '%s'", EngineToString(ds.GetEngine()))
}
if ds.filePath != "tmp/file.csv" {
if ds.filePath != "testdata/good/tmp/file.csv" {
t.Errorf("The file path is '%s'", ds.filePath)
}

Expand All @@ -43,7 +43,7 @@ func TestLoadCsvEngine(t *testing.T) {

func TestLoadZipCsvEngine(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "zipcsv")
ds, err := dss.load("testdata/good", "zipcsv")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -55,7 +55,7 @@ func TestLoadZipCsvEngine(t *testing.T) {
if ds.engine != CSV {
t.Errorf("Should be recognized as CSV datasource but was recognized as '%s'", EngineToString(ds.GetEngine()))
}
if ds.filePath != "tmp/file.zip" {
if ds.filePath != "testdata/good/tmp/file.zip" {
t.Errorf("The file path is '%s'", ds.filePath)
}

Expand All @@ -74,7 +74,7 @@ func TestLoadZipCsvEngine(t *testing.T) {

func TestLoadGZipCsvEngine(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "gzipcsv")
ds, err := dss.load("testdata/good", "gzipcsv")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -86,7 +86,7 @@ func TestLoadGZipCsvEngine(t *testing.T) {
if ds.engine != CSV {
t.Errorf("Should be recognized as CSV datasource but was recognized as '%s'", EngineToString(ds.GetEngine()))
}
if ds.filePath != "tmp/file.csv.gz" {
if ds.filePath != "testdata/good/tmp/file.csv.gz" {
t.Errorf("The file path is '%s'", ds.filePath)
}

Expand All @@ -105,7 +105,7 @@ func TestLoadGZipCsvEngine(t *testing.T) {

func TestLoadYamlEngine(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "yaml")
ds, err := dss.load("testdata/good", "yaml")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -117,7 +117,7 @@ func TestLoadYamlEngine(t *testing.T) {
if ds.engine != YAML {
t.Errorf("Should be recognized as CSV datasource but was recognized as '%s'", EngineToString(ds.GetEngine()))
}
if ds.filePath != "tmp/file.yaml" {
if ds.filePath != "testdata/good/tmp/file.yaml" {
t.Errorf("The file path is '%s'", ds.filePath)
}

Expand All @@ -136,7 +136,7 @@ func TestLoadYamlEngine(t *testing.T) {

func TestLoadJsonEngine(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "json")
ds, err := dss.load("testdata/good", "json")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -148,7 +148,7 @@ func TestLoadJsonEngine(t *testing.T) {
if ds.engine != JSON {
t.Errorf("Should be recognized as JSON datasource but was recognized as '%s'", EngineToString(ds.GetEngine()))
}
if ds.filePath != "tmp/file.json" {
if ds.filePath != "testdata/good/tmp/file.json" {
t.Errorf("The file path is '%s'", ds.filePath)
}

Expand All @@ -167,7 +167,7 @@ func TestLoadJsonEngine(t *testing.T) {

func TestLoadStdio(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "stdio")
ds, err := dss.load("testdata/good", "stdio")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -179,7 +179,7 @@ func TestLoadStdio(t *testing.T) {

func TestLoadURL(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "url")
ds, err := dss.load("testdata/good", "url")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -191,7 +191,7 @@ func TestLoadURL(t *testing.T) {

func TestLoadInline(t *testing.T) {
dss := setupFileTest()
ds, err := dss.load("testdata/good/datasources", "inline")
ds, err := dss.load("testdata/good", "inline")
if err != nil {
t.Errorf("Load returns an error %v", err)
}
Expand All @@ -203,7 +203,7 @@ func TestLoadInline(t *testing.T) {

func TestLoadNoPath(t *testing.T) {
dss := setupFileTest()
_, err := dss.load("testdata/fail/datasources", "nopath")
_, err := dss.load("testdata/fail", "nopath")
if err == nil {
t.Errorf("Load should returns an error")
}
Expand Down
Loading

0 comments on commit 9e7a436

Please sign in to comment.