Skip to content

Commit

Permalink
feat: allow port and disk path to be overriden (#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn authored Nov 29, 2023
1 parent ceb0580 commit 4fdaa32
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
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 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()
Expand All @@ -54,18 +55,34 @@ 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)
}
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 @@ 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
}
2 changes: 1 addition & 1 deletion cmd/crane/doc/crane_registry.md

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

10 changes: 6 additions & 4 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.

0 comments on commit 4fdaa32

Please sign in to comment.