From ef636c86fc94169cdc4a609dfe05d7e088373667 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Wed, 24 Jan 2024 09:48:29 -0800 Subject: [PATCH] fix: removes deprecated code (#2730) --- .github/workflows/test.yml | 4 ++++ internal/docker/client.go | 14 +++++++------- internal/docker/client_test.go | 8 ++++---- internal/docker/event_generator.go | 2 +- internal/docker/event_generator_test.go | 3 +-- internal/healthcheck/http.go | 2 +- internal/profile/disk.go | 6 +++--- internal/web/logs.go | 13 +++---------- main_test.go | 3 ++- 9 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d9818651..bc186c4e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -60,6 +60,10 @@ jobs: uses: actions/checkout@v4 - name: Run Go Tests with Coverage run: make test SKIP_ASSET=1 + - name: Stactic checker + uses: dominikh/staticcheck-action@v1.3.0 + with: + install-go: false int-test: name: Integration Tests timeout-minutes: 60 diff --git a/internal/docker/client.go b/internal/docker/client.go index 54472898..3b582978 100644 --- a/internal/docker/client.go +++ b/internal/docker/client.go @@ -44,13 +44,13 @@ func (s StdType) String() string { } type DockerCLI interface { - ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error) - ContainerLogs(context.Context, string, types.ContainerLogsOptions) (io.ReadCloser, error) + ContainerList(context.Context, container.ListOptions) ([]types.Container, error) + ContainerLogs(context.Context, string, container.LogsOptions) (io.ReadCloser, error) Events(context.Context, types.EventsOptions) (<-chan events.Message, <-chan error) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) Ping(ctx context.Context) (types.Ping, error) - ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error + ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error ContainerRestart(ctx context.Context, containerID string, options container.StopOptions) error } @@ -152,7 +152,7 @@ func (d *Client) FindContainer(id string) (Container, error) { func (d *Client) ContainerActions(action string, containerID string) error { switch action { case "start": - return d.cli.ContainerStart(context.Background(), containerID, types.ContainerStartOptions{}) + return d.cli.ContainerStart(context.Background(), containerID, container.StartOptions{}) case "stop": return d.cli.ContainerStop(context.Background(), containerID, container.StopOptions{}) case "restart": @@ -163,7 +163,7 @@ func (d *Client) ContainerActions(action string, containerID string) error { } func (d *Client) ListContainers() ([]Container, error) { - containerListOptions := types.ContainerListOptions{ + containerListOptions := container.ListOptions{ Filters: d.filters, All: true, } @@ -274,7 +274,7 @@ func (d *Client) ContainerLogs(ctx context.Context, id string, since string, std } } - options := types.ContainerLogsOptions{ + options := container.LogsOptions{ ShowStdout: stdType&STDOUT != 0, ShowStderr: stdType&STDERR != 0, Follow: true, @@ -320,7 +320,7 @@ func (d *Client) Events(ctx context.Context, messages chan<- ContainerEvent) <-c } func (d *Client) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time, stdType StdType) (io.ReadCloser, error) { - options := types.ContainerLogsOptions{ + options := container.LogsOptions{ ShowStdout: stdType&STDOUT != 0, ShowStderr: stdType&STDERR != 0, Timestamps: true, diff --git a/internal/docker/client_test.go b/internal/docker/client_test.go index 5c32eb48..e2391251 100644 --- a/internal/docker/client_test.go +++ b/internal/docker/client_test.go @@ -22,7 +22,7 @@ type mockedProxy struct { DockerCLI } -func (m *mockedProxy) ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error) { +func (m *mockedProxy) ContainerList(context.Context, container.ListOptions) ([]types.Container, error) { args := m.Called() containers, ok := args.Get(0).([]types.Container) if !ok && args.Get(0) != nil { @@ -32,7 +32,7 @@ func (m *mockedProxy) ContainerList(context.Context, types.ContainerListOptions) } -func (m *mockedProxy) ContainerLogs(ctx context.Context, id string, options types.ContainerLogsOptions) (io.ReadCloser, error) { +func (m *mockedProxy) ContainerLogs(ctx context.Context, id string, options container.LogsOptions) (io.ReadCloser, error) { args := m.Called(ctx, id, options) reader, ok := args.Get(0).(io.ReadCloser) if !ok && args.Get(0) != nil { @@ -50,7 +50,7 @@ func (m *mockedProxy) ContainerStats(ctx context.Context, containerID string, st return types.ContainerStats{}, nil } -func (m *mockedProxy) ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error { +func (m *mockedProxy) ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error { args := m.Called(ctx, containerID, options) err := args.Get(0) @@ -158,7 +158,7 @@ func Test_dockerClient_ContainerLogs_happy(t *testing.T) { b = append(b, []byte(expected)...) reader := io.NopCloser(bytes.NewReader(b)) - options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true, Since: "since"} + options := container.LogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true, Since: "since"} proxy.On("ContainerLogs", mock.Anything, id, options).Return(reader, nil) client := &Client{proxy, filters.NewArgs(), &Host{ID: "localhost"}} diff --git a/internal/docker/event_generator.go b/internal/docker/event_generator.go index e739700c..f67d6d7d 100644 --- a/internal/docker/event_generator.go +++ b/internal/docker/event_generator.go @@ -193,7 +193,7 @@ func createEvent(message string, streamType StdType) *LogEvent { for decoder.ScanKeyval() { key := decoder.Key() value := decoder.Value() - if validLogFmtKey.Match(key) == false { + if !validLogFmtKey.Match(key) { allValid = false break } diff --git a/internal/docker/event_generator_test.go b/internal/docker/event_generator_test.go index b74c19b8..4a5cc1f7 100644 --- a/internal/docker/event_generator_test.go +++ b/internal/docker/event_generator_test.go @@ -81,8 +81,7 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool { func Test_createEvent(t *testing.T) { type args struct { - message string - streamType StdType + message string } tests := []struct { name string diff --git a/internal/healthcheck/http.go b/internal/healthcheck/http.go index 539508c6..c4bc492f 100644 --- a/internal/healthcheck/http.go +++ b/internal/healthcheck/http.go @@ -35,5 +35,5 @@ func HttpRequest(addr string, base string) error { return nil } - return fmt.Errorf("Healthcheck failed with status code %d", resp.StatusCode) + return fmt.Errorf("healthcheck failed with status code %d", resp.StatusCode) } diff --git a/internal/profile/disk.go b/internal/profile/disk.go index df69dedf..cf29fade 100644 --- a/internal/profile/disk.go +++ b/internal/profile/disk.go @@ -17,7 +17,7 @@ const ( profileFilename = "profile.json" ) -var missingProfileErr = errors.New("Profile file does not exist") +var errMissingProfileErr = errors.New("Profile file does not exist") type Settings struct { Search bool `json:"search"` @@ -66,7 +66,7 @@ func UpdateFromReader(user auth.User, reader io.Reader) error { mux.Lock() defer mux.Unlock() existingProfile, err := Load(user) - if err != nil && err != missingProfileErr { + if err != nil && err != errMissingProfileErr { return err } @@ -112,7 +112,7 @@ func Load(user auth.User) (Profile, error) { profilePath := filepath.Join(path, profileFilename) if _, err := os.Stat(profilePath); os.IsNotExist(err) { - return Profile{}, missingProfileErr + return Profile{}, errMissingProfileErr } f, err := os.Open(profilePath) diff --git a/internal/web/logs.go b/internal/web/logs.go index 0ae0df51..31d71dc6 100644 --- a/internal/web/logs.go +++ b/internal/web/logs.go @@ -85,16 +85,9 @@ func (h *handler) fetchLogsBetweenDates(w http.ResponseWriter, r *http.Request) g := docker.NewEventGenerator(reader, container.Tty) encoder := json.NewEncoder(w) -loop: - for { - select { - case event, ok := <-g.Events: - if !ok { - break loop - } - if err := encoder.Encode(event); err != nil { - log.Errorf("json encoding error while streaming %v", err.Error()) - } + for event := range g.Events { + if err := encoder.Encode(event); err != nil { + log.Errorf("json encoding error while streaming %v", err.Error()) } } } diff --git a/main_test.go b/main_test.go index 5e349b91..1056b862 100644 --- a/main_test.go +++ b/main_test.go @@ -7,6 +7,7 @@ import ( "github.com/amir20/dozzle/internal/docker" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -17,7 +18,7 @@ type fakeCLI struct { mock.Mock } -func (f *fakeCLI) ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error) { +func (f *fakeCLI) ContainerList(context.Context, container.ListOptions) ([]types.Container, error) { args := f.Called() return args.Get(0).([]types.Container), args.Error(1) }