Compare commits

...

3 Commits

4 changed files with 89 additions and 10 deletions

View File

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

View File

@ -5,6 +5,7 @@ import (
"ocl/portainer-devtool/configs"
"ocl/portainer-devtool/tasks"
"ocl/portainer-devtool/utils"
"strconv"
)
func main() {
@ -14,6 +15,8 @@ func main() {
log.Fatalln(err)
}
config.Summarize()
// Init tasks
taskItems := []tasks.Tasker{
@ -34,8 +37,23 @@ func main() {
// 5. Quit`)
}
utils.PromptMenu(printMainMenu)
option := utils.SelectMenuItem(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,11 +1,22 @@
package tasks
import "ocl/portainer-devtool/configs"
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"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,
@ -13,6 +24,31 @@ 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
}

View File

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