Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor cli #258

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

107 changes: 107 additions & 0 deletions cmd/action_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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{}

func newActionCommand() *cobra.Command {
cmdOpts := &actionOptions{}

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

Check warning on line 28 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L19-L28

Added lines #L19 - L28 were not covered by tests
}
}

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

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)
}

Check warning on line 45 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L33-L45

Added lines #L33 - L45 were not covered by tests

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

Check warning on line 54 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L47-L54

Added lines #L47 - L54 were not covered by tests

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,
)

Check warning on line 80 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L56-L80

Added lines #L56 - L80 were not covered by tests
}

return nil

Check warning on line 83 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L83

Added line #L83 was not covered by tests
}

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
Loading