energy/cmd/internal/init.go

69 lines
1.5 KiB
Go
Raw Normal View History

//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
// 创建 energy 项目
package internal
2023-09-03 18:40:44 +08:00
import (
"fmt"
"github.com/energye/energy/v2/cmd/internal/command"
"github.com/energye/energy/v2/cmd/internal/initialize"
2023-09-11 17:32:42 +08:00
"github.com/energye/energy/v2/cmd/internal/term"
"github.com/pterm/pterm"
"os"
"strings"
2023-09-03 18:40:44 +08:00
)
2023-09-03 18:40:44 +08:00
var CmdInit = &command.Command{
UsageLine: "init -n [name]",
Short: "Initialized energy project",
Long: `
2023-09-11 19:49:35 +08:00
Initialize energy golang project
-n Initialized project name
`,
}
func init() {
CmdInit.Run = runInit
}
2023-09-03 18:40:44 +08:00
func runInit(c *command.Config) error {
m := &c.Init
if strings.TrimSpace(m.Name) == "" {
for strings.TrimSpace(m.Name) == "" {
print("Project Name: ")
fmt.Scan(&m.Name)
println()
}
}
m.Name = strings.TrimSpace(m.Name)
2023-09-11 17:32:42 +08:00
options := []string{"HTTP", "Local Load"}
printer := term.DefaultInteractiveSelect.WithOnInterruptFunc(func() {
os.Exit(1)
}).WithOptions(options)
printer.CheckmarkANSI()
printer.DefaultText = "Resource Loading. Default HTTP"
printer.Filter = false
selectedOption, err := printer.Show()
if err != nil {
return err
}
2023-09-11 17:32:42 +08:00
pterm.Info.Printfln("Selected: %s", pterm.Green(selectedOption))
if selectedOption == "" || selectedOption == "HTTP" {
m.ResLoad = "1"
2023-09-11 17:32:42 +08:00
} else if selectedOption == "Local Load" {
m.ResLoad = "2"
}
2023-09-11 17:32:42 +08:00
return initialize.InitEnergyProject(c)
}