Page MenuHomeDevCentral

D1837.diff
No OneTemporary

D1837.diff

diff --git a/cmd/whatcontainer/whatcontainer.go b/cmd/whatcontainer/whatcontainer.go
--- a/cmd/whatcontainer/whatcontainer.go
+++ b/cmd/whatcontainer/whatcontainer.go
@@ -3,6 +3,7 @@
import (
"bufio"
"context"
+ "devcentral.nasqueron.org/source/docker-processes/internal/stringutilities"
"devcentral.nasqueron.org/source/docker-processes/pkg/dockerutils"
"devcentral.nasqueron.org/source/docker-processes/pkg/process"
"flag"
@@ -18,6 +19,8 @@
const DockerApiVersion = "1.37"
type Config struct {
+ Prepend bool
+ Width int
WithPosition bool
Position int
}
@@ -37,6 +40,8 @@
config := Config{}
positionPtr := flag.Int("p", -1, "the position of the field with the PID")
+ widthPtr := flag.Int ("w", 20, "the amount of characters to use for container name")
+ appendPtr := flag.Bool("append", false, "append the container name to the line (default behavior is to prepend)")
flag.Parse()
@@ -45,10 +50,19 @@
config.WithPosition = true
}
+ config.Prepend = !*appendPtr
+ config.Width = *widthPtr
+
return config
}
func addContainerName (line string, processesMap map[int64]string, config Config) string {
+ containerName := determineContainerName(line, processesMap, config)
+
+ return formatLine(line, containerName, config)
+}
+
+func determineContainerName (line string, processesMap map[int64]string, config Config) string {
fields := strings.Fields(line)
for i, field := range fields {
@@ -63,11 +77,24 @@
}
if containerName, ok := processesMap[pidCandidate]; ok {
- return fmt.Sprintf("%s %s", line, containerName)
+ return containerName
}
}
- return line
+ return ""
+}
+
+func formatLine(line string, containerName string, config Config) string {
+ if config.Prepend {
+ name := stringutilities.PadField(containerName, config.Width)
+ return fmt.Sprintf("%s %s", name, line)
+ }
+
+ if containerName == "" {
+ return line
+ }
+
+ return fmt.Sprintf("%s %s", line, containerName)
}
func isValidFieldPosition(position int, config Config) bool {
diff --git a/internal/consoleutilities/consoleutilities.go b/internal/consoleutilities/consoleutilities.go
new file mode 100644
--- /dev/null
+++ b/internal/consoleutilities/consoleutilities.go
@@ -0,0 +1,12 @@
+package consoleutilities
+
+import (
+ "os"
+ "strings"
+)
+
+func IsUTF8() bool {
+ return strings.Contains(os.Getenv("LC_ALL"), "UTF-8") ||
+ strings.Contains(os.Getenv("LANG"), "UTF-8") ||
+ strings.Contains(os.Getenv("LC_CTYPE"), "UTF-8")
+}
diff --git a/internal/stringutilities/stringutilities.go b/internal/stringutilities/stringutilities.go
--- a/internal/stringutilities/stringutilities.go
+++ b/internal/stringutilities/stringutilities.go
@@ -1,5 +1,10 @@
package stringutilities
+import (
+ "devcentral.nasqueron.org/source/docker-processes/internal/consoleutilities"
+ "strings"
+)
+
func Contains(haystack []string, needle string) bool {
for _, item := range haystack {
if item == needle {
@@ -25,3 +30,20 @@
return longestWord
}
+func PadField(text string, length int) string {
+ textLen := len(text)
+
+ if textLen < length {
+ return strings.Repeat(" ", length - textLen) + text
+ }
+
+ if textLen > length {
+ if consoleutilities.IsUTF8() {
+ return text[:length-1] + "…"
+ }
+
+ return text[:length-1] + "+"
+ }
+
+ return text
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 10:26 (19 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2312289
Default Alt Text
D1837.diff (3 KB)

Event Timeline