This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: generate: create a local Spacefile without a new project in Space
Signed-off-by: brkp <[email protected]>
- Loading branch information
1 parent
3441d6c
commit 63adbae
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/deta/space/cmd/utils" | ||
"github.com/deta/space/pkg/components/styles" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdGenerate() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate [flags]", | ||
Short: "Create a Spacefile without a new project in Space", | ||
PostRunE: utils.CheckLatestVersion, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
projectDir, _ := cmd.Flags().GetString("dir") | ||
blankProject, _ := cmd.Flags().GetBool("blank") | ||
projectName, _ := cmd.Flags().GetString("name") | ||
|
||
if !cmd.Flags().Changed("name") { | ||
abs, err := filepath.Abs(projectDir) | ||
if err != nil { | ||
utils.Logger.Printf("%sError getting absolute path of project directory: %s", styles.ErrorExclamation, err.Error()) | ||
return err | ||
} | ||
|
||
name := filepath.Base(abs) | ||
projectName, err = selectProjectName(name) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Create spacefile if it doesn't exist | ||
spaceFilePath := filepath.Join(projectDir, "Spacefile") | ||
if _, err := os.Stat(spaceFilePath); errors.Is(err, os.ErrNotExist) { | ||
err := createSpacefile(projectDir, projectName, blankProject) | ||
if err != nil { | ||
utils.Logger.Printf("failed to create spacefile: %s", err) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
PreRunE: utils.CheckAll( | ||
utils.CheckExists("dir"), | ||
func(cmd *cobra.Command, args []string) error { | ||
if cmd.Flags().Changed("name") { | ||
name, _ := cmd.Flags().GetString("name") | ||
return validateProjectName(name) | ||
} | ||
|
||
return nil | ||
}), | ||
} | ||
|
||
cmd.Flags().StringP("name", "n", "", "project name") | ||
cmd.Flags().StringP("dir", "d", "./", "src of project to release") | ||
cmd.MarkFlagDirname("dir") | ||
cmd.Flags().BoolP("blank", "b", false, "create blank project") | ||
|
||
if !utils.IsOutputInteractive() { | ||
cmd.MarkFlagRequired("name") | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters