Skip to content

Commit 1c9620b

Browse files
committed
feat: allow port and disk path to be overriden
1 parent 5a53a12 commit 1c9620b

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

cmd/crane/cmd/serve.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func newCmdRegistry() *cobra.Command {
3737
}
3838

3939
func newCmdServe() *cobra.Command {
40-
var disk bool
40+
var address, disk string
4141
cmd := &cobra.Command{
4242
Use: "serve",
4343
Short: "Serve an in-memory registry implementation",
44-
Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (or $PORT)
44+
Long: `This sub-command serves an in-memory registry implementation on an automatically chosen port (:0), $PORT or --address
4545
4646
The command blocks while the server accepts pushes and pulls.
4747
@@ -54,20 +54,27 @@ Contents are only stored in memory, and when the process exits, pushed data is l
5454
if port == "" {
5555
port = "0"
5656
}
57-
listener, err := net.Listen("tcp", ":"+port)
57+
listenOn := ":" + port
58+
if address != "" {
59+
listenOn = address
60+
}
61+
62+
listener, err := net.Listen("tcp", listenOn)
5863
if err != nil {
5964
log.Fatalln(err)
6065
}
6166
porti := listener.Addr().(*net.TCPAddr).Port
6267
port = fmt.Sprintf("%d", porti)
6368

6469
bh := registry.NewInMemoryBlobHandler()
65-
if disk {
66-
tmp := os.TempDir()
67-
log.Printf("storing blobs in %s", tmp)
68-
bh = registry.NewDiskBlobHandler(tmp)
70+
if cmd.Flags().Changed("blobs-to-disk") {
71+
path := os.TempDir()
72+
if disk != "" && disk != " " {
73+
path = disk
74+
}
75+
log.Printf("storing blobs in %s", path)
76+
bh = registry.NewDiskBlobHandler(path)
6977
}
70-
7178
s := &http.Server{
7279
ReadHeaderTimeout: 5 * time.Second, // prevent slowloris, quiet linter
7380
Handler: registry.New(registry.WithBlobHandler(bh)),
@@ -89,7 +96,11 @@ Contents are only stored in memory, and when the process exits, pushed data is l
8996
return nil
9097
},
9198
}
92-
cmd.Flags().BoolVar(&disk, "blobs-to-disk", false, "Store blobs on disk")
99+
cmd.Flags().StringVarP(&disk, "blobs-to-disk", "", "", "Store blobs on disk")
100+
// allow --blobs-to-disk to work without a value
101+
cmd.Flags().Lookup("blobs-to-disk").NoOptDefVal = " "
93102
cmd.Flags().MarkHidden("blobs-to-disk")
103+
cmd.Flags().StringVar(&address, "address", "", "Address to listen on")
104+
94105
return cmd
95106
}

0 commit comments

Comments
 (0)