Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24929060
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/cmd/whatcontainer/whatcontainer.go b/cmd/whatcontainer/whatcontainer.go
index b86fc59..68a4e58 100644
--- a/cmd/whatcontainer/whatcontainer.go
+++ b/cmd/whatcontainer/whatcontainer.go
@@ -1,112 +1,139 @@
package main
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"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"log"
"os"
"strconv"
"strings"
)
const DockerApiVersion = "1.37"
type Config struct {
+ Prepend bool
+ Width int
WithPosition bool
Position int
}
func main() {
config := parseArguments()
scanner := bufio.NewScanner(os.Stdin)
processesMap := getProcessesMap()
for scanner.Scan() {
line := addContainerName(scanner.Text(), processesMap, config)
fmt.Println(line)
}
}
func parseArguments() Config {
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()
if *positionPtr > 0 {
config.Position = *positionPtr
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 {
if !isValidFieldPosition(i + 1, config) {
continue
}
pidCandidate, err := strconv.ParseInt(field, 10, 64)
if err != nil {
continue
}
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 {
if config.WithPosition {
return position == config.Position
} else {
// 1 for top and ps -ef
// 2 for ps auxw
return position < 3
}
}
func getProcessesMap () map[int64]string {
dockerClient, err := client.NewClientWithOpts(client.WithVersion(DockerApiVersion))
if err != nil {
log.Println("Can't connect to Docker engine.")
os.Exit(1)
}
containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
processesMap := make(map[int64]string)
psArgs := []string{"auxw"}
for _, container := range containers {
containerName := dockerutils.GetContainerName(container)
response, err := dockerClient.ContainerTop(context.Background(), container.ID, psArgs)
if err != nil {
continue
}
for _, containerProcess := range response.Processes {
processInfo := process.Parse(response.Titles, containerProcess)
processesMap[processInfo.Pid] = containerName
}
}
return processesMap
}
diff --git a/internal/consoleutilities/consoleutilities.go b/internal/consoleutilities/consoleutilities.go
new file mode 100644
index 0000000..7ee9cc0
--- /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
index 88f561b..114ac3a 100644
--- a/internal/stringutilities/stringutilities.go
+++ b/internal/stringutilities/stringutilities.go
@@ -1,27 +1,49 @@
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 {
return true
}
}
return false
}
func GetLongestWord(haystack []string) string {
maxLen := 0
longestWord := ""
for _, word := range haystack {
wordLen := len(word)
if wordLen > maxLen {
longestWord = word
maxLen = wordLen
}
}
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
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Mar 21, 07:17 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546628
Default Alt Text
(5 KB)
Attached To
Mode
rDP Docker Processes
Attached
Detach File
Event Timeline
Log In to Comment