github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/ddevapp/commands.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"github.com/drud/ddev/pkg/fileutil"
     5  	"github.com/drud/ddev/pkg/globalconfig"
     6  	"github.com/drud/ddev/pkg/util"
     7  	copy2 "github.com/otiai10/copy"
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  // PopulateCustomCommandFiles sets up the custom command files in the project
    13  // directories where they need to go.
    14  func PopulateCustomCommandFiles(app *DdevApp) error {
    15  
    16  	sourceGlobalCommandPath := filepath.Join(globalconfig.GetGlobalDdevDir(), "commands")
    17  	err := os.MkdirAll(sourceGlobalCommandPath, 0755)
    18  	if err != nil {
    19  		return nil
    20  	}
    21  
    22  	projectCommandPath := app.GetConfigPath("commands")
    23  	// Make sure our target global command directory is empty
    24  	copiedGlobalCommandPath := app.GetConfigPath(".global_commands")
    25  	err = os.MkdirAll(copiedGlobalCommandPath, 0755)
    26  	if err != nil {
    27  		util.Error("Unable to create directory %s: %v", copiedGlobalCommandPath, err)
    28  		return nil
    29  	}
    30  
    31  	// Make sure it's empty
    32  	err = fileutil.PurgeDirectory(copiedGlobalCommandPath)
    33  	if err != nil {
    34  		util.Error("Unable to remove %s: %v", copiedGlobalCommandPath, err)
    35  		return nil
    36  	}
    37  
    38  	err = copy2.Copy(sourceGlobalCommandPath, copiedGlobalCommandPath)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	if !fileutil.FileExists(projectCommandPath) || !fileutil.IsDirectory(projectCommandPath) {
    44  		return nil
    45  	}
    46  	return nil
    47  }