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 2 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
29 changes: 20 additions & 9 deletions cmd/crane/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func newCmdRegistry() *cobra.Command {
}

func newCmdServe() *cobra.Command {
var disk bool
var address, disk string
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)
Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address
thesayyn marked this conversation as resolved.
Show resolved Hide resolved

The command blocks while the server accepts pushes and pulls.

Expand All @@ -54,20 +54,27 @@ 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)
if cmd.Flags().Changed("blobs-to-disk") {
path := os.TempDir()
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
if disk != "" && disk != " " {
path = disk
}
log.Printf("storing blobs in %s", path)
bh = registry.NewDiskBlobHandler(path)
}

s := &http.Server{
ReadHeaderTimeout: 5 * time.Second, // prevent slowloris, quiet linter
Handler: registry.New(registry.WithBlobHandler(bh)),
Expand All @@ -89,7 +96,11 @@ 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")
cmd.Flags().StringVarP(&disk, "blobs-to-disk", "", "", "Store blobs on disk")
// allow --blobs-to-disk to work without a value
cmd.Flags().Lookup("blobs-to-disk").NoOptDefVal = " "
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().MarkHidden("blobs-to-disk")
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