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

feat: allow port and disk path to be overriden #1848

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 32 additions & 10 deletions cmd/crane/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
}

func newCmdServe() *cobra.Command {
var disk bool
var address, disk string
var blobs_to_disk bool

Check warning on line 41 in cmd/crane/cmd/serve.go

View workflow job for this annotation

GitHub Actions / Lint

var-naming: don't use underscores in Go names; var blobs_to_disk should be blobsToDisk (revive)
cmd := &cobra.Command{
Use: "serve",
Short: "Serve an in-memory registry implementation",
Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (or $PORT)
Short: "Serve a registry implementation",
Long: `This sub-command serves a registry implementation on an automatically chosen port (:0), $PORT or --address

The command blocks while the server accepts pushes and pulls.

Contents are only stored in memory, and when the process exits, pushed data is lost.`,
Contents are can be stored in memory (when the process exits, pushed data is lost.), and disk (--disk).`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
Expand All @@ -54,18 +55,34 @@
if port == "" {
port = "0"
}
listener, err := net.Listen("tcp", ":"+port)
listenOn := ":" + port
if address != "" {
listenOn = address
}

listener, err := net.Listen("tcp", listenOn)
if err != nil {
log.Fatalln(err)
}
porti := listener.Addr().(*net.TCPAddr).Port
port = fmt.Sprintf("%d", porti)

bh := registry.NewInMemoryBlobHandler()
if disk {
tmp := os.TempDir()
log.Printf("storing blobs in %s", tmp)
bh = registry.NewDiskBlobHandler(tmp)

diskp := disk
if cmd.Flags().Changed("blobs-to-disk") {
if disk != "" {
return fmt.Errorf("--disk and --blobs-to-disk can't be used together")
}
diskp, err = os.MkdirTemp(os.TempDir(), "craneregistry*")
if err != nil {
return err
}
}

if diskp != "" {
log.Printf("storing blobs in %s", diskp)
bh = registry.NewDiskBlobHandler(diskp)
}

s := &http.Server{
Expand All @@ -89,7 +106,12 @@
return nil
},
}
cmd.Flags().BoolVar(&disk, "blobs-to-disk", false, "Store blobs on disk")
// TODO: remove --blobs-to-disk in a future release.
cmd.Flags().BoolVarP(&blobs_to_disk, "blobs-to-disk", "", false, "Store blobs on disk on tmpdir")
cmd.Flags().MarkHidden("blobs-to-disk")
cmd.Flags().MarkDeprecated("blobs-to-disk", "and will stop working in a future release. use --disk=$(mktemp -d) instead.")
cmd.Flags().StringVarP(&disk, "disk", "", "", "Path to a directory where blobs will be stored")
cmd.Flags().StringVar(&address, "address", "", "Address to listen on")

return cmd
}
5 changes: 3 additions & 2 deletions cmd/crane/doc/crane_registry_serve.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading