Skip to content

Commit

Permalink
add cf workflow list and cf workflow delete (#98)
Browse files Browse the repository at this point in the history
* add `cf workflow list` and `cf workflow delete`

* update SDK to latest

* add docs
  • Loading branch information
chrnorm authored Oct 28, 2024
1 parent 1887736 commit 2be98c3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-hornets-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@common-fate/cli": minor
---

Adds 'cf workflow list' and 'cf workflow delete' commands.
14 changes: 14 additions & 0 deletions cmd/cli/command/workflow/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package workflow

import (
"github.com/urfave/cli/v2"
)

var Command = cli.Command{
Name: "workflow",
Usage: "Manage Common Fate Access Workflows",
Subcommands: []*cli.Command{
&listCommand,
&deleteCommand,
},
}
40 changes: 40 additions & 0 deletions cmd/cli/command/workflow/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package workflow

import (
"fmt"

"connectrpc.com/connect"
"github.com/common-fate/sdk/config"
configv1alpha1 "github.com/common-fate/sdk/gen/commonfate/control/config/v1alpha1"
"github.com/common-fate/sdk/service/control/configsvc"
"github.com/urfave/cli/v2"
)

var deleteCommand = cli.Command{
Name: "delete",
Usage: "Delete an Access Workflow",
Flags: []cli.Flag{
&cli.StringFlag{Name: "workflow-id", Required: true, Usage: "the workflow ID to delete"},
},
Action: func(c *cli.Context) error {
ctx := c.Context

cfg, err := config.LoadDefault(ctx)
if err != nil {
return err
}

client := configsvc.NewFromConfig(cfg)

res, err := client.AccessWorkflow().DeleteAccessWorkflow(ctx, connect.NewRequest(&configv1alpha1.DeleteAccessWorkflowRequest{
Id: c.String("workflow-id"),
}))
if err != nil {
return err
}

fmt.Printf("deleted %s\n", res.Msg.Id)

return nil
},
}
67 changes: 67 additions & 0 deletions cmd/cli/command/workflow/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package workflow

import (
"encoding/json"
"fmt"

"connectrpc.com/connect"
"github.com/common-fate/sdk/config"
resourcev1alpha1 "github.com/common-fate/sdk/gen/commonfate/control/resource/v1alpha1"
"github.com/common-fate/sdk/gen/commonfate/control/resource/v1alpha1/resourcev1alpha1connect"
"github.com/urfave/cli/v2"
)

var listCommand = cli.Command{
Name: "list",
Action: func(c *cli.Context) error {
ctx := c.Context

cfg, err := config.LoadDefault(ctx)
if err != nil {
return err
}

client := newResourceClient(cfg)
// the Common Fate API doesn't currently expose a ListAccessWorkflows method, so we use the
// QueryResources API.
res, err := client.QueryResources(ctx, connect.NewRequest(&resourcev1alpha1.QueryResourcesRequest{
Type: "Access::Workflow",
}))
if err != nil {
return err
}

workflows := allWorkflows{Workflows: []workflow{}}

for _, w := range res.Msg.Resources {
workflows.Workflows = append(workflows.Workflows, workflow{
ID: w.Eid.Id,
Name: w.Name,
})
}

workflowsJSON, err := json.MarshalIndent(workflows, "", " ")
if err != nil {
return err
}

fmt.Println(string(workflowsJSON))
return nil
},
}

// allWorkflows is used to ensure the output of the `cf workflows list`
// command remains stable, even if the API that we call changes from
// QueryResources to something else in future (such as ListAccessWorkflows).
type allWorkflows struct {
Workflows []workflow `json:"workflows"`
}

type workflow struct {
ID string `json:"id"`
Name string `json:"name"`
}

func newResourceClient(cfg *config.Context) resourcev1alpha1connect.ResourceServiceClient {
return resourcev1alpha1connect.NewResourceServiceClient(cfg.HTTPClient, cfg.APIURL)
}
2 changes: 2 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/common-fate/cli/cmd/cli/command/testcmd"
"github.com/common-fate/cli/cmd/cli/command/workflow"

"go.uber.org/zap"

Expand Down Expand Up @@ -127,6 +128,7 @@ func main() {
&deployment.Command,
&integration.Command,
&testcmd.Command,
&workflow.Command,
},
}
clio.SetLevelFromEnv("CF_LOG")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/common-fate/clio v1.2.3
github.com/common-fate/glide-cli v0.6.0
github.com/common-fate/grab v1.3.0
github.com/common-fate/sdk v1.54.1
github.com/common-fate/sdk v1.66.0
github.com/fatih/color v1.16.0
github.com/goreleaser/fileglob v1.3.0
github.com/mattn/go-isatty v0.0.20
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ github.com/common-fate/iso8601 v1.1.0 h1:nrej9shsK1aB4IyOAjZl68xGk8yDuUxVwQjoDzx
github.com/common-fate/iso8601 v1.1.0/go.mod h1:DU4mvUEkkWZUUSJq2aCuNqM1luSb0Pwyb2dLzXS+img=
github.com/common-fate/provider-registry-sdk-go v0.19.0 h1:V5ZUP5jvNFM0FVXH8akXLb5VxBu22OkKqQTu2CA1goI=
github.com/common-fate/provider-registry-sdk-go v0.19.0/go.mod h1:nD5qKGinFLxxhISpWHYCv5v7bQhB1wKap4jA8bmztOw=
github.com/common-fate/sdk v1.54.1 h1:qwirNpQ5IO3tAvCWRDKCrE7r8azrS3hbFeU+TPcWPFY=
github.com/common-fate/sdk v1.54.1/go.mod h1:OrXhzB2Y1JSrKGHrb4qRmY+6MF2M3MFb+3edBnessXo=
github.com/common-fate/sdk v1.66.0 h1:BaY6LrauxRxoQBAyDJlnBFPFPbkCNH8pY6zIxxmtHXw=
github.com/common-fate/sdk v1.66.0/go.mod h1:OrXhzB2Y1JSrKGHrb4qRmY+6MF2M3MFb+3edBnessXo=
github.com/common-fate/useragent v0.1.0 h1:RLmkIiJXcOUJAUyXWc/zCaGbrGmlCbHBGMx99ztQ3ZU=
github.com/common-fate/useragent v0.1.0/go.mod h1:GjXGR6cDiMboDP04qlfDfA5HTbeoRSoNgQWDAyOdW9o=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down

0 comments on commit 2be98c3

Please sign in to comment.