Skip to content

Commit

Permalink
feat(profile): add
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienJourdain committed Jun 30, 2023
1 parent 13e705d commit c887ec2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 41 deletions.
120 changes: 120 additions & 0 deletions checks/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package checks

import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/viper"
)

type Layer struct {
Name string
FullPath string
InitStatus bool
RefreshTime int
}

func RunProfileCheck() {
layers, _ := GetLayers()
for _, layer := range layers {
fmt.Println("Initializing layer", layer.FullPath)
err := layer.Init()
if err != nil {
panic(err)
}
err = layer.GetRefreshTime()
if err != nil {
panic(err)
}
}
}

func GetLayers() ([]Layer, error) {
root := viper.GetString("codebase-path") // Root directory to start browsing from
layers := []Layer{}

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Check if the current path is a file and its name matches "terragrunt.hcl"
if !info.IsDir() && info.Name() == "terragrunt.hcl" {
layers = append(layers, Layer{Name: path[len(root) : len(path)-len(info.Name())-1], FullPath: path[:len(path)-len(info.Name())-1]})
}

return nil
})

if err != nil {
fmt.Println("Error:", err)
}
return layers, nil
}

// Function to initialize a layer (terragrunt)
func (l *Layer) Init() error {
// Create the terragrunt command
terragruntCmd := exec.Command("terragrunt", "init")

// Set the command's working directory to the Terragrunt configuration directory
terragruntCmd.Dir = l.FullPath

// Redirect the command's output to the standard output
terragruntCmd.Stdout = os.Stdout
terragruntCmd.Stderr = os.Stderr

// Run the terragrunt command
err := terragruntCmd.Run()
if err != nil {
return err
}

l.InitStatus = true

return nil
}

// Function to generate a layer plan using terragrunt
func (l *Layer) GetRefreshTime() error {
// Create the terragrunt command
terragruntCmd := exec.Command("terragrunt", "state", "list")

// Set the command's working directory to the Terragrunt configuration directory
terragruntCmd.Dir = l.FullPath

// Create a pipe to capture the stdout
stdoutPipe, err := terragruntCmd.StdoutPipe()
if err != nil {
return err
}

// Start the terragrunt command
err = terragruntCmd.Start()
if err != nil {
return err
}

// Create a scanner to read from the stdout pipe
scanner := bufio.NewScanner(stdoutPipe)
lineCount := 0

// Read each line from the stdout and count the number of lines
for scanner.Scan() {
lineCount++
}

// Wait for the command to finish
err = terragruntCmd.Wait()
if err != nil {
return err
}

// Set the refresh time to the number of lines
l.RefreshTime = lineCount

return nil
}
41 changes: 0 additions & 41 deletions cmd/iterate.go

This file was deleted.

35 changes: 35 additions & 0 deletions cmd/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"guacamole/checks"

"github.com/spf13/cobra"
)

// profileCmd represents the profile command
var profileCmd = &cobra.Command{
Use: "profile",
Short: "Estimate the refresh time of your codebase",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("profile called")
checks.RunProfileCheck()
},
}

func init() {
rootCmd.AddCommand(profileCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// profileCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// profileCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

0 comments on commit c887ec2

Please sign in to comment.