Skip to content

Commit

Permalink
Test gen migration
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 13, 2022
1 parent 9072d2d commit d151083
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM golang:1.16-alpine AS build

LABEL org.opencontainers.image.source https://github.com/ohkrab/krab

WORKDIR /src
COPY go.* ./
RUN go mod download
Expand Down
41 changes: 41 additions & 0 deletions krab/action_gen_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package krab

import (
"context"
"fmt"

"github.com/ohkrab/krab/cli"
)

// ActionGenMigration generates migration file.
type ActionGenMigration struct {
Ui cli.UI
Cmd *CmdGenMigration
}

func (a *ActionGenMigration) Help() string {
return `Usage: krab gen migration
Generates migration file.
`
}

func (a *ActionGenMigration) Synopsis() string {
return fmt.Sprintf("Generate migration file")
}

// Run in CLI.
func (a *ActionGenMigration) Run(args []string) int {
resp, err := a.Cmd.Do(context.Background(), CmdOpts{Inputs{"args": args}})
if err != nil {
a.Ui.Error(err.Error())
return 1
}

response := resp.(ResponseGenMigration)

a.Ui.Output(response.Path)
a.Ui.Output(response.Ref)

return 0
}
67 changes: 67 additions & 0 deletions krab/cmd_gen_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package krab

import (
"bytes"
"context"
"fmt"
"time"

"github.com/ohkrab/krab/krabhcl"
"github.com/spf13/afero"
)

// CmdGenMigration generates migation file.
type CmdGenMigration struct {
FS afero.Afero
}

// ResponseGenMigration json
type ResponseGenMigration struct {
Path string `json:"path"`
Ref string `json:"ref"`
}

func (c *CmdGenMigration) Addr() krabhcl.Addr { return krabhcl.NullAddr }

func (c *CmdGenMigration) Name() []string {
return append([]string{"gen", "migration"})
}

func (c *CmdGenMigration) HttpMethod() string { return "" }

func (c *CmdGenMigration) Do(ctx context.Context, o CmdOpts) (interface{}, error) {

return c.run(ctx, args)
}

func (c *CmdGenMigration) run(ctx context.Context) (ResponseGenMigration, error) {
result := ResponseGenMigration{}
buf := bytes.Buffer{}

ref := "create_animals"
result.Ref = fmt.Sprint("migration.", ref)
version := time.Now().UTC().Format("20060102_150405") // YYYYMMDD_HHMMSS

buf.WriteString(`migration "`)
buf.WriteString(ref)
buf.WriteString(`" {`)
buf.WriteString("\n")
buf.WriteString(` version = "`)
buf.WriteString(version)
buf.WriteString(`"`)
buf.WriteString("\n\n")
buf.WriteString(` up {`)
buf.WriteString("\n")
buf.WriteString(` }`)
buf.WriteString("\n\n")
buf.WriteString(` down {`)
buf.WriteString("\n")
buf.WriteString(` }`)
buf.WriteString("\n")
buf.WriteString(`}`)
buf.WriteString("\n")

c.FS.WriteFile("/tmp/migrate.krab.hcl", buf.Bytes(), 0644)

return result, nil
}
5 changes: 4 additions & 1 deletion krab/cmd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package krab
import (
"github.com/ohkrab/krab/krabdb"
"github.com/ohkrab/krab/krabenv"
"github.com/spf13/afero"
)

// CmdRegistry is a list of registred commands.
Expand All @@ -16,9 +17,11 @@ func (r *CmdRegistry) Register(c Cmd) {
}

// RegisterAll registers all commands in the registry.
func (r *CmdRegistry) RegisterAll(config *Config, conn krabdb.Connection) {
func (r *CmdRegistry) RegisterAll(config *Config, fs afero.Afero, conn krabdb.Connection) {
r.Register(&CmdVersion{})

r.Register(&CmdGenMigration{FS: fs})

for _, action := range config.Actions {
action := action

Expand Down
11 changes: 8 additions & 3 deletions krabcli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ func (a *App) RegisterAll() {
return &krab.ActionTestRun{Ui: a.Ui, Cmd: c}
})

case *krab.CmdGenMigration:
a.RegisterCmd(name, func() Command {
return &krab.ActionGenMigration{Ui: a.Ui, Cmd: c}
})

default:
panic(fmt.Sprintf("Not implemented: failed to register CLI action for command %T", c))
}
}

a.RegisterCmd("agent", func() Command {
return &CmdAgent{Registry: a.Registry}
})
// a.RegisterCmd("agent", func() Command {
// return &CmdAgent{Registry: a.Registry}
// })
}

func (a *App) Run() (int, error) {
Expand Down
2 changes: 2 additions & 0 deletions krabhcl/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Addr struct {
Labels []string
}

var NullAddr Addr = Addr{Keyword: "", Labels: []string{}}

func AddrFromStrings(s []string) Addr {
a := Addr{}
for i := 0; i < len(s); i++ {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
conn := &krabdb.DefaultConnection{}

registry := &krab.CmdRegistry{Commands: []krab.Cmd{}}
registry.RegisterAll(config, conn)
registry.RegisterAll(config, parser.FS, conn)
// agent := krabapi.Agent{Registry: registry}
// agent.Run()

Expand Down

0 comments on commit d151083

Please sign in to comment.