github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/ipfs_registry_serve.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/api/types"
    21  	"github.com/containerd/nerdctl/pkg/cmd/ipfs"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  const (
    26  	defaultIPFSRegistry            = "localhost:5050"
    27  	defaultIPFSReadRetryNum        = 0
    28  	defaultIPFSReadTimeoutDuration = 0
    29  )
    30  
    31  func newIPFSRegistryServeCommand() *cobra.Command {
    32  	var ipfsRegistryServeCommand = &cobra.Command{
    33  		Use:           "serve",
    34  		Short:         "serve read-only registry backed by IPFS on localhost.",
    35  		RunE:          ipfsRegistryServeAction,
    36  		SilenceUsage:  true,
    37  		SilenceErrors: true,
    38  	}
    39  
    40  	AddStringFlag(ipfsRegistryServeCommand, "listen-registry", nil, defaultIPFSRegistry, "IPFS_REGISTRY_SERVE_LISTEN_REGISTRY", "address to listen")
    41  	AddStringFlag(ipfsRegistryServeCommand, "ipfs-address", nil, "", "IPFS_REGISTRY_SERVE_IPFS_ADDRESS", "multiaddr of IPFS API (default is pulled from $IPFS_PATH/api file. If $IPFS_PATH env var is not present, it defaults to ~/.ipfs)")
    42  	AddIntFlag(ipfsRegistryServeCommand, "read-retry-num", nil, defaultIPFSReadRetryNum, "IPFS_REGISTRY_SERVE_READ_RETRY_NUM", "times to retry query on IPFS. Zero or lower means no retry.")
    43  	AddDurationFlag(ipfsRegistryServeCommand, "read-timeout", nil, defaultIPFSReadTimeoutDuration, "IPFS_REGISTRY_SERVE_READ_TIMEOUT", "timeout duration of a read request to IPFS. Zero means no timeout.")
    44  
    45  	return ipfsRegistryServeCommand
    46  }
    47  
    48  func processIPFSRegistryServeOptions(cmd *cobra.Command) (opts types.IPFSRegistryServeOptions, err error) {
    49  	ipfsAddressStr, err := cmd.Flags().GetString("ipfs-address")
    50  	if err != nil {
    51  		return types.IPFSRegistryServeOptions{}, err
    52  	}
    53  	listenAddress, err := cmd.Flags().GetString("listen-registry")
    54  	if err != nil {
    55  		return types.IPFSRegistryServeOptions{}, err
    56  	}
    57  	readTimeout, err := cmd.Flags().GetDuration("read-timeout")
    58  	if err != nil {
    59  		return types.IPFSRegistryServeOptions{}, err
    60  	}
    61  	readRetryNum, err := cmd.Flags().GetInt("read-retry-num")
    62  	if err != nil {
    63  		return types.IPFSRegistryServeOptions{}, err
    64  	}
    65  	return types.IPFSRegistryServeOptions{
    66  		ListenRegistry: listenAddress,
    67  		IPFSAddress:    ipfsAddressStr,
    68  		ReadTimeout:    readTimeout,
    69  		ReadRetryNum:   readRetryNum,
    70  	}, nil
    71  }
    72  
    73  func ipfsRegistryServeAction(cmd *cobra.Command, args []string) error {
    74  	options, err := processIPFSRegistryServeOptions(cmd)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	return ipfs.RegistryServe(options)
    79  }