<commit_before><commit_msg>runbuild command<commit_after>package cmd

import (
	"context"
	"fmt"
	"time"

	"github.com/gofrs/uuid"
	"github.com/spf13/cobra"

	"github.com/dollarshaveclub/furan/pkg/builder"
	"github.com/dollarshaveclub/furan/pkg/buildkit"
	"github.com/dollarshaveclub/furan/pkg/datalayer"
	"github.com/dollarshaveclub/furan/pkg/github"
	"github.com/dollarshaveclub/furan/pkg/models"
	"github.com/dollarshaveclub/furan/pkg/s3"
	"github.com/dollarshaveclub/furan/pkg/tagcheck"
)

var runbuildCmd = &cobra.Command{
	Use:   "runbuild",
	Short: "Run a build job",
	Long: `This executes a build job with a running instance of BuildKit.

This is intended to be used in a Kubernetes pod with BuildKit as part of an automated build job
created by pkg/jobrunner.`,
	PreRun: func(cmd *cobra.Command, args []string) {
		gitHubSecrets()
		awsSecrets()
		quaySecrets()
		dbSecrets()
	},
	RunE: runbuild,
}

var buildid, bkaddr string
var runbuildtimeout time.Duration

func init() {
	runbuildCmd.PersistentFlags().StringVar(&buildid, "build-id", "", "Build ID")
	runbuildCmd.PersistentFlags().StringVar(&bkaddr, "buildkit-addr", "", "BuildKit UNIX socket address (unix:///path/to/socket)")
	runbuildCmd.PersistentFlags().DurationVar(&runbuildtimeout, "timeout", 30*time.Minute, "max build duration/timeout")
	RootCmd.AddCommand(runbuildCmd)
}

func runbuild(cmd *cobra.Command, args []string) error {
	dl, err := datalayer.NewPostgresDBLayer(dbConfig.PostgresURI)
	if err != nil {
		return fmt.Errorf("error configuring database: %w", err)
	}

	cm := &s3.CacheManager{
		AccessKeyID:     awsConfig.AccessKeyID,
		SecretAccessKey: awsConfig.SecretAccessKey,
		Region:          awsConfig.Region,
		Bucket:          awsConfig.CacheBucket,
		Keypfx:          awsConfig.CacheKeyPrefix,
		DL:              dl,
	}

	tc := &tagcheck.Checker{
		Quay: &tagcheck.QuayChecker{APIToken: quayConfig.Token},
		ECR: &tagcheck.ECRChecker{
			AccessKeyID:     awsConfig.AccessKeyID,
			SecretAccessKey: awsConfig.SecretAccessKey,
		},
	}

	bks, err := buildkit.NewBuildSolver(bkaddr, cm, dl)
	if err != nil {
		return fmt.Errorf("error initializing build solver: %w", err)
	}

	bm := &builder.Manager{
		DL:             dl,
		BRunner:        bks,
		TCheck:         tc,
		FetcherFactory: func(tkn string) models.CodeFetcher { return github.NewGitHubFetcher(tkn) },
		GitHubTokenKey: dbConfig.CredEncKeyArray,
	}

	bid, err := uuid.FromString(buildid)
	if err != nil {
		return fmt.Errorf("invalid build id: %w", err)
	}

	ctx, cf := context.WithTimeout(context.Background(), runbuildtimeout)
	defer cf()

	return bm.Run(ctx, bid)
}
{'fraction_non_alphanumeric': 0.12930374904361133, 'fraction_numerical': 0.0022953328232593728, 'mean_word_length': 4.261569416498994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1724461', 'n_tokens_mistral': 983, 'n_tokens_neox': 940, 'n_words': 193}