task: add curl lookup task

pull/7/head
oscarzhou 2022-12-26 00:53:51 +13:00
parent d50bf24226
commit 290e872c15
5 changed files with 83 additions and 7 deletions

View File

@ -21,13 +21,20 @@ func main() {
taskItems := []tasks.Tasker{ taskItems := []tasks.Tasker{
tasks.NewGenerateJwtTokenTask(config), tasks.NewGenerateJwtTokenTask(config),
tasks.NewCurlLookupTask(),
tasks.NewExitTask(),
} }
for { for {
printMainMenu := func() { printMainMenu := func() {
taskNames := []string{}
for _, task := range taskItems {
taskNames = append(taskNames, task.String())
}
utils.PrintMenu("Which repository of action do you want operate:", taskItems) utils.PrintMenu("Which repository of action do you want operate:", taskNames)
// utils.MenuPrint("Which repository or action do you want to operate:", ` // utils.MenuPrint("Which repository or action do you want to operate:", `
// 1. Portainer EE Repository // 1. Portainer EE Repository

View File

@ -0,0 +1,48 @@
package tasks
import (
"fmt"
"ocl/portainer-devtool/utils"
)
type CurlLookupTask struct {
}
func NewCurlLookupTask() *CurlLookupTask {
return &CurlLookupTask{}
}
func (task *CurlLookupTask) Execute() error {
var option string
utils.InputPrint("1.POST 2.GET 3.PUT 4.DELETE: ")
fmt.Scanf("%s", &option)
switch option {
case "1", "POST", "post":
utils.HighlightPrint("POST Command:")
utils.SuccessPrint("curl -d '{\"repository\":\"https://github.com/portainer/portainer-ee\",\"username\":\"oscarzhou\", \"password\":\"your PAT\"}' -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsInNjb3BlIjoiZGVmYXVsdCIsImZvcmNlQ2hhbmdlUGFzc3dvcmQiOmZhbHNlLCJleHAiOjE2NjAwMzQ2MjUsImlhdCI6MTY2MDAwNTgyNX0.S0UbPO4POD9kbuWOmvO9WR6LY6v424bpGw46rlEkNs0' http://127.0.0.1:9000/api/gitops/repo/refs")
break
case "2", "GET", "get":
utils.HighlightPrint("GET Command:")
utils.SuccessPrint("curl -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsInNjb3BlIjoiZGVmYXVsdCIsImZvcmNlQ2hhbmdlUGFzc3dvcmQiOmZhbHNlLCJleHAiOjE2NTUxMTg2ODUsImlhdCI6MTY1NTA4OTg4NX0.mJSZomeiEpRlz36MxSsLFWpUbA0BHRXWYijsZAo1NWc' http://127.0.0.1:9000/api/users/1/gitcredentials")
break
case "3", "PUT", "put":
utils.HighlightPrint("PUT Command:")
utils.SuccessPrint(`curl -X PUT http://127.0.0.1:9000/api/users/1/gitcredentials/11 -d '{"name":"test-credential-11","username":"cred11", "password":"cred11"}' -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsInNjb3BlIjoiZGVmYXVsdCIsImZvcmNlQ2hhbmdlUGFzc3dvcmQiOmZhbHNlLCJleHAiOjE2NTcwODQ5MzUsImlhdCI6MTY1NzA1NjEzNX0.kUhkhhSt4WH33Q3hYzLwsYDv1a9a2ygCi6p8MkKMbwc'`)
break
case "4", "DELETE", "delete":
utils.HighlightPrint("DELETE Command:")
utils.SuccessPrint(`curl -X DELETE http://192.168.1.109:9000/api/users/1/gitcredentials/1 -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsInNjb3BlIjoiZGVmYXVsdCIsImZvcmNlQ2hhbmdlUGFzc3dvcmQiOmZhbHNlLCJleHAiOjE2NTQ3NTc1NzYsImlhdCI6MTY1NDcyODc3Nn0.GlxGmL6XTTH29Ns8aRnX5qp1qBfDVF2zaPzuSmG7qUs'`)
break
default:
return fmt.Errorf("No option %v\n", option)
}
return nil
}
func (task *CurlLookupTask) String() string {
return "Lookup Curl Commands"
}

18
go/tasks/exit.go 100644
View File

@ -0,0 +1,18 @@
package tasks
import "errors"
type ExitTask struct {
}
func NewExitTask() *ExitTask {
return &ExitTask{}
}
func (task *ExitTask) Execute() error {
return errors.New("exit")
}
func (task *ExitTask) String() string {
return "Exit"
}

View File

@ -53,5 +53,5 @@ func (task *GenerateJwtTokenTask) Execute() error {
} }
func (task *GenerateJwtTokenTask) String() string { func (task *GenerateJwtTokenTask) String() string {
return "Generate JWT token" return "Generate JWT Token"
} }

View File

@ -2,7 +2,6 @@ package utils
import ( import (
"fmt" "fmt"
"ocl/portainer-devtool/tasks"
) )
const ( const (
@ -40,19 +39,23 @@ func ErrorPrint(message string) {
} }
func InputPrint(message string) { func InputPrint(message string) {
// adding \n before setting colorful output can
// remove the first space in the colorful output
fmt.Println() fmt.Println()
fmt.Println(colorYellow, message, colorReset) fmt.Println(colorYellow, message, colorReset)
} }
func PrintMenu(question string, tasks []tasks.Tasker) { func PrintMenu(question string, taskNames []string) {
if question != "" { if question != "" {
InputPrint(fmt.Sprintf("[%s]", question)) InputPrint(fmt.Sprintf("[%s]", question))
} }
menuContent := "" // adding \n before setting colorful output can
// remove the first space in the colorful output
menuContent := "\n"
for i, task := range tasks { for i, name := range taskNames {
menuContent += fmt.Sprintf("%d. %s\n", i+1, task.String()) menuContent += fmt.Sprintf("%d. %s\n", i+1, name)
} }
fmt.Println(colorCyan, menuContent, colorReset) fmt.Println(colorCyan, menuContent, colorReset)