config: refactor data initialization process
This commit is contained in:
		
							parent
							
								
									889b7a4ca2
								
							
						
					
					
						commit
						f4f3164978
					
				| @ -5,8 +5,8 @@ import ( | |||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
|  | 	"ocl/portainer-devtool/utils" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| @ -78,39 +78,16 @@ func (config *Config) Summarize() { | |||||||
| // The configuration also can be updated later | // The configuration also can be updated later | ||||||
| func initializeConfig(w io.WriteCloser) (*Config, error) { | func initializeConfig(w io.WriteCloser) (*Config, error) { | ||||||
| 	config := &Config{} | 	config := &Config{} | ||||||
| 	fmt.Printf("Initialize devtool path:\n (Project path will store volumes and repositories)") | 	config.ProjectPath = utils.Prompt("Specify Git Project Root Path") | ||||||
| 	fmt.Scanf("%s", &(config.ProjectPath)) | 
 | ||||||
|  | 	// analyze all the repositories in the project root path | ||||||
|  | 	// add the parsed information to RepositoryConfig | ||||||
|  | 	config.configureRepositories() | ||||||
| 
 | 
 | ||||||
| 	// generate volume path automatically | 	// generate volume path automatically | ||||||
| 	config.VolumePath = path.Join(config.ProjectPath, "volumes") | 	config.VolumePath = utils.Prompt("Specify Volume Path") | ||||||
| 
 |  | ||||||
| 	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 | 	// able to configure multiple project | ||||||
| 	// if utils.PromptConfirm("Do you want to configure the repository now?") { | 	// if utils.PromptConfirm("Do you want to configure the repository now?") { | ||||||
| 	// 	// configure repository | 	// 	// configure repository | ||||||
|  | |||||||
							
								
								
									
										32
									
								
								go/configs/credential.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								go/configs/credential.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | |||||||
|  | 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 | ||||||
|  | } | ||||||
							
								
								
									
										54
									
								
								go/configs/repository.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								go/configs/repository.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,54 @@ | |||||||
|  | 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 | ||||||
|  | } | ||||||
| @ -38,6 +38,11 @@ func ErrorPrint(message string) { | |||||||
| 	fmt.Println(colorRed, message, colorReset) | 	fmt.Println(colorRed, message, colorReset) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func WarnPrint(message string) { | ||||||
|  | 	fmt.Println() | ||||||
|  | 	fmt.Println(colorPurple, message, colorReset) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func InputPrint(message string) { | func InputPrint(message string) { | ||||||
| 	// adding \n before setting colorful output can | 	// adding \n before setting colorful output can | ||||||
| 	// remove the first space in the colorful output | 	// remove the first space in the colorful output | ||||||
|  | |||||||
| @ -6,7 +6,7 @@ import ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func PromptContinue() bool { | func PromptContinue() bool { | ||||||
| 	ret := strings.ToLower(prompt("Continue (y/n)")) | 	ret := strings.ToLower(Prompt("Continue (y/n)")) | ||||||
| 	if ret == "y" || ret == "yes" { | 	if ret == "y" || ret == "yes" { | ||||||
| 		return true | 		return true | ||||||
| 	} | 	} | ||||||
| @ -15,7 +15,7 @@ func PromptContinue() bool { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func PromptConfirm(question string) bool { | func PromptConfirm(question string) bool { | ||||||
| 	ret := fmt.Sprintf("%s (y/n)?", question) | 	ret := Prompt(fmt.Sprintf("%s (y/n)?", question)) | ||||||
| 	if ret == "y" || ret == "yes" { | 	if ret == "y" || ret == "yes" { | ||||||
| 		return true | 		return true | ||||||
| 	} | 	} | ||||||
| @ -31,7 +31,7 @@ func SelectMenuItem(listMenu func()) string { | |||||||
| 	return option | 	return option | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func prompt(question string) string { | func Prompt(question string) string { | ||||||
| 	fmt.Printf("%s %s :%s", colorYellow, question, colorReset) | 	fmt.Printf("%s %s :%s", colorYellow, question, colorReset) | ||||||
| 	var ret string | 	var ret string | ||||||
| 	fmt.Scanf("%s", &ret) | 	fmt.Scanf("%s", &ret) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user