<commit_before>/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package run

import (
	"context"
	"flag"
	"fmt"
	"os"

	// TODO: Use k/release/pkg/log instead
	"k8s.io/klog/v2"
	"k8s.io/release/pkg/promobot"
)

func main() {
	klog.InitFlags(nil)

	var options promobot.PromoteFilesOptions
	options.PopulateDefaults()

	flag.StringVar(
		&options.FilestoresPath,
		"filestores",
		options.FilestoresPath,
		"the manifest of filestores (REQUIRED)")
	flag.StringVar(
		&options.FilesPath,
		"files",
		options.FilesPath,
		"path to the files manifest (REQUIRED).  A directory can be specified.")
	flag.BoolVar(
		&options.DryRun,
		"dry-run",
		options.DryRun,
		"print what would have happened by running this tool;"+
			" do not actually modify any registry")

	flag.BoolVar(
		&options.UseServiceAccount,
		"use-service-account",
		options.UseServiceAccount,
		"allow service account usage with gcloud calls"+
			" (default: false)")

	flag.Parse()

	ctx := context.Background()
	if err := promobot.RunPromoteFiles(ctx, options); err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		// nolint[gomnd]
		os.Exit(1)
	} else {
		os.Exit(0)
	}
}<commit_msg>kpromo: Enable file promotion<commit_after>/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package run

import (
	"context"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"

	"k8s.io/release/pkg/promobot"
)

// filesCmd represents the subcommand for `kpromo run files`
var filesCmd = &cobra.Command{
	Use:           "files",
	Short:         "Promote files from a staging object store to production",
	SilenceUsage:  true,
	SilenceErrors: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		return errors.Wrap(runFilePromotion(filesOpts), "run `kpromo run files`")
	},
}

var (
	filesOpts = &promobot.PromoteFilesOptions{}
)

func init() {
	// TODO: Move this into a default options function in pkg/promobot
	filesOpts.PopulateDefaults()

	filesCmd.PersistentFlags().StringVar(
		&filesOpts.FilestoresPath,
		"filestores",
		filesOpts.FilestoresPath,
		"path to the `filestores` promoter manifest",
	)

	filesCmd.PersistentFlags().StringVar(
		&filesOpts.FilesPath,
		"files",
		filesOpts.FilesPath,
		"path to the `files` manifest",
	)

	// TODO: Consider moving this to the root command
	filesCmd.PersistentFlags().BoolVar(
		&filesOpts.DryRun,
		"dry-run",
		filesOpts.DryRun,
		"test run promotion without modifying any filestore",
	)

	filesCmd.PersistentFlags().BoolVar(
		&filesOpts.UseServiceAccount,
		"use-service-account",
		filesOpts.UseServiceAccount,
		"allow service account usage with gcloud calls",
	)

	// TODO: Consider moving this into a validation function
	filesCmd.MarkPersistentFlagRequired("filestores")
	filesCmd.MarkPersistentFlagRequired("files")

	RunCmd.AddCommand(filesCmd)
}

func runFilePromotion(opts *promobot.PromoteFilesOptions) error {
	ctx := context.Background()

	return promobot.RunPromoteFiles(ctx, *opts)
}

// TODO: Validate options
{'fraction_non_alphanumeric': 0.10233258088788563, 'fraction_numerical': 0.006019563581640331, 'mean_word_length': 4.353020134228188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7191371', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1270, 'n_words': 397}