From 3aee09eb974a51e23b132c586624335ef194a19d Mon Sep 17 00:00:00 2001 From: Nicola Giacchetta Date: Thu, 4 Apr 2024 11:22:29 -0700 Subject: [PATCH] create ttp cmd not to fail if folder does not exists Summary: The create ttp cmd is failing if the folder where we want to create the new ttp does not exist: ``` [ngiacchetta@devvm1745.lla0 /data/users/ngiacchetta/nicola]$ ./ttpforge create ttp ttps/nicola/my-ttp.yaml WARN No config file specified and default configuration file not found! WARN You probably want to run `ttpforge init`! WARN However, if you know what you are doing, then carry on :) ERROR failed to run command: failed to create new TTP file: open ttps/nicola/my-ttp.yaml: no such file or directory ``` This diff fixes the issue. Differential Revision: D55757581 --- cmd/createttp.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/createttp.go b/cmd/createttp.go index b3f062b7..3e093131 100644 --- a/cmd/createttp.go +++ b/cmd/createttp.go @@ -21,12 +21,13 @@ package cmd import ( "fmt" - "text/template" - "github.com/facebookincubator/ttpforge/pkg/platforms" "github.com/google/uuid" "github.com/spf13/afero" "github.com/spf13/cobra" + "os" + "path/filepath" + "text/template" ) const ttpTemplate = `--- @@ -74,6 +75,12 @@ func buildCreateTTPCommand() *cobra.Command { return fmt.Errorf("%v already exists", newTTPFilePath) } + // create the directory for the new TTP file, if it doesn't already exist + err = fsys.MkdirAll(filepath.Dir(newTTPFilePath), os.ModePerm) + if err != nil { + return fmt.Errorf("failed to create directory for new TTP file: %w", err) + } + // create the new TTP file with afero f, err := fsys.Create(newTTPFilePath) if err != nil {