126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var orgName, manifestsRepoName, fluxRepoName string
|
|
|
|
//go:embed templates/manifests.md.tmpl
|
|
var manifestsReadmeTmpl string
|
|
|
|
//go:embed templates/flux.md.tmpl
|
|
var fluxReadmeTmpl string
|
|
|
|
func main() {
|
|
var rootCmd = &cobra.Command{
|
|
Use: "cicd-tool",
|
|
Short: "A CLI tool to manage CI/CD setup for applications.",
|
|
}
|
|
|
|
var repoCmd = &cobra.Command{
|
|
Use: "repo",
|
|
Short: "Manage CI/CD repositories.",
|
|
}
|
|
|
|
var vaultCmd = &cobra.Command{
|
|
Use: "vault",
|
|
Short: "Manage Vault secrets for CI/CD.",
|
|
}
|
|
|
|
var initCmd = &cobra.Command{
|
|
Use: "init",
|
|
Short: "Initializes the manifests and flux repositories with the required structure.",
|
|
Run: runInitRepo,
|
|
}
|
|
|
|
var createDockerSecretCmd = &cobra.Command{
|
|
Use: "create-docker-secret",
|
|
Short: "Creates a Docker registry secret in Vault for Docker Hub.",
|
|
Run: runCreateDockerSecret,
|
|
}
|
|
|
|
var createGhcrSecretCmd = &cobra.Command{
|
|
Use: "create-ghcr-secret",
|
|
Short: "Creates a Docker registry secret in Vault for GHCR.",
|
|
Run: runCreateGhcrSecret,
|
|
}
|
|
|
|
var createGithubSecretCmd = &cobra.Command{
|
|
Use: "create-github-secret",
|
|
Short: "Creates or updates a GitHub authentication secret in Vault.",
|
|
Run: runCreateGithubSecret,
|
|
}
|
|
|
|
// Define command flags for repo init
|
|
initCmd.Flags().StringVar(&orgName, "org", "", "The GitHub organization (e.g., Free-Maidn)")
|
|
initCmd.Flags().StringVar(&manifestsRepoName, "manifests-repo", "cicd-deployment-manifests", "The name of the manifests repository")
|
|
initCmd.Flags().StringVar(&fluxRepoName, "flux-repo", "", "The name of the flux repository")
|
|
initCmd.MarkFlagRequired("org")
|
|
initCmd.MarkFlagRequired("flux-repo")
|
|
|
|
// Add commands to their parent commands
|
|
repoCmd.AddCommand(initCmd)
|
|
vaultCmd.AddCommand(createDockerSecretCmd)
|
|
vaultCmd.AddCommand(createGhcrSecretCmd)
|
|
vaultCmd.AddCommand(createGithubSecretCmd)
|
|
rootCmd.AddCommand(repoCmd)
|
|
rootCmd.AddCommand(vaultCmd)
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runInitRepo(cmd *cobra.Command, args []string) {
|
|
// Check system dependencies
|
|
fmt.Println("[INFO] Checking dependencies (git and gh)...")
|
|
if !commandExists("git") || !commandExists("gh") {
|
|
fmt.Println("[ERROR] 'git' and 'gh' must be installed and in your PATH.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Verify GitHub authentication
|
|
fmt.Println("[INFO] Checking GitHub authentication...")
|
|
if err := runCommandQuiet("gh", "auth", "status"); err != nil {
|
|
fmt.Println("[ERROR] You are not logged into the GitHub CLI. Please run 'gh auth login'.")
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("[SUCCESS] Dependencies and authentication are OK.")
|
|
|
|
// Initialize both repositories
|
|
initializeManifestsRepo()
|
|
initializeFluxRepo()
|
|
|
|
fmt.Println("\n🎉 Onboarding complete! 🎉")
|
|
fmt.Printf("✅ Manifests repository: https://github.com/%s/%s\n", orgName, manifestsRepoName)
|
|
fmt.Printf("✅ Flux repository: https://github.com/%s/%s\n", orgName, fluxRepoName)
|
|
}
|
|
|
|
func runCreateDockerSecret(cmd *cobra.Command, args []string) {
|
|
if err := createDockerHubSecretWorkflow(); err != nil {
|
|
fmt.Printf("[ERROR] Failed to create Docker Hub secret: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runCreateGhcrSecret(cmd *cobra.Command, args []string) {
|
|
if err := createGhcrSecretWorkflow(); err != nil {
|
|
fmt.Printf("[ERROR] Failed to create GHCR secret: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runCreateGithubSecret(cmd *cobra.Command, args []string) {
|
|
if err := createAndStoreGithubAuthSecret(); err != nil {
|
|
fmt.Printf("[ERROR] Failed to create GitHub auth secret: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("[SUCCESS] GitHub auth secret created successfully in Vault!")
|
|
}
|