remove

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RemoveCmd = &cobra.Command{
		Use:           "remove",
		Short:         "removes the tagging configuration for the target bucket",
		SilenceUsage:  true,
		SilenceErrors: true,
		Example: `# remove comma separated tagging configuration from bucket
s3-manager tags remove foo1=bar1,foo2=bar2
		`,
		PreRunE: func(cmd *cobra.Command, args []string) (err error) {
			svc, tagOpts, logger = utils2.PrepareConstants(cmd, options.GetTagOptions())

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

			tagOpts.ActualTags = make(map[string]string)
			tagOpts.TagsToRemove = 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.ActualTags[*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
				}

				if utils.HasKeyValuePair(tagOpts.ActualTags, tag[0], tag[1]) {
					tagOpts.TagsToRemove[tag[0]] = tag[1]
				}
			}

			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			if len(tagOpts.TagsToRemove) == 0 {
				logger.Warn().Msg("specified tags are not deletable, exiting")
				return nil
			}

			logger.Info().Msg("here is the list of current tags")
			for i, v := range tagOpts.ActualTags {
				fmt.Printf(outputStr, i, v)
			}

			logger.Info().Msg("will try to remove below tags")
			for i, v := range tagOpts.TagsToRemove {
				fmt.Printf(outputStr, i, v)
			}

			utils.RemoveMapElements(tagOpts.ActualTags, tagOpts.TagsToRemove)

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

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

			logger.Info().Msg("successfully removed target tags")
			logger.Info().Msg("here is the list of current tags again")
			for i, v := range tagOpts.ActualTags {
				fmt.Printf(outputStr, i, v)
			}

			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