Compare commits
	
		
			No commits in common. "d50bf2422670ea7da87ca121e3e709f451913a7a" and "9c56ec0d8549068f805dd7da62e506a96fbd8b1c" have entirely different histories.
		
	
	
		
			d50bf24226
			...
			9c56ec0d85
		
	
		
@ -47,31 +47,16 @@ func GetConfig() (*Config, error) {
 | 
			
		||||
	file, err := getConfigFile(ConfigFileName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if err == ErrConfigNotInitialized {
 | 
			
		||||
			return initializeConfig(file)
 | 
			
		||||
			config, err := initializeConfig(file)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return config, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
	return getConfig(file)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (config *Config) Summarize() {
 | 
			
		||||
	fmt.Printf("The project path is %s\nThe volume path is %s\n", config.ProjectPath, config.VolumePath)
 | 
			
		||||
	if config.LoginCredential.Username != "" && config.LoginCredential.Password != "" {
 | 
			
		||||
		fmt.Printf("Login credential [%s] is configured\n", config.LoginCredential.Username)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(config.RepositoryConfig) > 0 {
 | 
			
		||||
		for name := range config.RepositoryConfig {
 | 
			
		||||
			fmt.Printf("Repository [%s] is added\n", name)
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("No repository is added")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func initializeConfig(w io.WriteCloser) (*Config, error) {
 | 
			
		||||
	config := &Config{}
 | 
			
		||||
	fmt.Printf("Set the project path: ")
 | 
			
		||||
@ -124,23 +109,13 @@ func initializeConfig(w io.WriteCloser) (*Config, error) {
 | 
			
		||||
	return config, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getConfig(f *os.File) (*Config, error) {
 | 
			
		||||
func getConfig(f io.Reader) (*Config, error) {
 | 
			
		||||
	config := &Config{}
 | 
			
		||||
 | 
			
		||||
	info, err := f.Stat()
 | 
			
		||||
	bytes := make([]byte, 0)
 | 
			
		||||
	_, err := f.Read(bytes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	bytes := make([]byte, info.Size())
 | 
			
		||||
	n, err := f.Read(bytes)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if n == 0 {
 | 
			
		||||
		// The file exists, but it's empty file, so we need to initalize
 | 
			
		||||
		return initializeConfig(f)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = json.Unmarshal(bytes, &config)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								go/main.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								go/main.go
									
									
									
									
									
								
							@ -5,7 +5,6 @@ import (
 | 
			
		||||
	"ocl/portainer-devtool/configs"
 | 
			
		||||
	"ocl/portainer-devtool/tasks"
 | 
			
		||||
	"ocl/portainer-devtool/utils"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
@ -15,8 +14,6 @@ func main() {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config.Summarize()
 | 
			
		||||
 | 
			
		||||
	// Init tasks
 | 
			
		||||
 | 
			
		||||
	taskItems := []tasks.Tasker{
 | 
			
		||||
@ -37,23 +34,8 @@ func main() {
 | 
			
		||||
			//  5. Quit`)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		option := utils.SelectMenuItem(printMainMenu)
 | 
			
		||||
		utils.PromptMenu(printMainMenu)
 | 
			
		||||
 | 
			
		||||
		index, err := strconv.Atoi(option)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Printf("please type the option number\n")
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if index < 1 || index > len(taskItems) {
 | 
			
		||||
			log.Printf("no such option %s, please select again\n", option)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = taskItems[index-1].Execute()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalln(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,22 +1,11 @@
 | 
			
		||||
package tasks
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"ocl/portainer-devtool/configs"
 | 
			
		||||
)
 | 
			
		||||
import "ocl/portainer-devtool/configs"
 | 
			
		||||
 | 
			
		||||
type GenerateJwtTokenTask struct {
 | 
			
		||||
	Config *configs.Config
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GenerateJwtTokenResponse struct {
 | 
			
		||||
	JWT string `json:"jwt"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewGenerateJwtTokenTask(cfg *configs.Config) *GenerateJwtTokenTask {
 | 
			
		||||
	return &GenerateJwtTokenTask{
 | 
			
		||||
		Config: cfg,
 | 
			
		||||
@ -24,31 +13,6 @@ func NewGenerateJwtTokenTask(cfg *configs.Config) *GenerateJwtTokenTask {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (task *GenerateJwtTokenTask) Execute() error {
 | 
			
		||||
	postBody, _ := json.Marshal(map[string]string{
 | 
			
		||||
		"username": task.Config.LoginCredential.Username,
 | 
			
		||||
		"password": task.Config.LoginCredential.Password,
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	responseBody := bytes.NewBuffer(postBody)
 | 
			
		||||
	resp, err := http.Post(task.Config.LoginCredential.Address, "application/json", responseBody)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("http requset error: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	//Read the response body
 | 
			
		||||
	body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("failed to parse the response body: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var ret GenerateJwtTokenResponse
 | 
			
		||||
	err = json.Unmarshal(body, &ret)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("jwt token is:\n%s\n", ret.JWT)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -23,7 +23,7 @@ func PromptConfirm(question string) bool {
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SelectMenuItem(listMenu func()) string {
 | 
			
		||||
func PromptMenu(listMenu func()) string {
 | 
			
		||||
	listMenu()
 | 
			
		||||
 | 
			
		||||
	var option string
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user