github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/namespace_update.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/clientutil"
    22  	"github.com/containerd/nerdctl/pkg/cmd/namespace"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newNamespacelabelUpdateCommand() *cobra.Command {
    27  	namespaceLableCommand := &cobra.Command{
    28  		Use:           "update [flags] NAMESPACE",
    29  		Short:         "Update labels for a namespace",
    30  		RunE:          labelUpdateAction,
    31  		Args:          cobra.MinimumNArgs(1),
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  	}
    35  	namespaceLableCommand.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace")
    36  	return namespaceLableCommand
    37  }
    38  
    39  func processNamespaceUpdateCommandOption(cmd *cobra.Command) (types.NamespaceUpdateOptions, error) {
    40  	globalOptions, err := processRootCmdFlags(cmd)
    41  	if err != nil {
    42  		return types.NamespaceUpdateOptions{}, err
    43  	}
    44  	labels, err := cmd.Flags().GetStringArray("label")
    45  	if err != nil {
    46  		return types.NamespaceUpdateOptions{}, err
    47  	}
    48  	return types.NamespaceUpdateOptions{
    49  		GOptions: globalOptions,
    50  		Labels:   labels,
    51  	}, nil
    52  }
    53  
    54  func labelUpdateAction(cmd *cobra.Command, args []string) error {
    55  	options, err := processNamespaceUpdateCommandOption(cmd)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	defer cancel()
    65  
    66  	return namespace.Update(ctx, client, args[0], options)
    67  }