Compare commits

..

No commits in common. "d50bf2422670ea7da87ca121e3e709f451913a7a" and "9c56ec0d8549068f805dd7da62e506a96fbd8b1c" have entirely different histories.

4 changed files with 10 additions and 89 deletions

View File

@ -47,31 +47,16 @@ func GetConfig() (*Config, error) {
file, err := getConfigFile(ConfigFileName) file, err := getConfigFile(ConfigFileName)
if err != nil { if err != nil {
if err == ErrConfigNotInitialized { 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) 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) { func initializeConfig(w io.WriteCloser) (*Config, error) {
config := &Config{} config := &Config{}
fmt.Printf("Set the project path: ") fmt.Printf("Set the project path: ")
@ -124,23 +109,13 @@ func initializeConfig(w io.WriteCloser) (*Config, error) {
return config, nil return config, nil
} }
func getConfig(f *os.File) (*Config, error) { func getConfig(f io.Reader) (*Config, error) {
config := &Config{} config := &Config{}
bytes := make([]byte, 0)
info, err := f.Stat() _, err := f.Read(bytes)
if err != nil { if err != nil {
return nil, err 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) err = json.Unmarshal(bytes, &config)
if err != nil { if err != nil {

View File

@ -5,7 +5,6 @@ import (
"ocl/portainer-devtool/configs" "ocl/portainer-devtool/configs"
"ocl/portainer-devtool/tasks" "ocl/portainer-devtool/tasks"
"ocl/portainer-devtool/utils" "ocl/portainer-devtool/utils"
"strconv"
) )
func main() { func main() {
@ -15,8 +14,6 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
config.Summarize()
// Init tasks // Init tasks
taskItems := []tasks.Tasker{ taskItems := []tasks.Tasker{
@ -37,23 +34,8 @@ func main() {
// 5. Quit`) // 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)
}
} }
} }

View File

@ -1,22 +1,11 @@
package tasks package tasks
import ( import "ocl/portainer-devtool/configs"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"ocl/portainer-devtool/configs"
)
type GenerateJwtTokenTask struct { type GenerateJwtTokenTask struct {
Config *configs.Config Config *configs.Config
} }
type GenerateJwtTokenResponse struct {
JWT string `json:"jwt"`
}
func NewGenerateJwtTokenTask(cfg *configs.Config) *GenerateJwtTokenTask { func NewGenerateJwtTokenTask(cfg *configs.Config) *GenerateJwtTokenTask {
return &GenerateJwtTokenTask{ return &GenerateJwtTokenTask{
Config: cfg, Config: cfg,
@ -24,31 +13,6 @@ func NewGenerateJwtTokenTask(cfg *configs.Config) *GenerateJwtTokenTask {
} }
func (task *GenerateJwtTokenTask) Execute() error { 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 return nil
} }

View File

@ -23,7 +23,7 @@ func PromptConfirm(question string) bool {
return false return false
} }
func SelectMenuItem(listMenu func()) string { func PromptMenu(listMenu func()) string {
listMenu() listMenu()
var option string var option string