Closed k8scat closed 3 years ago
I have realized it without the App2
, only need to parse the output of ls -la
.
type File struct {
Name string `json:"name"`
Permission string `json:"permission"`
Owner string `json:"owner"`
Group string `json:"group"`
Size int64 `json:"size"`
Modifytime int64 `json:"modify_time"`
}
func ListContainerFiles(containerID, path string) ([]*File, error) {
out, err := docker.Exec(containerID, "root", []string{"ls", "-la", path})
if err != nil {
return nil, err
}
var files []*File
r := bufio.NewReader(bytes.NewBuffer(out))
for {
line, _, err := r.ReadLine()
if err != nil {
break
}
fields := strings.Fields(string(line))
if len(fields) < 9 {
continue
}
filename := fields[8]
if filename == "." || filename == ".." {
continue
}
filesize, err := strconv.ParseInt(fields[4], 10, 64)
if err != nil {
return nil, err
}
ty := fields[7] // time or year
var t string
var y string
if strings.Contains(ty, ":") {
t = ty
y = time.Now().Format("2006")
} else {
y = ty
t = "00:00"
}
d := fields[6]
if len(d) == 1 {
d = fmt.Sprintf("0%s", d)
}
m := fields[5]
mt, err := time.Parse("2006 Jan 02 15:04", fmt.Sprintf("%s %s %s %s", y, m, d, t))
if err != nil {
return nil, err
}
file := &File{
Name: filename,
Permission: fields[0],
Owner: fields[2],
Group: fields[3],
Size: filesize,
Modifytime: mt.Unix(),
}
files = append(files, file)
}
return files, nil
}
GOOD JOB! But I'm not sure if all mirrors have the ls command, If the command does not exist in container, and this feature will not be available!
I think most of docker images have this command even the minimal docker image Alpine
, and the App2
maybe not support multi platforms like darwin/amd64 or linux/arm64, so exec ls -la ${path}
maybe a better choice to list file inside the container or the host machine.
I have not used the windows container, and unluckily, it seems that cannot be runned on my MacOS:
$ docker pull mcr.microsoft.com/windows:10.0.17763.1039-amd64
10.0.17763.1039-amd64: Pulling from windows
e7f2973ed2dd: Pulling fs layer
1e896ba34de7: Pulling fs layer
image operating system "windows" cannot be used on this platform
You are right, I will integrate this code into the project, thank you for your work!
You are right, I will integrate this code into the project, thank you for your work!
Maybe I can submit a PR
Sure, if you have enough time!
Sorry, I didn't notice this issue!It is a small tool, similar to command 'ls', through its program can collect the file system system, and then show on web page, I will upload its source code in the last two days!