Skip to content
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 .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v9
with:
version: latest
4 changes: 2 additions & 2 deletions cli/container/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (c *InstallCommand) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
defer file.Close()
defer func() { _ = file.Close() }()

imageResp, err := cli.Client.ImageLoad(ctx, file, client.ImageLoadWithQuiet(true))
if err != nil {
return err
}
defer imageResp.Body.Close()
defer func() { _ = imageResp.Body.Close() }()
if imageResp.JSON {
b, err := io.ReadAll(imageResp.Body)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {
stdout := cmd.OutOrStdout()
for _, item := range containers {
if item.ServiceType == container.ContainerType {
fmt.Fprintf(stdout, "%s\t%s\n", item.Name, container.NormalizeImageRef(item.Container.Image))
_, _ = fmt.Fprintf(stdout, "%s\t%s\n", item.Name, container.NormalizeImageRef(item.Container.Image))
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/container_group/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (cp *ComposeProject) GetVersion() string {
if err != nil {
return "latest"
}
defer file.Close()
defer func() { _ = file.Close() }()
scanner := bufio.NewScanner(file)
scanner.Scan()
return scanner.Text()
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {

for _, key := range keys {
project := projects[key]
fmt.Fprintf(stdout, "%s\t%s\n", project.Name, project.GetVersion())
_, _ = fmt.Fprintf(stdout, "%s\t%s\n", project.Name, project.GetVersion())
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions cli/container_image/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (c *InstallCommand) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
defer file.Close()
defer func() { _ = file.Close() }()

imageResp, err := cli.Client.ImageLoad(ctx, file, client.ImageLoadWithQuiet(true))
if err != nil {
return err
}
defer imageResp.Body.Close()
defer func() { _ = imageResp.Body.Close() }()
if imageResp.JSON {
b, err := io.ReadAll(imageResp.Body)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/container_image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {
for _, item := range images {
for _, tag := range item.RepoTags {
if name, version, ok := strings.Cut(tag, ":"); ok {
fmt.Fprintf(stdout, "%s\t%s\n", name, version)
_, _ = fmt.Fprintf(stdout, "%s\t%s\n", name, version)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/container_logs/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {
for _, item := range containers {
switch item.ServiceType {
case container.ContainerType:
fmt.Fprintf(stdout, "%s\n", item.Name)
_, _ = fmt.Fprintf(stdout, "%s\n", item.Name)
case container.ContainerGroupType:
// TODO: Should container groups actually reference use <project>@<service>
// so it is easier for users to see the mapping? Or just create a new container-group-logs
// subcommand to handle this independently
fmt.Fprintf(stdout, "%s\n", item.Container.Name)
_, _ = fmt.Fprintf(stdout, "%s\n", item.Container.Name)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cli/initcmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewInitCommand(cliContext cli.Cli) *cobra.Command {
if err != nil {
return nil
}
defer os.RemoveAll(dir)
defer func() { _ = os.RemoveAll(dir) }()

tmpfile := filepath.Join(dir, "config."+command.Format)
slog.Debug("Writing to temp file.", "path", tmpfile)
Expand Down
2 changes: 1 addition & 1 deletion cli/log_plugins/container_group_plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {
stdout := cmd.OutOrStdout()
for _, item := range containers {
if item.ServiceType == container.ContainerGroupType {
fmt.Fprintf(stdout, "%s\n", item.Name)
_, _ = fmt.Fprintf(stdout, "%s\n", item.Name)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cli/log_plugins/container_plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewListCommand(cliContext cli.Cli) *cobra.Command {
stdout := cmd.OutOrStdout()
for _, item := range containers {
if item.ServiceType == container.ContainerType {
fmt.Fprintf(stdout, "%s\n", item.Name)
_, _ = fmt.Fprintf(stdout, "%s\n", item.Name)
}
}
return nil
Expand Down
19 changes: 11 additions & 8 deletions cli/self/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,39 @@ func (c *CheckCommand) RunE(cmd *cobra.Command, args []string) error {

match := UpdateInfo{}
for _, module := range updateList {
if module.Type == SoftwareManagementTypeSelf {
switch module.Type {
case SoftwareManagementTypeSelf:
// self update (to be removed in the future)
for _, item := range module.Modules {
if item.Action == SoftwareManagementActionInstall {
switch item.Action {
case SoftwareManagementActionInstall:
match.ContainerName = item.Name
match.Image = item.Version
includesSelfUpdate = true
} else if item.Action == SoftwareManagementActionRemove {
case SoftwareManagementActionRemove:
return cli.ExitCodeError{
Code: ExitError,
Err: fmt.Errorf("tedge's own container cannot be removed. name=%s, version=%s, containerId=%s", item.Name, item.Version, selfContainerID),
Silent: true,
}
}
}
} else if module.Type == SoftwareManagementTypeContainer {
case SoftwareManagementTypeContainer:
// container update
// Filter non self-updated modules
filteredModules := make([]SoftwareItem, 0)

for _, item := range module.Modules {
if selfContainerName == item.Name {
if item.Action == SoftwareManagementActionRemove {
switch item.Action {
case SoftwareManagementActionRemove:
// Protect against the user deleting the tedge container itself
return cli.ExitCodeError{
Code: ExitError,
Err: fmt.Errorf("tedge's own container cannot be removed. name=%s, version=%s, containerId=%s", item.Name, item.Version, selfContainerID),
Silent: true,
}
} else if item.Action == SoftwareManagementActionInstall {
case SoftwareManagementActionInstall:
// install
match.ContainerName = item.Name
match.Image = item.Version
Expand All @@ -147,7 +150,7 @@ func (c *CheckCommand) RunE(cmd *cobra.Command, args []string) error {
Modules: filteredModules,
})
}
} else {
default:
outputUpdateModules = append(outputUpdateModules, module)
}
}
Expand All @@ -168,7 +171,7 @@ func (c *CheckCommand) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return newUnexpectedError(err)
}
fmt.Fprintf(cmd.OutOrStdout(), ":::begin-tedge:::\n%s\n:::end-tedge:::\n", payload)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), ":::begin-tedge:::\n%s\n:::end-tedge:::\n", payload)

// includes self update
return nil
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/thin-edge/tedge-container-plugin

go 1.24.0

toolchain go1.24.5
go 1.26.0

require (
github.com/codeclysm/extract/v4 v4.0.0
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmdbuilder/cmdbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func TestHelperProcess(t *testing.T) {
}

if strings.Contains(cmd, "docker") && subArgs[0] == "compose" && subArgs[1] == "version" {
fmt.Fprintln(os.Stdout, "Docker Compose version v2.10.0")
_, _ = fmt.Fprintln(os.Stdout, "Docker Compose version v2.10.0")
os.Exit(0)
}
if strings.Contains(cmd, "docker-compose") && subArgs[0] == "version" {
fmt.Fprintln(os.Stdout, "docker-compose version 1.29.2, build abcdef")
_, _ = fmt.Fprintln(os.Stdout, "docker-compose version 1.29.2, build abcdef")
os.Exit(0)
}
if strings.Contains(cmd, "podman-compose") && subArgs[0] == "version" {
Expand All @@ -110,7 +110,7 @@ func TestHelperProcess(t *testing.T) {
if v := os.Getenv("GO_TEST_PODMAN_COMPOSE_VERSION"); v != "" {
versionOutput = fmt.Sprintf("podman-compose version %s\n['podman', '--version', '']\nusing podman version: 4.x.x", v)
}
fmt.Fprintln(os.Stdout, versionOutput)
_, _ = fmt.Fprintln(os.Stdout, versionOutput)
os.Exit(0)
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/container/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ services:
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(workingDir)
defer func() { _ = os.RemoveAll(workingDir) }()

composeFile := filepath.Join(workingDir, "docker-compose.yml")
if err := os.WriteFile(composeFile, []byte(contents), 0o644); err != nil {
Expand Down Expand Up @@ -202,11 +202,11 @@ func TestHelperProcess(t *testing.T) {
}

if strings.Contains(cmd, "docker") && subArgs[0] == "compose" && subArgs[1] == "version" {
fmt.Fprintln(os.Stdout, "Docker Compose version v2.10.0")
_, _ = fmt.Fprintln(os.Stdout, "Docker Compose version v2.10.0")
os.Exit(0)
}
if strings.Contains(cmd, "docker-compose") && subArgs[0] == "version" {
fmt.Fprintln(os.Stdout, "docker-compose version 1.29.2, build abcdef")
_, _ = fmt.Fprintln(os.Stdout, "docker-compose version 1.29.2, build abcdef")
os.Exit(0)
}
if strings.Contains(cmd, "podman-compose") && subArgs[0] == "version" {
Expand All @@ -215,7 +215,7 @@ func TestHelperProcess(t *testing.T) {
if v := os.Getenv("GO_TEST_PODMAN_COMPOSE_VERSION"); v != "" {
versionOutput = fmt.Sprintf("podman-compose version %s\n['podman', '--version', '']\nusing podman version: 4.x.x", v)
}
fmt.Fprintln(os.Stdout, versionOutput)
_, _ = fmt.Fprintln(os.Stdout, versionOutput)
os.Exit(0)
}

Expand Down Expand Up @@ -252,8 +252,8 @@ func TestDetectCompose(t *testing.T) {
// For simplicity, the current TestHelperProcess prioritizes docker compose.

t.Run("no compose found", func(t *testing.T) {
os.Setenv("GO_TEST_SIMULATE_NO_COMPOSE_FOUND", "1")
defer os.Unsetenv("GO_TEST_SIMULATE_NO_COMPOSE_FOUND")
_ = os.Setenv("GO_TEST_SIMULATE_NO_COMPOSE_FOUND", "1")
defer func() { _ = os.Unsetenv("GO_TEST_SIMULATE_NO_COMPOSE_FOUND") }()

cmd, err := detectCompose()
assert.Error(t, err)
Expand Down Expand Up @@ -290,8 +290,8 @@ func TestPrepareComposeCommand(t *testing.T) {
// or we'd need to simulate the detection order.

// Simulate podman-compose was detected
os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.0.6") // Version < 1.1.0
defer os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION")
_ = os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.0.6") // Version < 1.1.0
defer func() { _ = os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION") }()

// Temporarily override detectCompose for this specific sub-test
// This is a common pattern if direct exec mocking is too complex for all scenarios
Expand All @@ -309,8 +309,8 @@ func TestPrepareComposeCommand(t *testing.T) {
})

t.Run("podman-compose up - verbose added for version >= 1.1.0", func(t *testing.T) {
os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.1.0")
defer os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION")
_ = os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.1.0")
defer func() { _ = os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION") }()

oldDetect := detectComposeFunc
detectComposeFunc = func() (*cmdbuilder.Command, error) {
Expand All @@ -326,8 +326,8 @@ func TestPrepareComposeCommand(t *testing.T) {
})

t.Run("podman-compose up - verbose NOT added for version < 1.1.0", func(t *testing.T) {
os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.0.0")
defer os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION")
_ = os.Setenv("GO_TEST_PODMAN_COMPOSE_VERSION", "1.0.0")
defer func() { _ = os.Unsetenv("GO_TEST_PODMAN_COMPOSE_VERSION") }()

oldDetect := detectComposeFunc
detectComposeFunc = func() (*cmdbuilder.Command, error) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (t JSONTime) MarshalJSON() ([]byte, error) {
v := fmt.Sprintf("\"%s\"", time.Time(t.Time).Format(time.RFC3339))
return []byte(v), nil
}
v := fmt.Sprintf("%d", t.Time.Unix())
v := fmt.Sprintf("%d", t.Unix())
return []byte(v), nil
}

Expand Down Expand Up @@ -725,7 +725,7 @@ func (c *ContainerClient) ImagePullWithRetries(ctx context.Context, imageRef str
if err != nil {
return nil, err
}
defer out.Close()
defer func() { _ = out.Close() }()
if _, ioErr := io.Copy(os.Stderr, out); ioErr != nil {
slog.Warn("Could not write to stderr.", "err", ioErr)
}
Expand Down Expand Up @@ -859,7 +859,7 @@ func (c *ContainerClient) ComposeUp(ctx context.Context, w io.Writer, projectNam
prog := exec.Command(command, args...)
prog.Dir = workingDir
out, err := prog.CombinedOutput()
fmt.Fprintf(w, "%s", out)
_, _ = fmt.Fprintf(w, "%s", out)

if err != nil {
return err
Expand Down Expand Up @@ -954,7 +954,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
prog := exec.Command(command, args...)
prog.Dir = workingDir
out, err := prog.CombinedOutput()
fmt.Fprintf(w, "%s", out)
_, _ = fmt.Fprintf(w, "%s", out)

if err == nil {
slog.Info("Removing project directory.", "dir", workingDir)
Expand Down Expand Up @@ -1152,7 +1152,7 @@ func (c *ContainerClient) ContainerLogs(ctx context.Context, w io.Writer, contai
if logErr != nil {
return logErr
}
defer reader.Close()
defer func() { _ = reader.Close() }()
_, err := StdCopy(w, w, reader)
if err != nil {
return err
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func (c *ContainerClient) CloneContainer(ctx context.Context, containerID string
if logErr != nil {
slog.Warn("Could not get logs from new container.", "id", nextContainer.ID, "err", logErr)
} else {
defer reader.Close()
defer func() { _ = reader.Close() }()

_, err := StdCopy(os.Stderr, os.Stderr, reader)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/container/socket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (c *SocketClient) PruneImages(body io.Reader) (report image.PruneReport, er
return
}

defer func() { _ = r.Body.Close() }()
b, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func (c *SocketClient) PullImages(ctx context.Context, imageRef string, alwaysPu
return err
}

defer r.Body.Close()
defer func() { _ = r.Body.Close() }()
if _, ioErr := io.Copy(os.Stderr, r.Body); ioErr != nil {
slog.Warn("Could not write to stderr.", "err", ioErr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/container/stats_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, strea
s.SetError(err)
return
}
defer response.Body.Close()
defer func() { _ = response.Body.Close() }()

dec := json.NewDecoder(response.Body)
go func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/container/stdcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type stdWriter struct {
// It makes stdWriter to implement io.Writer.
func (w *stdWriter) Write(p []byte) (n int, err error) {
if w == nil || w.Writer == nil {
return 0, errors.New("Writer not instantiated")
return 0, errors.New("writer not instantiated")
}
if p == nil {
return 0, nil
Expand Down Expand Up @@ -135,7 +135,7 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
// to outstream if Systemerr is the stream
out = nil
default:
return 0, fmt.Errorf("Unrecognized input header: %d", buf[stdWriterFdIndex])
return 0, fmt.Errorf("unrecognized input header: %d", buf[stdWriterFdIndex])
}

// Retrieve the size of the frame
Expand Down
Loading
Loading