github.com/wtfutil/wtf@v0.43.0/modules/docker/client.go (about)

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/dustin/go-humanize"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func (widget *Widget) getSystemInfo() string {
    15  	info, err := widget.cli.Info(context.Background())
    16  	if err != nil {
    17  		return errors.Wrap(err, "could not get docker system info").Error()
    18  	}
    19  
    20  	diskUsage, err := widget.cli.DiskUsage(context.Background(), types.DiskUsageOptions{})
    21  	if err != nil {
    22  		return errors.Wrap(err, "could not get disk usage").Error()
    23  	}
    24  
    25  	var duContainer int64
    26  	for _, c := range diskUsage.Containers {
    27  		duContainer += c.SizeRw
    28  	}
    29  	var duImg int64
    30  	for _, im := range diskUsage.Images {
    31  		duImg += im.Size
    32  	}
    33  	var duVol int64
    34  	for _, v := range diskUsage.Volumes {
    35  		duVol += v.UsageData.Size
    36  	}
    37  
    38  	sysInfo := []struct {
    39  		name  string
    40  		value string
    41  	}{
    42  		{
    43  			name:  "name:",
    44  			value: fmt.Sprintf("[%s]%s", widget.settings.Colors.RowTheme.EvenForeground, info.Name),
    45  		}, {
    46  			name:  "version:",
    47  			value: fmt.Sprintf("[%s]%s", widget.settings.Colors.RowTheme.EvenForeground, info.ServerVersion),
    48  		}, {
    49  			name:  "root:",
    50  			value: fmt.Sprintf("[%s]%s", widget.settings.Colors.RowTheme.EvenForeground, info.DockerRootDir),
    51  		},
    52  		{
    53  			name: "containers:",
    54  			value: fmt.Sprintf("[lime]%d[white]/[yellow]%d[white]/[red]%d",
    55  				info.ContainersRunning,
    56  				info.ContainersPaused, info.ContainersStopped),
    57  		},
    58  		{
    59  			name:  "images:",
    60  			value: fmt.Sprintf("[%s]%d", widget.settings.Colors.RowTheme.EvenForeground, info.Images),
    61  		},
    62  		{
    63  			name:  "volumes:",
    64  			value: fmt.Sprintf("[%s]%v", widget.settings.Colors.RowTheme.EvenForeground, len(diskUsage.Volumes)),
    65  		},
    66  		{
    67  			name:  "memory limit:",
    68  			value: fmt.Sprintf("[%s]%s", widget.settings.Colors.RowTheme.EvenForeground, humanize.Bytes(uint64(info.MemTotal))),
    69  		},
    70  		{
    71  			name: "disk usage:",
    72  			value: fmt.Sprintf(`
    73      [%s]* containers: [%s]%s
    74      [%s]* images:     [%s]%s
    75      [%s]* volumes:    [%s]%s
    76      [%s]* [::b]total:      [%s]%s[::-]
    77  `,
    78  				widget.settings.labelColor,
    79  				widget.settings.Colors.RowTheme.EvenForeground,
    80  				humanize.Bytes(uint64(duContainer)),
    81  
    82  				widget.settings.labelColor,
    83  				widget.settings.Colors.RowTheme.EvenForeground,
    84  				humanize.Bytes(uint64(duImg)),
    85  
    86  				widget.settings.labelColor,
    87  				widget.settings.Colors.RowTheme.EvenForeground,
    88  				humanize.Bytes(uint64(duVol)),
    89  
    90  				widget.settings.labelColor,
    91  				widget.settings.Colors.RowTheme.EvenForeground,
    92  				humanize.Bytes(uint64(duContainer+duImg+duVol))),
    93  		},
    94  	}
    95  
    96  	padSlice(true, sysInfo, func(i int) string {
    97  		return sysInfo[i].name
    98  	}, func(i int, newVal string) {
    99  		sysInfo[i].name = newVal
   100  	})
   101  
   102  	result := ""
   103  	for _, info := range sysInfo {
   104  		result += fmt.Sprintf("[%s]%s %s\n", widget.settings.labelColor, info.name, info.value)
   105  	}
   106  
   107  	return result
   108  }
   109  
   110  func (widget *Widget) getContainerStates() string {
   111  	cntrs, err := widget.cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
   112  	if err != nil {
   113  		return errors.Wrapf(err, " could not get container list").Error()
   114  	}
   115  
   116  	if len(cntrs) == 0 {
   117  		return " no containers"
   118  	}
   119  
   120  	colorMap := map[string]string{
   121  		"created":    "green",
   122  		"running":    "lime",
   123  		"paused":     "yellow",
   124  		"restarting": "yellow",
   125  		"removing":   "yellow",
   126  		"exited":     "red",
   127  		"dead":       "red",
   128  	}
   129  
   130  	containers := []struct {
   131  		name  string
   132  		state string
   133  	}{}
   134  	for _, c := range cntrs {
   135  		container := struct {
   136  			name  string
   137  			state string
   138  		}{
   139  			name:  c.Names[0],
   140  			state: c.State,
   141  		}
   142  
   143  		container.name = strings.ReplaceAll(container.name, "/", "")
   144  		containers = append(containers, container)
   145  	}
   146  
   147  	sort.Slice(containers, func(i, j int) bool {
   148  		return containers[i].name < containers[j].name
   149  	})
   150  
   151  	padSlice(false, containers, func(i int) string {
   152  		return containers[i].name
   153  	}, func(i int, val string) {
   154  		containers[i].name = val
   155  	})
   156  
   157  	result := ""
   158  	for _, c := range containers {
   159  		result += fmt.Sprintf("[white]%s [%s]%s\n", c.name, colorMap[c.state], c.state)
   160  	}
   161  
   162  	return result
   163  }