diff --git a/go/main.go b/go/main.go index 932f70f..f70a3f5 100644 --- a/go/main.go +++ b/go/main.go @@ -21,13 +21,20 @@ func main() { taskItems := []tasks.Tasker{ tasks.NewGenerateJwtTokenTask(config), + tasks.NewCurlLookupTask(), + + tasks.NewExitTask(), } for { 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:", ` // 1. Portainer EE Repository diff --git a/go/tasks/curl_lookup.go b/go/tasks/curl_lookup.go new file mode 100644 index 0000000..be80959 --- /dev/null +++ b/go/tasks/curl_lookup.go @@ -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" +} diff --git a/go/tasks/exit.go b/go/tasks/exit.go new file mode 100644 index 0000000..2067151 --- /dev/null +++ b/go/tasks/exit.go @@ -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" +} diff --git a/go/tasks/jwt_token_gen.go b/go/tasks/jwt_token_gen.go index c1ffecb..17c8f93 100644 --- a/go/tasks/jwt_token_gen.go +++ b/go/tasks/jwt_token_gen.go @@ -53,5 +53,5 @@ func (task *GenerateJwtTokenTask) Execute() error { } func (task *GenerateJwtTokenTask) String() string { - return "Generate JWT token" + return "Generate JWT Token" } diff --git a/go/utils/print.go b/go/utils/print.go index d095382..0144aee 100644 --- a/go/utils/print.go +++ b/go/utils/print.go @@ -2,7 +2,6 @@ package utils import ( "fmt" - "ocl/portainer-devtool/tasks" ) const ( @@ -40,19 +39,23 @@ func ErrorPrint(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(colorYellow, message, colorReset) } -func PrintMenu(question string, tasks []tasks.Tasker) { +func PrintMenu(question string, taskNames []string) { if 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 { - menuContent += fmt.Sprintf("%d. %s\n", i+1, task.String()) + for i, name := range taskNames { + menuContent += fmt.Sprintf("%d. %s\n", i+1, name) } fmt.Println(colorCyan, menuContent, colorReset)