Skip to content

Commit

Permalink
refactor cli (akuity#258)
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour authored Mar 21, 2024
1 parent bd33988 commit 4812fe0
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 463 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
-ldflags "-w -X ${VERSION_PACKAGE}.version=${VERSION} -X ${VERSION_PACKAGE}.buildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ') -X ${VERSION_PACKAGE}.gitCommit=${GIT_COMMIT} -X ${VERSION_PACKAGE}.gitTreeState=${GIT_TREE_STATE}" \
-o bin/kargo-render \
./cmd \
&& bin/kargo-render version \
&& cd bin \
&& ln -s kargo-render kargo-render-action
&& bin/kargo-render version

FROM alpine:3.19.1 as final

Expand Down
68 changes: 0 additions & 68 deletions cmd/action/action.go

This file was deleted.

5 changes: 0 additions & 5 deletions cmd/action/logger.go

This file was deleted.

31 changes: 0 additions & 31 deletions cmd/action/request.go

This file was deleted.

111 changes: 111 additions & 0 deletions cmd/action_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"context"
"fmt"
"io"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

render "github.com/akuity/kargo-render"
libLog "github.com/akuity/kargo-render/internal/log"
libOS "github.com/akuity/kargo-render/internal/os"
"github.com/akuity/kargo-render/internal/version"
)

type actionOptions struct {
logger *log.Logger
}

func newActionCommand() *cobra.Command {
cmdOpts := &actionOptions{
logger: libLog.LoggerOrDie(),
}

return &cobra.Command{
Use: "action",
Hidden: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return cmdOpts.run(cmd.Context(), cmd.OutOrStdout())
},
}
}

// run performs manifest rendering in a GitHub Actions-compatible manner.
func (o *actionOptions) run(_ context.Context, out io.Writer) error {
logger := o.logger

ver := version.GetVersion()
logger.WithFields(log.Fields{
"version": ver.Version,
"commit": ver.GitCommit,
}).Info("Starting Kargo Render Action")

req, err := request()
if err != nil {
logger.Fatal(err)
}

res, err := render.NewService(
&render.ServiceOptions{
LogLevel: render.LogLevel(logger.Level),
},
).RenderManifests(context.Background(), req)
if err != nil {
logger.Fatal(err)
}

switch res.ActionTaken {
case render.ActionTakenNone:
fmt.Fprintln(
out,
"\nThis request would not change any state. No action was taken.",
)
case render.ActionTakenOpenedPR:
fmt.Fprintf(
out,
"\nOpened PR %s\n",
res.PullRequestURL,
)
case render.ActionTakenPushedDirectly:
fmt.Fprintf(
out,
"\nCommitted %s to branch %s\n",
res.CommitID,
req.TargetBranch,
)
case render.ActionTakenUpdatedPR:
fmt.Fprintf(
out,
"\nUpdated an existing PR to %s\n",
req.TargetBranch,
)
}

return nil
}

func request() (render.Request, error) {
req := render.Request{
RepoCreds: render.RepoCredentials{
Username: "git",
},
Images: libOS.GetStringSliceFromEnvVar("INPUT_IMAGES", nil),
}
repo, err := libOS.GetRequiredEnvVar("GITHUB_REPOSITORY")
if err != nil {
return req, err
}
req.RepoURL = fmt.Sprintf("https://github.com/%s", repo)
if req.RepoCreds.Password, err =
libOS.GetRequiredEnvVar("INPUT_PERSONALACCESSTOKEN"); err != nil {
return req, err
}
if req.Ref, err = libOS.GetRequiredEnvVar("GITHUB_SHA"); err != nil {
return req, err
}
req.TargetBranch, err = libOS.GetRequiredEnvVar("INPUT_TARGETBRANCH")
return req, err
}
2 changes: 1 addition & 1 deletion cmd/action/request_test.go → cmd/action_cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package main

import (
"fmt"
Expand Down
33 changes: 0 additions & 33 deletions cmd/cli/cli.go

This file was deleted.

Loading

0 comments on commit 4812fe0

Please sign in to comment.