Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use log/slog #1680

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions docker-images/mod-downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package main

import (
"context"
"fmt"
"log"
"log/slog"
"os"
"strings"

"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)

func init() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
}

func main() {
downloadTargetDirPath := os.Getenv("DOWNLOAD_TARGET_DIR_PATH")
err := os.MkdirAll(downloadTargetDirPath, 0600)
Expand Down Expand Up @@ -48,25 +52,25 @@ func main() {
})
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
slog.Error("Error:", "error", object.Err)
return
}
// キー名が最初からprefix付きで返ってくるので、ディレクトリ指定の際にはTrimする必要がある
filePathToSave := downloadTargetDirPath + strings.TrimPrefix(object.Key, prefixName)
fmt.Println("Downloading object:", object.Key)
slog.Info("Downloading object:", "objectKey", object.Key)
err = minioClient.FGetObject(context.Background(), bucketName, object.Key, filePathToSave, minio.GetObjectOptions{})
if err != nil {
fmt.Println(err)
slog.Error("Error downloading object:", "objectKey", object.Key, "error", err)
return
}
// 保存したファイルの所有権をitzg/minecraftに渡す ref. https://github.com/itzg/docker-minecraft-server/issues/1583
err := os.Chown(filePathToSave, 1000, 1000)
if err != nil {
fmt.Println(err)
slog.Error("Error changing file ownership:", "filePath", filePathToSave, "error", err)
} else {
fmt.Println("File ownership changed successfully")
slog.Info("File ownership changed successfully", "filePath", filePathToSave)
}
}

fmt.Println("Downloaded all visible objects.")
slog.Info("Downloaded all visible objects.")
}