github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/modinstaller/verify_location.go (about)

     1  package modinstaller
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/fatih/color"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  	filehelpers "github.com/turbot/go-kit/files"
    12  	"github.com/turbot/steampipe/pkg/constants"
    13  	"github.com/turbot/steampipe/pkg/utils"
    14  )
    15  
    16  // ValidateModLocation checks whether you are running from the home directory or if you have
    17  // a lot of non .sql and .sp file in your current directory, and asks for confirmation to continue
    18  func ValidateModLocation(ctx context.Context, workspacePath string) (bool, error) {
    19  	const MaxResults = 10
    20  	cmd := viper.Get(constants.ConfigKeyActiveCommand).(*cobra.Command)
    21  	home, _ := os.UserHomeDir()
    22  
    23  	// check if running in home directory
    24  	if workspacePath == home {
    25  		cont, err := utils.UserConfirmation(ctx, fmt.Sprintf("%s: Creating a mod file in the home directory is not recommended.\nBest practice is to create a new directory and run %s from there.\nDo you want to continue? (y/n)", color.YellowString("Warning"), constants.Bold(fmt.Sprintf("steampipe mod %s", cmd.Name()))))
    26  		if err != nil {
    27  			return false, err
    28  		}
    29  		return cont, nil
    30  	}
    31  	// else check if running in a directory containing lot of sql and sp files
    32  	fileList, _ := filehelpers.ListFilesWithContext(ctx, workspacePath, &filehelpers.ListOptions{
    33  		Flags:      filehelpers.FilesRecursive,
    34  		Exclude:    filehelpers.InclusionsFromExtensions([]string{".sql", ".sp"}),
    35  		MaxResults: MaxResults,
    36  	})
    37  	if len(fileList) == MaxResults {
    38  		cont, err := utils.UserConfirmation(ctx, fmt.Sprintf("%s: Creating a mod file in a directory with a lot of files or subdirectories is not recommended.\nBest practice is to create a new directory and run %s from there.\nDo you want to continue? (y/n)", color.YellowString("Warning"), constants.Bold(fmt.Sprintf("steampipe mod %s", cmd.Name()))))
    39  		if err != nil {
    40  			return false, err
    41  		}
    42  		return cont, nil
    43  	}
    44  	return true, nil
    45  }