Skip to content

Commit

Permalink
add policy flag for limiting which policies to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4c6565 committed Nov 14, 2020
1 parent 96a10ec commit e41217a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Executes cleanup process
#### Flags

* `--dry-run`: Specifies execution should be ran in dry run mode. Tag deletions will not occur
* `--policy`: Specifies which policies should be ran. Defaults to all. Accepted comma-seperated list of policies. Can be repeated


## Config
Expand Down
22 changes: 21 additions & 1 deletion cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ExecuteCmd() *cobra.Command {

cmd.Flags().Bool("dry-run", false, "Specifies command should be ran in dry-run mode")
cmd.Flags().Bool("progress", false, "Outputs progress")
cmd.Flags().StringSlice("policy", []string{""}, "Limit policies to execute")

return cmd
}
Expand Down Expand Up @@ -108,9 +109,28 @@ func processRepositories(cmd *cobra.Command, client *gitlab.Client, cfg *config.
return nil
}

func stringInSlice(str string, slice []string) bool {
for _, sliceStr := range slice {
if str == sliceStr {
return true
}
}
return false
}

func processRepository(cmd *cobra.Command, client *gitlab.Client, cfg *config.Config, repository *gitlab.RegistryRepository, repositoryCfg config.RepositoryConfig) error {
var policyFilter []string
if cmd.Flags().Changed("policy") {
policyFilter, _ = cmd.Flags().GetStringSlice("policy")
}
for _, policyName := range repositoryCfg.Policies {
log.Infof("Processing repository policy %s", policyName)

if len(policyFilter) > 0 && !stringInSlice(policyName, policyFilter) {
log.Warnf("Skipping policy %s as not specified in policy flag", policyName)
continue
}

policyCfg, err := cfg.GetPolicyConfig(policyName)
if err != nil {
return err
Expand Down Expand Up @@ -187,7 +207,7 @@ func processRepositoryPolicy(cmd *cobra.Command, client *gitlab.Client, reposito
if dryRun {
log.Warnf("[DRY RUN]: %s", logLine)
} else {
log.Debug(logLine)
log.Info(logLine)
_, err := client.ContainerRegistry.DeleteRegistryRepositoryTag(repositoryCfg.Project, repository.ID, filteredTag.Name)
if err != nil {
return fmt.Errorf("Failed to remove tag %s: %w", filteredTag.Name, err)
Expand Down

0 comments on commit e41217a

Please sign in to comment.