add

package
v1.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AddCmd = &cobra.Command{
		Use:           "add",
		Short:         "adds the tagging configuration for the target bucket",
		SilenceUsage:  false,
		SilenceErrors: true,
		Example: `# add comma separated tagging configuration into bucket
s3-manager tags add foo1=bar1,foo2=bar2
		`,
		PreRunE: func(cmd *cobra.Command, args []string) (err error) {
			var rootOpts *rootopts.RootOptions
			svc, rootOpts, logger, confirmRunner = utils.PrepareConstants(cmd)
			tagOpts.RootOptions = rootOpts

			if err := utils.CheckArgs(args, 1); err != nil {
				logger.Error().Msg(err.Error())
				return err
			}

			tagOpts.ActualTags = make(map[string]string)
			tagOpts.TagsToAdd = make(map[string]string)

			tags, err := aws.GetBucketTags(svc, tagOpts)
			if err != nil {
				logger.Error().
					Str("error", err.Error()).
					Msg("an error occurred while fetching current tags")
				return err
			}

			logger.Info().Msg("fetched current bucket tags successfully")

			for _, v := range tags.TagSet {
				tagOpts.TagsToAdd[*v.Key] = *v.Value
			}

			tagArr := strings.Split(args[0], ",")
			for _, v := range tagArr {
				tag := strings.Split(v, "=")
				if len(tag) != 2 {
					err = errors.New("each key value pair for a tag should be separated with '='")
					logger.Error().
						Msg(err.Error())
					return err
				}

				tagOpts.TagsToAdd[tag[0]] = tag[1]
			}

			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			logger.Info().Msg("will try to set tags as below")
			for i, v := range tagOpts.TagsToAdd {
				fmt.Printf("%s=%s\n", i, v)
			}

			if tagOpts.DryRun {
				logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
				return nil
			}

			if !tagOpts.AutoApprove {
				var res string
				if res, err = confirmRunner.Run(); err != nil {
					if strings.ToLower(res) == "n" {
						return constants.ErrUserTerminated
					}

					return constants.ErrInvalidInput
				}
			}

			if _, err := aws.SetBucketTags(svc, tagOpts); err != nil {
				logger.Error().
					Str("error", err.Error()).
					Msg("an error occurred while setting tags")
				return err
			}

			logger.Info().Msg("set bucket tags successfully")

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL