Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
cmd: generate: create a local Spacefile without a new project in Space
Browse files Browse the repository at this point in the history
Signed-off-by: brkp <[email protected]>
  • Loading branch information
nullptropy committed Sep 13, 2023
1 parent 3441d6c commit 63adbae
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/generate.go
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
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Complete documentation available at %s`, utils.DocsUrl),
cmd.AddCommand(newCmdPrintAccessToken())
cmd.AddCommand(newCmdTrigger())
cmd.AddCommand(newCmdBuilder())
cmd.AddCommand(newCmdGenerate())

// XXX: This will prevent the usage from being displayed when an error occurs
// while calling the Execute function in the main.go file.
Expand Down

0 comments on commit 63adbae

Please sign in to comment.