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

fix: unmarshalling info and config #17

Merged
merged 1 commit into from
Sep 6, 2023
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
36 changes: 24 additions & 12 deletions compose/compose.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compose

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -219,16 +220,20 @@ func (c *Compose) config(ctx context.Context, id ...string) ([]containerConfig,
return nil, fmt.Errorf("docker inspect: output: %s, err: %w", string(o), err)
}

lines := strings.Split(strings.TrimSpace(string(o)), "\n")
var configs []containerConfig

configs := make([]containerConfig, len(lines))
for i, l := range lines {
err = json.Unmarshal([]byte(l), &configs[i])
if err != nil {
return nil, fmt.Errorf("docker inspect: unmarshal: config: %s, err: %w", l, err)
}
if err = json.Unmarshal(o, &configs); err == nil {
return configs, nil
}

scanner := bufio.NewScanner(strings.NewReader(string(o)))
for scanner.Scan() {
var config containerConfig
if err = json.Unmarshal(scanner.Bytes(), &config); err != nil {
return nil, fmt.Errorf("docker inspect: unmarshal: config: %s, err: %w", scanner.Text(), err)
}
configs = append(configs, config)
}
return configs, nil
}

Expand All @@ -245,11 +250,18 @@ func (c *Compose) ps(ctx context.Context) ([]containerInfo, error) {
return nil, fmt.Errorf("docker compose ps: output: %s, err: %w", string(o), err)
}

var info []containerInfo
err = json.Unmarshal(o, &info)
if err != nil {
return nil, fmt.Errorf("docker compose ps: unmarshal: output: %s, err: %w", string(o), err)
var infos []containerInfo
if err = json.Unmarshal(o, &infos); err == nil {
return infos, nil
}

return info, nil
scanner := bufio.NewScanner(strings.NewReader(string(o)))
for scanner.Scan() {
var info containerInfo
if err = json.Unmarshal(scanner.Bytes(), &info); err != nil {
return nil, fmt.Errorf("docker compose ps: unmarshal: info: %s, err: %w", scanner.Text(), err)
}
infos = append(infos, info)
}
return infos, nil
}
2 changes: 1 addition & 1 deletion compose/testdata/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "2.1"
version: "3.9"

services:
postgresDB:
Expand Down
Loading