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 all commits
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.

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

Check warning on line 32 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L21-L32

Added lines #L21 - L32 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 := 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)
}

Check warning on line 49 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L37-L49

Added lines #L37 - L49 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 58 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L51-L58

Added lines #L51 - L58 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 84 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L60-L84

Added lines #L60 - L84 were not covered by tests
}

return nil

Check warning on line 87 in cmd/action_cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/action_cmd.go#L87

Added line #L87 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