Skip to content

Commit 4fdaa32

Browse files
authored
feat: allow port and disk path to be overriden (#1848)
1 parent ceb0580 commit 4fdaa32

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

Diff for: cmd/crane/cmd/serve.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ func NewCmdRegistry() *cobra.Command {
3737
}
3838

3939
func newCmdServe() *cobra.Command {
40-
var disk bool
40+
var address, disk string
41+
var blobsToDisk bool
4142
cmd := &cobra.Command{
4243
Use: "serve",
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+
Short: "Serve a registry implementation",
45+
Long: `This sub-command serves a registry implementation on an automatically chosen port (:0), $PORT or --address
4546
4647
The command blocks while the server accepts pushes and pulls.
4748
48-
Contents are only stored in memory, and when the process exits, pushed data is lost.`,
49+
Contents are can be stored in memory (when the process exits, pushed data is lost.), and disk (--disk).`,
4950
Args: cobra.NoArgs,
5051
RunE: func(cmd *cobra.Command, _ []string) error {
5152
ctx := cmd.Context()
@@ -54,18 +55,34 @@ Contents are only stored in memory, and when the process exits, pushed data is l
5455
if port == "" {
5556
port = "0"
5657
}
57-
listener, err := net.Listen("tcp", ":"+port)
58+
listenOn := ":" + port
59+
if address != "" {
60+
listenOn = address
61+
}
62+
63+
listener, err := net.Listen("tcp", listenOn)
5864
if err != nil {
5965
log.Fatalln(err)
6066
}
6167
porti := listener.Addr().(*net.TCPAddr).Port
6268
port = fmt.Sprintf("%d", porti)
6369

6470
bh := registry.NewInMemoryBlobHandler()
65-
if disk {
66-
tmp := os.TempDir()
67-
log.Printf("storing blobs in %s", tmp)
68-
bh = registry.NewDiskBlobHandler(tmp)
71+
72+
diskp := disk
73+
if cmd.Flags().Changed("blobs-to-disk") {
74+
if disk != "" {
75+
return fmt.Errorf("--disk and --blobs-to-disk can't be used together")
76+
}
77+
diskp, err = os.MkdirTemp(os.TempDir(), "craneregistry*")
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
83+
if diskp != "" {
84+
log.Printf("storing blobs in %s", diskp)
85+
bh = registry.NewDiskBlobHandler(diskp)
6986
}
7087

7188
s := &http.Server{
@@ -89,7 +106,12 @@ Contents are only stored in memory, and when the process exits, pushed data is l
89106
return nil
90107
},
91108
}
92-
cmd.Flags().BoolVar(&disk, "blobs-to-disk", false, "Store blobs on disk")
109+
// TODO: remove --blobs-to-disk in a future release.
110+
cmd.Flags().BoolVarP(&blobsToDisk, "blobs-to-disk", "", false, "Store blobs on disk on tmpdir")
93111
cmd.Flags().MarkHidden("blobs-to-disk")
112+
cmd.Flags().MarkDeprecated("blobs-to-disk", "and will stop working in a future release. use --disk=$(mktemp -d) instead.")
113+
cmd.Flags().StringVarP(&disk, "disk", "", "", "Path to a directory where blobs will be stored")
114+
cmd.Flags().StringVar(&address, "address", "", "Address to listen on")
115+
94116
return cmd
95117
}

Diff for: cmd/crane/doc/crane_registry.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: cmd/crane/doc/crane_registry_serve.md

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)