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

cleanup pkg/util packages #5480

Merged
merged 2 commits into from
May 9, 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
2 changes: 1 addition & 1 deletion pkg/builder/jib.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (t *jibTask) Do(ctx context.Context) v1.BuildStatus {
cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_CONFIG_HOME=%s/jib", mavenDir))
cmd.Dir = mavenDir

myerror := util.RunAndLog(ctx, cmd, maven.MavenLogHandler, maven.MavenLogHandler)
myerror := util.RunAndLog(ctx, cmd, maven.LogHandler, maven.LogHandler)

if myerror != nil {
log.Errorf(myerror, "jib integration image containerization did not run successfully")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz", cmd)
tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz", cmd.OutOrStdout())
return nil
})
} else {
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/maven/maven_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Command) Do(ctx context.Context) error {
return err
}

return util.RunAndLog(ctx, cmd, MavenLogHandler, MavenLogHandler)
return util.RunAndLog(ctx, cmd, LogHandler, LogHandler)
}

func NewContext(buildDir string) Context {
Expand Down Expand Up @@ -186,7 +186,7 @@ func (c *Context) AddSystemProperty(name string, value string) {
}

func generateProjectStructure(context Context, project Project) error {
if err := util.WriteFileWithBytesMarshallerContent(context.Path, "pom.xml", project); err != nil {
if err := util.WriteFileWithBytesMarshallerContent(context.Path, "pom.xml", &project); err != nil {
return err
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func generateProjectStructure(context Context, project Project) error {
func (c *Command) prepareMavenWrapper(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "cp", "--recursive", "/usr/share/maven/mvnw/.", ".")
cmd.Dir = c.context.Path
return util.RunAndLog(ctx, cmd, MavenLogHandler, MavenLogHandler)
return util.RunAndLog(ctx, cmd, LogHandler, LogHandler)
}

// ParseGAV decodes the provided Maven GAV into the corresponding Dependency.
Expand Down
16 changes: 10 additions & 6 deletions pkg/util/maven/maven_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import (
"github.com/apache/camel-k/v2/pkg/util/log"
)

// nolint: stylecheck
type mavenLog struct {
Level string `json:"level"`
Ts string `json:"ts"`
TS string `json:"ts"`
Logger string `json:"logger"`
Msg string `json:"msg"`
Class string `json:"class"`
Expand All @@ -48,10 +47,10 @@ const (

var mavenLogger = log.WithName("maven.build")

func MavenLogHandler(s string) string {
mavenLog, parseError := parseLog(s)
func LogHandler(s string) string {
l, parseError := parseLog(s)
if parseError == nil {
normalizeLog(mavenLog)
normalizeLog(l)
} else {
// Why we are ignoring the parsing errors here: there are a few scenarios where this would likely occur.
// For example, if something outside of Maven outputs something (i.e.: the JDK, a misbehaved plugin,
Expand All @@ -69,8 +68,13 @@ func MavenLogHandler(s string) string {

func parseLog(line string) (mavenLog, error) {
var l mavenLog

err := json.Unmarshal([]byte(line), &l)
return l, err
if err != nil {
return l, err
}

return l, nil
}

func normalizeLog(mavenLog mavenLog) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/maven/maven_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestRunAndLogErrorMvn(t *testing.T) {
}

cmd := exec.CommandContext(context.Background(), mavenCmd, "package", "-B")
err := util.RunAndLog(context.Background(), cmd, MavenLogHandler, MavenLogHandler)
err := util.RunAndLog(context.Background(), cmd, LogHandler, LogHandler)

require.Error(t, err)
require.ErrorContains(t, err, "[ERROR] The goal you specified requires a project to execute but there is no POM in this directory")
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func NewProjectWithGAV(group string, artifact string, version string) Project {
return p
}

func (p Project) Command(context Context) *Command {
func (p *Project) Command(context Context) *Command {
return &Command{
context: context,
project: p,
project: *p,
}
}

func (p Project) MarshalBytes() ([]byte, error) {
func (p *Project) MarshalBytes() ([]byte, error) {
w := &bytes.Buffer{}
w.WriteString(xml.Header)

Expand Down
7 changes: 3 additions & 4 deletions pkg/util/maven/maven_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var DefaultRepositories = defaultRepositories{}

type defaultRepositories struct{}

// nolint: unparam
func (o defaultRepositories) apply(settings *Settings) error {
for _, repository := range defaultMavenRepositories() {
upsertRepository(repository, &settings.Profiles[0].Repositories)
Expand All @@ -38,9 +37,9 @@ func (o defaultRepositories) apply(settings *Settings) error {
}

func defaultMavenRepositories() []v1.Repository {
defaultRepositories := strings.Split(DefaultMavenRepositories, ",")
repositories := make([]v1.Repository, 0, len(defaultRepositories))
for _, repository := range defaultRepositories {
repos := strings.Split(DefaultMavenRepositories, ",")
repositories := make([]v1.Repository, 0, len(repos))
for _, repository := range repos {
repositories = append(repositories, NewRepository(repository))
}
return repositories
Expand Down
24 changes: 15 additions & 9 deletions pkg/util/tar/util_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ import (
"io"
"os"

"github.com/spf13/cobra"
"github.com/apache/camel-k/v2/pkg/util"
)

func CreateTarFile(fileNames []string, archiveName string, cmd *cobra.Command) {
func CreateTarFile(fileNames []string, archiveName string, buf io.Writer) {
out, err := os.Create(archiveName)
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "Error writing archive:", err.Error())
_, _ = fmt.Fprintln(buf, "Error writing archive:", err.Error())
}
defer out.Close()
defer util.CloseQuietly(out)

err = createArchiveFile(fileNames, out)
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "Error writing archive:", err.Error())
_, _ = fmt.Fprintln(buf, "Error writing archive:", err.Error())
}
}

func createArchiveFile(files []string, buf io.Writer) error {
gw := gzip.NewWriter(buf)
defer gw.Close()
defer util.CloseQuietly(gw)
tw := tar.NewWriter(gw)
defer tw.Close()
defer util.CloseQuietly(tw)

// Iterate over files and add them to the tar archive
for _, file := range files {
Expand All @@ -61,7 +61,8 @@ func addEntryToArchive(tw *tar.Writer, filename string) error {
if err != nil {
return err
}
defer file.Close()
defer util.CloseQuietly(file)

info, err := file.Stat()
if err != nil {
return err
Expand All @@ -79,6 +80,11 @@ func addEntryToArchive(tw *tar.Writer, filename string) error {
if err != nil {
return err
}
defer os.Remove(filename)

err = os.Remove(filename)
if err != nil {
return err
}

return nil
}
Loading