add tests, fix bugs, and make cli usable without interactivity

This commit is contained in:
Zoe
2025-05-06 11:00:56 -05:00
parent 4ab58f6324
commit 5bb696052a
12 changed files with 216 additions and 47 deletions

View File

@@ -82,7 +82,7 @@ func DeleteCommand(ctx CommandCtx, args []string) error {
project, err := util.GetProject("delete", args, ctx.Config)
if err != nil {
return fmt.Errorf("\tfailed to get project name: %v.\n\tSee flux delete --help for more information", err)
return fmt.Errorf("\tfailed to get project name: %v.\n\tSee flux delete -help for more information", err)
}
// ask for confirmation if not --no-confirm

View File

@@ -6,6 +6,7 @@ import (
"bytes"
"compress/gzip"
"encoding/json"
"flag"
"fmt"
"io"
"mime/multipart"
@@ -175,11 +176,38 @@ func preprocessEnvFile(envFile string, target *[]string) error {
return nil
}
var deployUsage = `Usage:
flux deploy [flags]
Flags:
-help, -h: Show this help message
%s
Flux will deploy or redeploy the app in the current directory.
`
func DeployCommand(ctx CommandCtx, args []string) error {
if _, err := os.Stat("flux.json"); err != nil {
return fmt.Errorf("no flux.json found, please run flux init first")
}
fs := flag.NewFlagSet("deploy", flag.ExitOnError)
fs.Usage = func() {
var buf bytes.Buffer
// Redirect flagset to print to buffer instead of stdout
fs.SetOutput(&buf)
fs.PrintDefaults()
fmt.Println(deployUsage, strings.TrimRight(buf.String(), "\n"))
}
quiet := fs.Bool("q", false, "Don't print the deployment logs")
err := fs.Parse(args)
if err != nil {
return err
}
spinnerWriter := util.NewCustomSpinnerWriter()
loadingSpinner := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(spinnerWriter))
@@ -353,7 +381,10 @@ func DeployCommand(ctx CommandCtx, args []string) error {
}
return nil
case "cmd_output":
customWriter.Printf("... %s\n", data.Message)
// suppress the command output if the quiet flag is set
if quiet == nil || !*quiet {
customWriter.Printf("... %s\n", data.Message)
}
case "error":
loadingSpinner.Stop()
return fmt.Errorf("deployment failed: %s", data.Message)

View File

@@ -13,17 +13,20 @@ import (
)
var initUsage = `Usage:
flux init [project-name]
flux init [flags] [project-name]
Options:
project-name: The name of the project to initialize
Flux will initialize a new project in the current directory or the specified project.`
Flags:
-help, -h: Show this help message
%s
Flux will initialize a new project in the current directory or the specified project.
`
func InitCommand(ctx CommandCtx, args []string) error {
if !ctx.Interactive {
return fmt.Errorf("init command can only be run in interactive mode")
}
var projectConfig pkg.ProjectConfig
fs := flag.NewFlagSet("init", flag.ExitOnError)
fs.Usage = func() {
@@ -32,8 +35,10 @@ func InitCommand(ctx CommandCtx, args []string) error {
fs.SetOutput(&buf)
fs.PrintDefaults()
fmt.Println(initUsage)
fmt.Printf(initUsage, strings.TrimRight(buf.String(), "\n"))
}
hostUrl := fs.String("host-url", "", "The URL of the host")
projectPort := fs.Uint("project-port", 0, "The port of the host")
err := fs.Parse(args)
if err != nil {
@@ -43,10 +48,22 @@ func InitCommand(ctx CommandCtx, args []string) error {
args = fs.Args()
var projectConfig pkg.ProjectConfig
if !ctx.Interactive {
if hostUrl == nil || *hostUrl == "" {
return fmt.Errorf("host-url is required when not in interactive mode")
}
if projectPort == nil || *projectPort == 0 {
return fmt.Errorf("project-port is required when not in interactive mode")
}
if len(args) < 1 {
return fmt.Errorf("project-name is required when not in interactive mode")
}
}
var response string
if len(args) > 1 {
if len(args) > 0 {
response = args[0]
} else {
fmt.Println("What is the name of your project?")
@@ -55,28 +72,47 @@ func InitCommand(ctx CommandCtx, args []string) error {
projectConfig.Name = response
fmt.Println("What URL should your project listen to?")
fmt.Scanln(&response)
if strings.HasPrefix(response, "http") {
response = strings.TrimPrefix(response, "http://")
response = strings.TrimPrefix(response, "https://")
if hostUrl != nil && *hostUrl != "" {
if strings.HasPrefix(*hostUrl, "http") {
*hostUrl = strings.TrimPrefix(*hostUrl, "http://")
*hostUrl = strings.TrimPrefix(*hostUrl, "https://")
}
*hostUrl = strings.Split(*hostUrl, "/")[0]
projectConfig.Url = *hostUrl
} else {
fmt.Println("What URL should your project listen to?")
fmt.Scanln(&response)
if strings.HasPrefix(response, "http") {
response = strings.TrimPrefix(response, "http://")
response = strings.TrimPrefix(response, "https://")
}
response = strings.Split(response, "/")[0]
projectConfig.Url = response
}
response = strings.Split(response, "/")[0]
if projectPort != nil && *projectPort != 0 {
if *projectPort < 1024 || *projectPort > 65535 {
return fmt.Errorf("project-port must be between 1024 and 65535")
}
projectConfig.Url = response
projectConfig.Port = uint16(*projectPort)
} else {
fmt.Println("What port does your project listen to?")
fmt.Scanln(&response)
port, err := strconv.ParseUint(response, 10, 16)
portErr := fmt.Errorf("that doesnt look like a valid port, try a number between 1024 and 65535")
if port > 65535 {
return portErr
}
fmt.Println("What port does your project listen to?")
fmt.Scanln(&response)
port, err := strconv.ParseUint(response, 10, 16)
portErr := fmt.Errorf("that doesnt look like a valid port, try a number between 1024 and 65535")
if port > 65535 {
return portErr
}
projectConfig.Port = uint16(port)
if err != nil || projectConfig.Port < 1024 {
return portErr
projectConfig.Port = uint16(port)
if err != nil || projectConfig.Port < 1024 {
return portErr
}
}
configBytes, err := json.MarshalIndent(projectConfig, "", " ")

View File

@@ -35,9 +35,9 @@ Available Commands:
%s
Available Flags:
--help, -h: Show this help message
-help, -h: Show this help message
Use "flux <command> --help" for more information about a command.
Use "flux <command> -help" for more information about a command.
`
var maxDistance = 3