Skip to content

Commit

Permalink
Add Init + Run commands to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dnnyjns committed May 21, 2019
1 parent d446a55 commit 51b6b9a
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 79 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
BINARY_NAME=migraine
BINARY_PATH=cli/main.go
MIGRAINES_DIR=./migraines

all: test build
build:
$(GOBUILD) -o $(BINARY_NAME) -v ${BINARY_PATH}
$(GOBUILD) -o $(BINARY_NAME) -v
test:
$(GOTEST) -v ./...
clean:
Expand Down
15 changes: 15 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/cobra"
)

func initCommand() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialize your migraines",
Run: func(cmd *cobra.Command, args []string) {
createRunner()
},
}
}
1 change: 0 additions & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func newCommand() *cobra.Command {
Short: "Generate a new migraine",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
createDir()
createTemplate(args[0])
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func Execute() error {
}

// Register Command
rootCmd.AddCommand(initCommand())
rootCmd.AddCommand(newCommand())
rootCmd.AddCommand(runCommand())

// Run Command
return rootCmd.Execute()
Expand Down
39 changes: 39 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/cobra"
)

func runCommand() *cobra.Command {
return &cobra.Command{
Use: "run",
Short: "Run your migraines",
Run: func(cmd *cobra.Command, _ []string) {
var (
b bytes.Buffer
args = []string{"run"}
)
runnerDir := fmt.Sprintf("%s/*.go", migraineDir)
matches, _ := filepath.Glob(runnerDir)
for _, match := range matches {
args = append(args, match)
}
c := exec.Command("go", args...)
c.Stdout = &b
c.Stderr = &b
if err := c.Run(); err != nil {
fmt.Println(fmt.Errorf("migraine run: %v", err))
fmt.Println(b.String())
os.Exit(1)
} else {
fmt.Println(b.String())
}
},
}
}
47 changes: 39 additions & 8 deletions cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,44 @@ type migraineTemplate struct {
}

var (
migraineDir = "./migraines"
templateStr = `package migraines
migraineDir = "./migraines"
migraineTemplateStr = `package main
import (
m "github.com/dnnyjns/migraine"
r "github.com/dnnyjns/migraine/runner"
"github.com/jinzhu/gorm"
)
// Migraine for {{.Name}}
func init() {
migraine := &m.Migraine{
r.Add(&r.Migraine{
Version: "{{.Version}}",
Perform: func(db *gorm.DB) {
Perform: func(db *gorm.DB) error {
},
})
}
`
runnerTemplateStr = []byte(`package main
import (
r "github.com/dnnyjns/migraine/runner"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func main() {
var db *gorm.DB
db, err := gorm.Open("postgres", "host=myhost port=myport user=gorm dbname=gorm password=mypassword")
if err != nil {
panic(err)
}
m.Add(migraine)
defer db.Close()
db.LogMode(true)
r.Run(db)
}
`
`)
)

func (m migraineTemplate) file() string {
Expand All @@ -54,13 +73,25 @@ func createDir() {
os.MkdirAll(migraineDir, os.ModePerm)
}

func createRunner() {
createDir()
f, err := os.Create(fmt.Sprintf("%s/runner.go", migraineDir))
defer f.Close()
_, err = f.Write(runnerTemplateStr)
if err != nil {
panic(err)
}
}

func createTemplate(name string) {
createDir()
m := &migraineTemplate{name: name}
tmpl, err := template.New("migraine.tmpl").Parse(templateStr)
tmpl, err := template.New("migraine").Parse(migraineTemplateStr)
if err != nil {
panic(err)
}
f, err := os.Create(m.file())
defer f.Close()
if err != nil {
panic(err)
}
Expand Down
File renamed without changes.
29 changes: 0 additions & 29 deletions migraine.go

This file was deleted.

13 changes: 13 additions & 0 deletions runner/migraine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Migraine struct {
Version string `gorm:"size:255;PRIMARY_KEY;NOT NULL"`
}

var (
internalMigraines migraines
)

func (m Migraine) Run(db *gorm.DB) {
var tx *gorm.DB
if m.DisableDDL {
Expand All @@ -31,3 +35,12 @@ func (m Migraine) Run(db *gorm.DB) {
panic(err)
}
}

func Add(m *Migraine) {
internalMigraines = append(internalMigraines, m)
}

func Run(db *gorm.DB) error {
db.AutoMigrate(&Migraine{})
return internalMigraines.Run(db)
}
33 changes: 29 additions & 4 deletions runner/migraines.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@ package runner

import (
"sort"

"github.com/jinzhu/gorm"
)

type Migraines []*Migraine
type migraines []*Migraine

func (m *migraines) Run(db *gorm.DB) error {
var (
ids = make([]string, len(*m))
persisted migraines
)

for i, migraine := range *m {
ids[i] = migraine.Version
}
db.Model(&Migraine{}).Where(ids).Order("version").Find(&persisted)

m.sort()
for _, migraine := range *m {
length := len(persisted)
i := sort.Search(length, func(i int) bool { return persisted[i].Version == migraine.Version })
if i == length {
migraine.Run(db)
}
}

return nil
}

func (ms Migraines) sort() {
sort.Slice(ms, func(i, j int) bool {
return ms[i].Version < ms[j].Version
func (m migraines) sort() {
sort.Slice(m, func(i, j int) bool {
return m[i].Version < m[j].Version
})
}
22 changes: 22 additions & 0 deletions runner/migraines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package runner

import (
"testing"
)

func TestSort(t *testing.T) {
m := migraines{
&Migraine{
Version: "2",
},
&Migraine{
Version: "1",
},
}

m.sort()

if m[0].Version != "1" {
t.Errorf("m[0].Version = %s; expected 1", m[0].Version)
}
}
35 changes: 0 additions & 35 deletions runner/runner.go

This file was deleted.

0 comments on commit 51b6b9a

Please sign in to comment.