Compare commits
No commits in common. "f4f3164978d2c72885291d3e063966cb251603d4" and "a0e852feaba3898f5ce0baa7fe0a8103afa0f818" have entirely different histories.
f4f3164978
...
a0e852feab
1
go/.gitignore
vendored
1
go/.gitignore
vendored
@ -1 +0,0 @@
|
||||
/test
|
@ -5,8 +5,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"ocl/portainer-devtool/utils"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -78,16 +78,39 @@ func (config *Config) Summarize() {
|
||||
// The configuration also can be updated later
|
||||
func initializeConfig(w io.WriteCloser) (*Config, error) {
|
||||
config := &Config{}
|
||||
config.ProjectPath = utils.Prompt("Specify Git Project Root Path")
|
||||
|
||||
// analyze all the repositories in the project root path
|
||||
// add the parsed information to RepositoryConfig
|
||||
config.configureRepositories()
|
||||
fmt.Printf("Initialize devtool path:\n (Project path will store volumes and repositories)")
|
||||
fmt.Scanf("%s", &(config.ProjectPath))
|
||||
|
||||
// generate volume path automatically
|
||||
config.VolumePath = utils.Prompt("Specify Volume Path")
|
||||
config.VolumePath = path.Join(config.ProjectPath, "volumes")
|
||||
|
||||
var loginCredential LoginCredential
|
||||
fmt.Printf("Set login credential username(admin): ")
|
||||
fmt.Scanf("%s", &(loginCredential.Username))
|
||||
if loginCredential.Username == "" {
|
||||
loginCredential.Username = "admin"
|
||||
}
|
||||
|
||||
for {
|
||||
fmt.Printf("Set login credential password(******): ")
|
||||
fmt.Scanf("%s", &(loginCredential.Password))
|
||||
if loginCredential.Password != "" {
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Println("Login credential password must be provided")
|
||||
}
|
||||
|
||||
fmt.Printf("Set login address(127.0.0.1): ")
|
||||
fmt.Scanf("%s", &(loginCredential.Address))
|
||||
if loginCredential.Address == "" {
|
||||
loginCredential.Address = "http://127.0.0.1:9000/api/auth"
|
||||
} else {
|
||||
loginCredential.Address = fmt.Sprintf("http://%s:9000/api/auth", loginCredential.Address)
|
||||
}
|
||||
|
||||
config.LoginCredential = loginCredential
|
||||
|
||||
config.configureLoginCredential()
|
||||
// able to configure multiple project
|
||||
// if utils.PromptConfirm("Do you want to configure the repository now?") {
|
||||
// // configure repository
|
||||
|
@ -1,32 +0,0 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ocl/portainer-devtool/utils"
|
||||
)
|
||||
|
||||
func (config *Config) configureLoginCredential() {
|
||||
var loginCredential LoginCredential
|
||||
loginCredential.Username = utils.Prompt("Set Login Credential Username(admin)")
|
||||
if loginCredential.Username == "" {
|
||||
loginCredential.Username = "admin"
|
||||
}
|
||||
|
||||
for {
|
||||
loginCredential.Password = utils.Prompt("Set Login Credential Password(*****)")
|
||||
if loginCredential.Password != "" {
|
||||
break
|
||||
}
|
||||
|
||||
utils.WarnPrint("Login Credential Password must be provided")
|
||||
}
|
||||
|
||||
loginCredential.Address = utils.Prompt("Set Login Address(127.0.0.1)")
|
||||
if loginCredential.Address == "" {
|
||||
loginCredential.Address = "http://127.0.0.1:9000/api/auth"
|
||||
} else {
|
||||
loginCredential.Address = fmt.Sprintf("http://%s:9000/api/auth", loginCredential.Address)
|
||||
}
|
||||
|
||||
config.LoginCredential = loginCredential
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"ocl/portainer-devtool/utils"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func (config *Config) configureRepositories() {
|
||||
if config.RepositoryConfig == nil {
|
||||
config.RepositoryConfig = make(map[string]RepositoryConfig)
|
||||
}
|
||||
for {
|
||||
if !utils.PromptConfirm("Set up new repository") {
|
||||
break
|
||||
}
|
||||
|
||||
repoConfig := RepositoryConfig{}
|
||||
repoConfig.Name = utils.Prompt("Name")
|
||||
repoConfig.URL = utils.Prompt("URL")
|
||||
repoConfig.Directory = utils.Prompt("Directory")
|
||||
config.RepositoryConfig[repoConfig.Name] = repoConfig
|
||||
}
|
||||
|
||||
utils.HighlightPrint("Configure repositories completed")
|
||||
}
|
||||
|
||||
func (config *Config) generateRepositoriesBasedOnProjectPath(projectPath string) error {
|
||||
|
||||
filepath.WalkDir(projectPath, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
log.Printf("fail to walk in the project path %s, error: %v\n", projectPath, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if utils.MatchPathLength(projectPath, path, 1) {
|
||||
fmt.Println(path)
|
||||
|
||||
// posLastSeparator := strings.LastIndex(path, string(filepath.Separator))
|
||||
// repoName := path[posLastSeparator+1:]
|
||||
|
||||
// repoConfig := RepositoryConfig{
|
||||
// Name: repoName,
|
||||
// // URL:
|
||||
// }
|
||||
// config.RepositoryConfig[repoName] =
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
@ -20,6 +20,8 @@ func main() {
|
||||
tasks.NewCurlLookupTask(),
|
||||
tasks.NewCodeSecurityScanTask(),
|
||||
tasks.NewListDevToolCommandTask(config),
|
||||
|
||||
tasks.NewExitTask(),
|
||||
}
|
||||
|
||||
tasks.ListCommandMenu(taskItems, "Which repository of action do you want operate:")
|
||||
|
@ -18,7 +18,6 @@ func NewListDevToolCommandTask(cfg *configs.Config) *ListDevToolCommandTask {
|
||||
func (task *ListDevToolCommandTask) Execute() error {
|
||||
subTaskItems := []Tasker{
|
||||
subtasks.NewListVolumeSubTask(task.Config),
|
||||
subtasks.NewListRepositorySubTask(task.Config),
|
||||
}
|
||||
|
||||
ListCommandMenu(subTaskItems, "Which management commands do you want to choose:")
|
||||
|
@ -1,34 +0,0 @@
|
||||
package subtasks
|
||||
|
||||
import (
|
||||
"ocl/portainer-devtool/configs"
|
||||
"ocl/portainer-devtool/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ListRepositorySubTask struct {
|
||||
Config *configs.Config
|
||||
}
|
||||
|
||||
func NewListRepositorySubTask(cfg *configs.Config) *ListRepositorySubTask {
|
||||
return &ListRepositorySubTask{
|
||||
Config: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (task *ListRepositorySubTask) Execute() error {
|
||||
utils.InputPrint("Which repository?")
|
||||
|
||||
repositoryList := []string{" "}
|
||||
for _, repo := range task.Config.RepositoryConfig {
|
||||
repositoryList = append(repositoryList, repo.Name)
|
||||
}
|
||||
|
||||
utils.SuccessPrint(strings.Join(repositoryList, "\n"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (task *ListRepositorySubTask) String() string {
|
||||
return "List Repositories"
|
||||
}
|
@ -22,6 +22,8 @@ func NewListVolumeSubTask(cfg *configs.Config) *ListVolumeSubTask {
|
||||
func (task *ListVolumeSubTask) Execute() error {
|
||||
utils.HighlightPrint(fmt.Sprintf("Volume path: %s", task.Config.VolumePath))
|
||||
|
||||
volumeLength := lenPath(task.Config.VolumePath)
|
||||
|
||||
volumeList := []string{" "}
|
||||
filepath.WalkDir(task.Config.VolumePath, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
@ -33,7 +35,8 @@ func (task *ListVolumeSubTask) Execute() error {
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
if utils.MatchPathLength(task.Config.VolumePath, path, 1) {
|
||||
dirLength := lenPath(path)
|
||||
if volumeLength+1 == dirLength {
|
||||
volumeList = append(volumeList, d.Name())
|
||||
}
|
||||
}
|
||||
@ -47,5 +50,9 @@ func (task *ListVolumeSubTask) Execute() error {
|
||||
}
|
||||
|
||||
func (task *ListVolumeSubTask) String() string {
|
||||
return "List Volumes"
|
||||
return "List Volume"
|
||||
}
|
||||
|
||||
func lenPath(path string) int {
|
||||
return len(strings.Split(path, string(filepath.Separator)))
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ type Tasker interface {
|
||||
|
||||
// ListCommandMenu iterates task items to display them // on the screen as the menu options
|
||||
func ListCommandMenu(taskItems []Tasker, menuDesp string) error {
|
||||
taskItems = append(taskItems, NewExitTask())
|
||||
for {
|
||||
printMainMenu := func() {
|
||||
taskNames := []string{}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MatchPathLength matches the length of target path separated by path separator
|
||||
// to the length of base path separated by path separator plus offset
|
||||
func MatchPathLength(basePath, targetPath string, offset int) bool {
|
||||
basePathLength := len(strings.Split(basePath, string(filepath.Separator)))
|
||||
targetPathLength := len(strings.Split(targetPath, string(filepath.Separator)))
|
||||
if basePathLength+offset == targetPathLength {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@ -38,11 +38,6 @@ func ErrorPrint(message string) {
|
||||
fmt.Println(colorRed, message, colorReset)
|
||||
}
|
||||
|
||||
func WarnPrint(message string) {
|
||||
fmt.Println()
|
||||
fmt.Println(colorPurple, message, colorReset)
|
||||
}
|
||||
|
||||
func InputPrint(message string) {
|
||||
// adding \n before setting colorful output can
|
||||
// remove the first space in the colorful output
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func PromptContinue() bool {
|
||||
ret := strings.ToLower(Prompt("Continue (y/n)"))
|
||||
ret := strings.ToLower(prompt("Continue (y/n)"))
|
||||
if ret == "y" || ret == "yes" {
|
||||
return true
|
||||
}
|
||||
@ -15,7 +15,7 @@ func PromptContinue() bool {
|
||||
}
|
||||
|
||||
func PromptConfirm(question string) bool {
|
||||
ret := Prompt(fmt.Sprintf("%s (y/n)?", question))
|
||||
ret := fmt.Sprintf("%s (y/n)?", question)
|
||||
if ret == "y" || ret == "yes" {
|
||||
return true
|
||||
}
|
||||
@ -31,7 +31,7 @@ func SelectMenuItem(listMenu func()) string {
|
||||
return option
|
||||
}
|
||||
|
||||
func Prompt(question string) string {
|
||||
func prompt(question string) string {
|
||||
fmt.Printf("%s %s :%s", colorYellow, question, colorReset)
|
||||
var ret string
|
||||
fmt.Scanf("%s", &ret)
|
||||
|
Loading…
Reference in New Issue
Block a user