Skip to content

Commit

Permalink
feat: Add action description to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Nov 18, 2023
1 parent 991e51c commit d853ca1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
14 changes: 14 additions & 0 deletions krab/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Action struct {

Arguments *Arguments

Description string
SQL string
Transaction bool // wrap operation in transaction
}
Expand All @@ -41,6 +42,10 @@ var schemaAction = &hcl.BodySchema{
Name: "transaction",
Required: false,
},
{
Name: "description",
Required: true,
},
},
}

Expand Down Expand Up @@ -81,6 +86,14 @@ func (a *Action) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
}
a.SQL = val

case "description":
expr := krabhcl.Expression{Expr: v.Expr, EvalContext: ctx}
val, err := expr.String()
if err != nil {
return err
}
a.Description = val

case "transaction":
expr := krabhcl.Expression{Expr: v.Expr, EvalContext: ctx}
val, err := expr.Bool()
Expand All @@ -101,6 +114,7 @@ func (a *Action) Validate() error {
return ErrorCoalesce(
ValidateRefName(a.Namespace),
ValidateRefName(a.RefName),
ValidateStringNonEmpty("description", a.Description),
)
}

Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/actions/actions.krab.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
action "db" "create" {
transaction = false
description = "Create a database and assign database owner"

arguments {
arg "name" {
description = "Database name"
}

arg "user" {
description = "Database user"
}
}

sql = "CREATE DATABASE {{`{{ .Args.name | quote_ident }}`}} OWNER {{`{{ .Args.user | quote_ident }}`}}"
}

action "user" "create" {
description = "Create a database user with password"

arguments {
arg "user" {
description = "Database user"
}

arg "password" {
description = "Database password"
}
}

sql = "CREATE USER {{`{{ .Args.user | quote_ident }}`}} WITH PASSWORD {{`{{ .Args.password | quote }}`}}"
}
2 changes: 2 additions & 0 deletions test/fixtures/args/migrations.krab.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ migration_set "animals" {
}

action "view" "refresh" {
description = "Refresh a materialized view"

arguments {
arg "name" {
description = "Materialized view to be refreshed"
Expand Down
16 changes: 1 addition & 15 deletions views/action_list.templ
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "fmt"

templ ActionList(actions []*dto.ActionListItem) {
<h1 class="mb-4 text-xl font-semibold text-gray-700">Actions</h1>
<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4">
for _, action := range actions {
@ActionListItem(action)
}
Expand All @@ -25,20 +25,6 @@ templ ActionListItem(action *dto.ActionListItem) {
</h3>
<p class="text-sm text-muted-foreground">{ action.Description }</p>
</div>
<div class="px-6 py-2">
<div class="flex flex-col space-y-2">
for _, arg := range action.Arguments {
<div class="flex items-center space-x-2">
<span class="px-2 py-1 text-sm text-black bg-white rounded">
{ arg.Name } : { arg.Type }
</span>
<p class="text-sm text-gray-500">
{ arg.Description }
</p>
</div>
}
</div>
</div>
<div class="flex items-center p-6">
<a href={ templ.SafeURL(fmt.Sprintf("/ui/actions/new/%s/%s", action.Namespace, action.Name)) }
class="inline-flex items-center justify-center text-sm font-medium px-3 py-1 rounded-sm bg-rose-500 text-rose-100">
Expand Down
6 changes: 4 additions & 2 deletions views/action_new.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import "github.com/ohkrab/krab/web/dto"
import "fmt"

templ ActionForm(form *dto.ActionForm) {
<h1 class="mb-4 text-xl font-semibold text-gray-700">Run action</h1>
<h1 class="mb-2 text-xl font-semibold text-gray-700">Run { form.Namespace }/{ form.Name } action</h1>
<form action="/api/actions/execute" method="POST">
<div class="text-sm font-medium text-gray-500">{ form.Namespace } / { form.Name }</div>
<div class="text-sm font-medium text-gray-500">
{ form.Description }
</div>
<input type="hidden" name="id" value={ form.ExecutionID } />
<input type="hidden" name="namespace" value={ form.Namespace } />
<input type="hidden" name="name" value={ form.Name } />
Expand Down
1 change: 1 addition & 0 deletions web/dto/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ActionListItemArgument struct {
}

type ActionForm struct {
Description string
ExecutionID string
Namespace string
Name string
Expand Down
3 changes: 2 additions & 1 deletion web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (s *Server) Run(args []string) int {
data = append(data, &dto.ActionListItem{
Namespace: action.Namespace,
Name: action.RefName,
Description: "",
Description: action.Description,
Transaction: action.Transaction,
Arguments: args,
})
Expand All @@ -280,6 +280,7 @@ func (s *Server) Run(args []string) int {
}
data := dto.ActionForm{
ExecutionID: uuid.New().String(),
Description: action.Description,
Namespace: action.Namespace,
Name: action.RefName,
Arguments: args,
Expand Down

0 comments on commit d853ca1

Please sign in to comment.