diff --git a/go-statfs/Dockerfile b/go-statfs/Dockerfile new file mode 100644 index 0000000..26ad174 --- /dev/null +++ b/go-statfs/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:1.23-alpine3.20 +WORKDIR /app +# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change +COPY go.mod test.txt ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o /app/go-statfs ./... + +CMD ["/app/go-statfs"] diff --git a/go-statfs/go.mod b/go-statfs/go.mod new file mode 100644 index 0000000..93c750b --- /dev/null +++ b/go-statfs/go.mod @@ -0,0 +1,3 @@ +module go-statfs + +go 1.23.5 diff --git a/go-statfs/main.go b/go-statfs/main.go new file mode 100644 index 0000000..aa45448 --- /dev/null +++ b/go-statfs/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding/json" + "fmt" + _ "net" // for dynamic linking + "os" + "os/signal" + "syscall" +) + +func main() { + rootPath := "/" + var statfs syscall.Statfs_t + err := syscall.Statfs(rootPath, &statfs) + if err != nil { + fmt.Println("Error:", err) + return + } + + // Convert struct to a JSON-friendly format + data := map[string]interface{}{ + "bavail": statfs.Bavail, + "bfree": statfs.Bfree, + "blocks": statfs.Blocks, + "bsize": statfs.Bsize, + "ffree": statfs.Ffree, + "files": statfs.Files, + "flags": statfs.Flags, + "frsize": statfs.Frsize, + "fsid": []int32{int32(statfs.Fsid.X__val[0]), int32(statfs.Fsid.X__val[1])}, + "namelen": statfs.Namelen, + "spare": statfs.Spare, + "type": statfs.Type, + } + + // Convert to JSON + jsonData, err := json.MarshalIndent(data, "", " ") + if err != nil { + fmt.Println("JSON Encoding Error:", err) + return + } + + // Print JSON + fmt.Println(string(jsonData)) + + // Create a channel to receive OS signals + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + // Block forever waiting for a signal + <-sigs + +} diff --git a/go-statfs/test.txt b/go-statfs/test.txt new file mode 100644 index 0000000..1b37687 --- /dev/null +++ b/go-statfs/test.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.