diff --git a/cmd/crane/cmd/serve.go b/cmd/crane/cmd/serve.go index 5256c3714..c375d648e 100644 --- a/cmd/crane/cmd/serve.go +++ b/cmd/crane/cmd/serve.go @@ -37,15 +37,16 @@ func NewCmdRegistry() *cobra.Command { } func newCmdServe() *cobra.Command { - var disk bool + var address, disk string + var blobsToDisk bool 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() @@ -54,7 +55,12 @@ Contents are only stored in memory, and when the process exits, pushed data is l 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) } @@ -62,10 +68,21 @@ Contents are only stored in memory, and when the process exits, pushed data is l 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{ @@ -89,7 +106,12 @@ Contents are only stored in memory, and when the process exits, pushed data is l 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(&blobsToDisk, "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 } diff --git a/cmd/crane/doc/crane_registry.md b/cmd/crane/doc/crane_registry.md index d2bf920d8..a75321ee7 100644 --- a/cmd/crane/doc/crane_registry.md +++ b/cmd/crane/doc/crane_registry.md @@ -20,5 +20,5 @@ ### SEE ALSO * [crane](crane.md) - Crane is a tool for managing container images -* [crane registry serve](crane_registry_serve.md) - Serve an in-memory registry implementation +* [crane registry serve](crane_registry_serve.md) - Serve a registry implementation diff --git a/cmd/crane/doc/crane_registry_serve.md b/cmd/crane/doc/crane_registry_serve.md index f405688e0..a411e5c22 100644 --- a/cmd/crane/doc/crane_registry_serve.md +++ b/cmd/crane/doc/crane_registry_serve.md @@ -1,14 +1,14 @@ ## crane registry serve -Serve an in-memory registry implementation +Serve a registry implementation ### Synopsis -This sub-command serves an in-memory registry implementation on an automatically chosen port (or $PORT) +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). ``` crane registry serve [flags] @@ -17,7 +17,9 @@ crane registry serve [flags] ### Options ``` - -h, --help help for serve + --address string Address to listen on + --disk string Path to a directory where blobs will be stored + -h, --help help for serve ``` ### Options inherited from parent commands