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: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AddCmd = &cobra.Command{
		Use:           "add",
		Short:         "adds a bucket policy configuration for the target bucket by specifying a valid policy file",
		SilenceUsage:  false,
		SilenceErrors: true,
		Example: `# add a bucket policy configuration onto target bucket
s3-manager bucketpolicy add my_custom_policy.json
		`,
		RunE: func(cmd *cobra.Command, args []string) (err error) {
			var rootOpts *rootopts.RootOptions
			svc, rootOpts, logger, confirmRunner = utils.PrepareConstants(cmd)
			bucketPolicyOpts.RootOptions = rootOpts

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

			logger = logger.With().Str("policyFilePath", args[0]).Logger()

			logger.Info().Msg("trying to read target policy file")
			file, err := os.Open(args[0])
			if err != nil {
				logger.Error().Msg(err.Error())
				return err
			}

			defer func() {
				if err := file.Close(); err != nil {
					panic(err)
				}
			}()

			content, err := io.ReadAll(file)
			if err != nil {
				logger.Error().Msg(err.Error())
				return err
			}

			logger.Info().Msg("successfully read target policy file")
			bucketPolicyOpts.BucketPolicyContent = string(content)

			logger.Info().Msg("will attempt to add below bucket policy")
			fmt.Println(bucketPolicyOpts.BucketPolicyContent)
			if bucketPolicyOpts.DryRun {
				logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
				return nil
			}

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

					return constants.ErrInvalidInput
				}
			}

			logger.Info().Msg("trying to add bucket policy")
			_, err = aws.SetBucketPolicy(svc, bucketPolicyOpts)
			if err != nil {
				logger.Error().
					Str("error", err.Error()).
					Msg("an error occurred while setting bucket policy")
				return err
			}

			logger.Info().Msg("successfully set bucket policy with target file content on target bucket")

			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