@@ -37,11 +37,11 @@ func newCmdRegistry() *cobra.Command {
37
37
}
38
38
39
39
func newCmdServe () * cobra.Command {
40
- var disk bool
40
+ var address , disk string
41
41
cmd := & cobra.Command {
42
42
Use : "serve" ,
43
43
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
45
45
46
46
The command blocks while the server accepts pushes and pulls.
47
47
@@ -54,20 +54,27 @@ Contents are only stored in memory, and when the process exits, pushed data is l
54
54
if port == "" {
55
55
port = "0"
56
56
}
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 )
58
63
if err != nil {
59
64
log .Fatalln (err )
60
65
}
61
66
porti := listener .Addr ().(* net.TCPAddr ).Port
62
67
port = fmt .Sprintf ("%d" , porti )
63
68
64
69
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 )
69
77
}
70
-
71
78
s := & http.Server {
72
79
ReadHeaderTimeout : 5 * time .Second , // prevent slowloris, quiet linter
73
80
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
89
96
return nil
90
97
},
91
98
}
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 = " "
93
102
cmd .Flags ().MarkHidden ("blobs-to-disk" )
103
+ cmd .Flags ().StringVar (& address , "address" , "" , "Address to listen on" )
104
+
94
105
return cmd
95
106
}
0 commit comments