Skip to content

Instantly share code, notes, and snippets.

@sarthaksarthak9
Created March 21, 2025 20:27
Show Gist options
  • Select an option

  • Save sarthaksarthak9/3fc50f149dd9b0408da5ea053f2ba0db to your computer and use it in GitHub Desktop.

Select an option

Save sarthaksarthak9/3fc50f149dd9b0408da5ea053f2ba0db to your computer and use it in GitHub Desktop.
package scaffolds

// Scaffolder scaffolds the Kubebuilder updater files
type Scaffolder struct {
	// Config is the project configuration
	Config config.Config

	// ScheduleInterval is the update check interval (in cron format)
	ScheduleInterval string

	// CreatePR determines if the GitHub Action should create a Pull Request
	CreatePR bool

	// Project name is the name to use in GitHub Actions
	ProjectName string

	// Fs is the filesystem to scaffold to
	Fs machinery.Filesystem
}

// NewScaffolder returns a new Scaffolder for updater plugin
func NewScaffolder(config config.Config, scheduleInterval string, createPR bool) *Scaffolder {
	return &Scaffolder{
		Config:           config,
		ScheduleInterval: scheduleInterval,
		CreatePR:         createPR,
		ProjectName:      config.GetProjectName(),
	}
}

// Scaffold implements Scaffolder
func (s *Scaffolder) Scaffold() error {
	fmt.Println("Scaffolding Kubebuilder updater GitHub Action...")

	// Initialize the machinery
	if err := s.initialize(); err != nil {
		return err
	}

	// Initialize the scaffolding machinery
	scaffolder := machinery.NewScaffolder(s.Fs)

	// Scaffold the GitHub Action workflow file
	if err := scaffolder.Execute(
		s.newGitHubActionWorkflow(),
	); err != nil {
		return err
	}

	// Scaffold the update script file
	if err := scaffolder.Execute(
		s.newUpdateScript(),
	); err != nil {
		return err
	}

	return nil
}

// initialize will set up the Scaffolder
func (s *Scaffolder) initialize() error {
	s.Fs = machinery.Filesystem{
		FS: machinery.OSFileSystem{},
	}

	return nil
}

// newGitHubActionWorkflow returns a new GitHub Action workflow file scaffolder
func (s *Scaffolder) newGitHubActionWorkflow() machinery.Builder {
	return &templates.GitHubActionWorkflow{
		ProjectName:      s.ProjectName,
		ProjectVersion:   s.Config.GetVersion().Number,
		Domain:           s.Config.GetDomain(),
		Repo:             s.Config.GetRepository(),
		ScheduleInterval: s.ScheduleInterval,
		CreatePR:         s.CreatePR,
	}
}

// newUpdateScript returns a new update script file scaffolder
func (s *Scaffolder) newUpdateScript() machinery.Builder {
	return &templates.UpdateScript{
		ProjectName:    s.ProjectName,
		ProjectVersion: s.Config.GetVersion().Number,
		Domain:         s.Config.GetDomain(),
		Repo:           s.Config.GetRepository(),
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment